blob: 3344666028f20cb5541731c487542884f92172df [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 Reiss80664fe2008-06-11 01:00:30 +000031 ok = handle_function(State, list_to_atom(Function)),
David Reissae756f42008-06-10 22:57:11 +000032 loop(State);
David Reissc49dd1e2008-06-11 01:02:39 +000033 {error, timeout} ->
34 thrift_protocol:close_transport(OProto),
35 ok;
David Reissae756f42008-06-10 22:57:11 +000036 {error, closed} ->
David Reissc11734e2008-06-11 00:59:48 +000037 %% error_logger:info_msg("Client disconnected~n"),
David Reiss80664fe2008-06-11 01:00:30 +000038 thrift_protocol:close_transport(OProto),
39 exit(shutdown)
David Reissac549552008-06-10 22:56:59 +000040 end.
David Reissae756f42008-06-10 22:57:11 +000041
David Reissc11734e2008-06-11 00:59:48 +000042handle_function(State=#thrift_processor{in_protocol = IProto,
43 out_protocol = OProto,
44 handler = Handler,
45 service = Service},
David Reiss1c1ca742008-06-10 22:57:21 +000046 Function) ->
47 InParams = Service:function_info(Function, params_type),
48
49 {ok, Params} = thrift_protocol:read(IProto, InParams),
David Reiss76f0d112008-06-10 22:57:35 +000050
David Reiss982c72d2008-06-10 22:58:33 +000051 try
David Reiss11d855c2008-06-11 00:59:12 +000052 Result = Handler:handle_function(Function, Params),
David Reissc11734e2008-06-11 00:59:48 +000053 %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]),
54 %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n",
55 %% [Function, Params, Micro/1000.0]),
David Reiss982c72d2008-06-10 22:58:33 +000056 handle_success(State, Function, Result)
57 catch
David Reiss5541d652008-06-11 00:57:42 +000058 Type:Data ->
59 handle_function_catch(State, Function, Type, Data)
David Reissc11734e2008-06-11 00:59:48 +000060 end,
61 after_reply(OProto).
David Reiss5541d652008-06-11 00:57:42 +000062
David Reissc11734e2008-06-11 00:59:48 +000063handle_function_catch(State = #thrift_processor{service = Service},
David Reiss5541d652008-06-11 00:57:42 +000064 Function, ErrType, ErrData) ->
David Reiss6ce401d2009-03-24 20:01:58 +000065 IsOneway = Service:function_info(Function, reply_type) =:= async_void,
David Reiss5541d652008-06-11 00:57:42 +000066
67 case {ErrType, ErrData} of
David Reiss6ce401d2009-03-24 20:01:58 +000068 _ when IsOneway ->
David Reiss4ec777e2008-06-11 01:01:29 +000069 Stack = erlang:get_stacktrace(),
David Reiss5541d652008-06-11 00:57:42 +000070 error_logger:warning_msg(
David Reiss6ce401d2009-03-24 20:01:58 +000071 "oneway void ~p threw error which must be ignored: ~p",
David Reiss4ec777e2008-06-11 01:01:29 +000072 [Function, {ErrType, ErrData, Stack}]),
David Reiss5541d652008-06-11 00:57:42 +000073 ok;
74
75 {throw, Exception} when is_tuple(Exception), size(Exception) > 0 ->
David Reiss982c72d2008-06-10 22:58:33 +000076 error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]),
77 handle_exception(State, Function, Exception),
David Reiss920959a2008-06-11 00:56:35 +000078 ok; % we still want to accept more requests from this client
David Reiss5541d652008-06-11 00:57:42 +000079
80 {error, Error} ->
David Reiss920959a2008-06-11 00:56:35 +000081 ok = handle_error(State, Function, Error)
David Reiss982c72d2008-06-10 22:58:33 +000082 end.
83
David Reissc11734e2008-06-11 00:59:48 +000084handle_success(State = #thrift_processor{out_protocol = OProto,
85 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +000086 Function,
87 Result) ->
David Reiss11d855c2008-06-11 00:59:12 +000088 ReplyType = Service:function_info(Function, reply_type),
David Reisse1a79982008-06-10 22:58:14 +000089 StructName = atom_to_list(Function) ++ "_result",
David Reiss11d855c2008-06-11 00:59:12 +000090
David Reissc11734e2008-06-11 00:59:48 +000091 ok = case Result of
92 {reply, ReplyData} ->
93 Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
94 send_reply(OProto, Function, ?tMessageType_REPLY, Reply);
David Reisse1a79982008-06-10 22:58:14 +000095
David Reissc11734e2008-06-11 00:59:48 +000096 ok when ReplyType == {struct, []} ->
97 send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});
David Reiss11d855c2008-06-11 00:59:12 +000098
David Reissc11734e2008-06-11 00:59:48 +000099 ok when ReplyType == async_void ->
David Reissc51986f2009-03-24 20:01:25 +0000100 %% no reply for oneway void
David Reissc11734e2008-06-11 00:59:48 +0000101 ok
102 end.
David Reisse1a79982008-06-10 22:58:14 +0000103
David Reissc11734e2008-06-11 00:59:48 +0000104handle_exception(State = #thrift_processor{out_protocol = OProto,
105 service = Service},
David Reiss982c72d2008-06-10 22:58:33 +0000106 Function,
107 Exception) ->
108 ExceptionType = element(1, Exception),
David Reissc11734e2008-06-11 00:59:48 +0000109 %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}},
110 %% {-3, {struct, {Module, Type}}}]}
David Reisse1a79982008-06-10 22:58:14 +0000111
David Reiss982c72d2008-06-10 22:58:33 +0000112 ReplySpec = Service:function_info(Function, exceptions),
113 {struct, XInfo} = ReplySpec,
114
115 true = is_list(XInfo),
David Reiss982c72d2008-06-10 22:58:33 +0000116
David Reissc11734e2008-06-11 00:59:48 +0000117 %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined]
118 %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}]
David Reiss11d855c2008-06-11 00:59:12 +0000119 ExceptionList = [case Type of
120 ExceptionType -> Exception;
121 _ -> undefined
David Reiss982c72d2008-06-10 22:58:33 +0000122 end
David Reiss11d855c2008-06-11 00:59:12 +0000123 || {_Fid, {struct, {_Module, Type}}} <- XInfo],
124
David Reiss982c72d2008-06-10 22:58:33 +0000125 ExceptionTuple = list_to_tuple([Function | ExceptionList]),
David Reiss11d855c2008-06-11 00:59:12 +0000126
David Reissc11734e2008-06-11 00:59:48 +0000127 % Make sure we got at least one defined
David Reiss982c72d2008-06-10 22:58:33 +0000128 case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of
129 true ->
130 ok = handle_unknown_exception(State, Function, Exception);
131 false ->
132 ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple})
133 end.
134
David Reiss920959a2008-06-11 00:56:35 +0000135%%
David Reissc11734e2008-06-11 00:59:48 +0000136%% Called when an exception has been explicitly thrown by the service, but it was
137%% not one of the exceptions that was defined for the function.
David Reiss920959a2008-06-11 00:56:35 +0000138%%
David Reiss982c72d2008-06-10 22:58:33 +0000139handle_unknown_exception(State, Function, Exception) ->
David Reiss920959a2008-06-11 00:56:35 +0000140 handle_error(State, Function, {exception_not_declared_as_thrown,
141 Exception}).
142
David Reissc11734e2008-06-11 00:59:48 +0000143handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) ->
David Reissda070672008-06-11 00:56:42 +0000144 Stack = erlang:get_stacktrace(),
145 error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]),
David Reiss11d855c2008-06-11 00:59:12 +0000146
David Reiss920959a2008-06-11 00:56:35 +0000147 Message =
148 case application:get_env(thrift, exceptions_include_traces) of
149 {ok, true} ->
150 lists:flatten(io_lib:format("An error occurred: ~p~n",
David Reissda070672008-06-11 00:56:42 +0000151 [{Error, Stack}]));
David Reiss920959a2008-06-11 00:56:35 +0000152 _ ->
153 "An unknown handler error occurred."
154 end,
155 Reply = {?TApplicationException_Structure,
156 #'TApplicationException'{
157 message = Message,
158 type = ?TApplicationException_UNKNOWN}},
159 send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply).
David Reiss982c72d2008-06-10 22:58:33 +0000160
David Reiss982c72d2008-06-10 22:58:33 +0000161send_reply(OProto, Function, ReplyMessageType, Reply) ->
David Reisse1a79982008-06-10 22:58:14 +0000162 ok = thrift_protocol:write(OProto, #protocol_message_begin{
163 name = atom_to_list(Function),
David Reiss982c72d2008-06-10 22:58:33 +0000164 type = ReplyMessageType,
David Reisse1a79982008-06-10 22:58:14 +0000165 seqid = 0}),
166 ok = thrift_protocol:write(OProto, Reply),
167 ok = thrift_protocol:write(OProto, message_end),
David Reiss90b40832008-06-10 22:58:52 +0000168 ok = thrift_protocol:flush_transport(OProto),
David Reisse1a79982008-06-10 22:58:14 +0000169 ok.
David Reiss982c72d2008-06-10 22:58:33 +0000170
David Reissc11734e2008-06-11 00:59:48 +0000171after_reply(OProto) ->
David Reiss7255ed42008-06-11 01:00:04 +0000172 ok = thrift_protocol:flush_transport(OProto)
173 %% ok = thrift_protocol:close_transport(OProto)
174 .