blob: c78dda345651035a3d6192c9b9288650b46bebac [file] [log] [blame]
David Reissac549552008-06-10 22:56:59 +00001%%%-------------------------------------------------------------------
2%%% File : thrift_processor.erl
3%%% Author : <todd@lipcon.org>
David Reiss11d855c2008-06-11 00:59:12 +00004%%% Description :
David Reissac549552008-06-10 22:56:59 +00005%%%
6%%% Created : 28 Jan 2008 by <todd@lipcon.org>
7%%%-------------------------------------------------------------------
8-module(thrift_processor).
David Reissc11734e2008-06-11 00:59:48 +00009-author('todd@lipcon.org').
10-author('eletuchy@facebook.com').
David Reissac549552008-06-10 22:56:59 +000011
David Reissc11734e2008-06-11 00:59:48 +000012-export([init/1]).
David Reissac549552008-06-10 22:56:59 +000013
14-include("thrift_constants.hrl").
15-include("thrift_protocol.hrl").
16
David Reissc11734e2008-06-11 00:59:48 +000017-record(thrift_processor, {handler, in_protocol, out_protocol, service}).
David Reissac549552008-06-10 22:56:59 +000018
David Reissc11734e2008-06-11 00:59:48 +000019init({Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) ->
20 {ok, IProt, OProt} = ProtoGen(),
21 loop(#thrift_processor{in_protocol = IProt,
22 out_protocol = OProt,
23 service = Service,
24 handler = Handler}).
David Reissac549552008-06-10 22:56:59 +000025
David Reissc11734e2008-06-11 00:59:48 +000026loop(State = #thrift_processor{in_protocol = IProto,
27 out_protocol = OProto}) ->
David Reissae756f42008-06-10 22:57:11 +000028 case thrift_protocol:read(IProto, message_begin) of
29 #protocol_message_begin{name = Function,
30 type = ?tMessageType_CALL} ->
David Reissc11734e2008-06-11 00:59:48 +000031 ok=handle_function(State, list_to_atom(Function)),
David Reissae756f42008-06-10 22:57:11 +000032 loop(State);
33 {error, closed} ->
David Reissc11734e2008-06-11 00:59:48 +000034 %% error_logger:info_msg("Client disconnected~n"),
David Reissa5a53db2008-06-11 00:59:34 +000035 exit(protocol_closed)
David Reissac549552008-06-10 22:56:59 +000036 end.
David Reissae756f42008-06-10 22:57:11 +000037
David Reissc11734e2008-06-11 00:59:48 +000038handle_function(State=#thrift_processor{in_protocol = IProto,
39 out_protocol = OProto,
40 handler = Handler,
41 service = Service},
David Reiss1c1ca742008-06-10 22:57:21 +000042 Function) ->
43 InParams = Service:function_info(Function, params_type),
44
45 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000046
David Reiss982c72d2008-06-10 22:58:33 +000047 try
David Reiss11d855c2008-06-11 00:59:12 +000048 Result = Handler:handle_function(Function, Params),
David Reissc11734e2008-06-11 00:59:48 +000049 %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]),
50 %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n",
51 %% [Function, Params, Micro/1000.0]),
David Reiss982c72d2008-06-10 22:58:33 +000052 handle_success(State, Function, Result)
53 catch
David Reiss5541d652008-06-11 00:57:42 +000054 Type:Data ->
David Reissc11734e2008-06-11 00:59:48 +000055 error_logger:info_msg("handle_function oh noes: ~p ~p", [Type, Data]),
David Reiss5541d652008-06-11 00:57:42 +000056 handle_function_catch(State, Function, Type, Data)
David Reissc11734e2008-06-11 00:59:48 +000057 end,
58 after_reply(OProto).
David Reiss5541d652008-06-11 00:57:42 +000059
David Reissc11734e2008-06-11 00:59:48 +000060handle_function_catch(State = #thrift_processor{service = Service},
David Reiss5541d652008-06-11 00:57:42 +000061 Function, ErrType, ErrData) ->
62 IsAsync = Service:function_info(Function, reply_type) =:= async_void,
63
64 case {ErrType, ErrData} of
65 _ when IsAsync ->
66 error_logger:warning_msg(
67 "async void ~p threw error which must be ignored: ~p",
68 [Function, {ErrType, ErrData}]),
69 ok;
70
71 {throw, Exception} when is_tuple(Exception), size(Exception) > 0 ->
David Reiss982c72d2008-06-10 22:58:33 +000072 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
73 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000074 ok; % we still want to accept more requests from this client
David Reiss5541d652008-06-11 00:57:42 +000075
76 {error, Error} ->
David Reiss920959a2008-06-11 00:56:35 +000077 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000078 end.
79
David Reissc11734e2008-06-11 00:59:48 +000080handle_success(State = #thrift_processor{out_protocol = OProto,
81 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +000082 Function,
83 Result) ->
David Reiss11d855c2008-06-11 00:59:12 +000084 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +000085 StructName = atom_to_list(Function) ++ "_result",
David Reiss11d855c2008-06-11 00:59:12 +000086
David Reissc11734e2008-06-11 00:59:48 +000087 ok = case Result of
88 {reply, ReplyData} ->
89 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
90 send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +000091
David Reissc11734e2008-06-11 00:59:48 +000092 ok when ReplyType == {struct, []} ->
93 send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});
David Reiss11d855c2008-06-11 00:59:12 +000094
David Reissc11734e2008-06-11 00:59:48 +000095 ok when ReplyType == async_void ->
96 %% no reply for async void
97 ok
98 end.
David Reisse1a79982008-06-10 22:58:14 +000099
David Reissc11734e2008-06-11 00:59:48 +0000100handle_exception(State = #thrift_processor{out_protocol = OProto,
101 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000102 Function,
103 Exception) ->
104 ExceptionType = element(1, Exception),
David Reissc11734e2008-06-11 00:59:48 +0000105 %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
106 %% {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +0000107
David Reiss982c72d2008-06-10 22:58:33 +0000108 ReplySpec = Service:function_info(Function, exceptions),
109 {struct, XInfo} = ReplySpec,
110
111 true = is_list(XInfo),
David Reiss982c72d2008-06-10 22:58:33 +0000112
David Reissc11734e2008-06-11 00:59:48 +0000113 %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined]
114 %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
David Reiss11d855c2008-06-11 00:59:12 +0000115 ExceptionList = [case Type of
116 ExceptionType -> Exception;
117 _ -> undefined
David Reiss982c72d2008-06-10 22:58:33 +0000118 end
David Reiss11d855c2008-06-11 00:59:12 +0000119 || {_Fid, {struct, {_Module, Type}}} <- XInfo],
120
David Reiss982c72d2008-06-10 22:58:33 +0000121 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
David Reiss11d855c2008-06-11 00:59:12 +0000122
David Reissc11734e2008-06-11 00:59:48 +0000123 % Make sure we got at least one defined
David Reiss982c72d2008-06-10 22:58:33 +0000124 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
125 true ->
126 ok = handle_unknown_exception(State, Function, Exception);
127 false ->
128 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple})
129 end.
130
David Reiss920959a2008-06-11 00:56:35 +0000131%%
David Reissc11734e2008-06-11 00:59:48 +0000132%% Called when an exception has been explicitly thrown by the service, but it was
133%% not one of the exceptions that was defined for the function.
David Reiss920959a2008-06-11 00:56:35 +0000134%%
David Reiss982c72d2008-06-10 22:58:33 +0000135handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000136 handle_error(State, Function, {exception_not_declared_as_thrown,
137 Exception}).
138
David Reissc11734e2008-06-11 00:59:48 +0000139handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000140 Stack = erlang:get_stacktrace(),
141 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
David Reiss11d855c2008-06-11 00:59:12 +0000142
David Reiss920959a2008-06-11 00:56:35 +0000143 Message =
144 case application:get_env(thrift, exceptions_include_traces) of
145 {ok, true} ->
146 lists:flatten(io_lib:format("An error occurred: ~p~n",
David Reissda070672008-06-11 00:56:42 +0000147 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000148 _ ->
149 "An unknown handler error occurred."
150 end,
151 Reply = {?TApplicationException_Structure,
152 #'TApplicationException'{
153 message = Message,
154 type = ?TApplicationException_UNKNOWN}},
155 send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply).
David Reiss982c72d2008-06-10 22:58:33 +0000156
David Reiss982c72d2008-06-10 22:58:33 +0000157send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000158 ok = thrift_protocol:write(OProto, #protocol_message_begin{
159 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000160 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000161 seqid = 0}),
162 ok = thrift_protocol:write(OProto, Reply),
163 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000164 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000165 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000166
David Reissc11734e2008-06-11 00:59:48 +0000167after_reply(OProto) ->
David Reiss7255ed42008-06-11 01:00:04 +0000168 ok = thrift_protocol:flush_transport(OProto)
169 %% ok = thrift_protocol:close_transport(OProto)
170 .