David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 1 | %%%------------------------------------------------------------------- |
| 2 | %%% File : thrift_processor.erl |
| 3 | %%% Author : <todd@lipcon.org> |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 4 | %%% Description : |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 5 | %%% |
| 6 | %%% Created : 28 Jan 2008 by <todd@lipcon.org> |
| 7 | %%%------------------------------------------------------------------- |
| 8 | -module(thrift_processor). |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 9 | -author('todd@lipcon.org'). |
| 10 | -author('eletuchy@facebook.com'). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 11 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 12 | -export([init/1]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 13 | |
| 14 | -include("thrift_constants.hrl"). |
| 15 | -include("thrift_protocol.hrl"). |
| 16 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 17 | -record(thrift_processor, {handler, in_protocol, out_protocol, service}). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 18 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 19 | init({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 Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 25 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 26 | loop(State = #thrift_processor{in_protocol = IProto, |
| 27 | out_protocol = OProto}) -> |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 28 | case thrift_protocol:read(IProto, message_begin) of |
| 29 | #protocol_message_begin{name = Function, |
| 30 | type = ?tMessageType_CALL} -> |
David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 31 | ok = handle_function(State, list_to_atom(Function)), |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 32 | loop(State); |
| 33 | {error, closed} -> |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 34 | %% error_logger:info_msg("Client disconnected~n"), |
David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 35 | thrift_protocol:close_transport(OProto), |
| 36 | exit(shutdown) |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 37 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 38 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 39 | handle_function(State=#thrift_processor{in_protocol = IProto, |
| 40 | out_protocol = OProto, |
| 41 | handler = Handler, |
| 42 | service = Service}, |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 43 | Function) -> |
| 44 | InParams = Service:function_info(Function, params_type), |
| 45 | |
| 46 | {ok, Params} = thrift_protocol:read(IProto, InParams), |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 47 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 48 | try |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 49 | Result = Handler:handle_function(Function, Params), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 50 | %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), |
| 51 | %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", |
| 52 | %% [Function, Params, Micro/1000.0]), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 53 | handle_success(State, Function, Result) |
| 54 | catch |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 55 | Type:Data -> |
| 56 | handle_function_catch(State, Function, Type, Data) |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 57 | end, |
| 58 | after_reply(OProto). |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 59 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 60 | handle_function_catch(State = #thrift_processor{service = Service}, |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 61 | Function, ErrType, ErrData) -> |
| 62 | IsAsync = Service:function_info(Function, reply_type) =:= async_void, |
| 63 | |
| 64 | case {ErrType, ErrData} of |
| 65 | _ when IsAsync -> |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame^] | 66 | Stack = erlang:get_stacktrace(), |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 67 | error_logger:warning_msg( |
| 68 | "async void ~p threw error which must be ignored: ~p", |
David Reiss | 4ec777e | 2008-06-11 01:01:29 +0000 | [diff] [blame^] | 69 | [Function, {ErrType, ErrData, Stack}]), |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 70 | ok; |
| 71 | |
| 72 | {throw, Exception} when is_tuple(Exception), size(Exception) > 0 -> |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 73 | error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]), |
| 74 | handle_exception(State, Function, Exception), |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 75 | ok; % we still want to accept more requests from this client |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 76 | |
| 77 | {error, Error} -> |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 78 | ok = handle_error(State, Function, Error) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 79 | end. |
| 80 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 81 | handle_success(State = #thrift_processor{out_protocol = OProto, |
| 82 | service = Service}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 83 | Function, |
| 84 | Result) -> |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 85 | ReplyType = Service:function_info(Function, reply_type), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 86 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 87 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 88 | ok = case Result of |
| 89 | {reply, ReplyData} -> |
| 90 | Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, |
| 91 | send_reply(OProto, Function, ?tMessageType_REPLY, Reply); |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 92 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 93 | ok when ReplyType == {struct, []} -> |
| 94 | send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}); |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 95 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 96 | ok when ReplyType == async_void -> |
| 97 | %% no reply for async void |
| 98 | ok |
| 99 | end. |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 100 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 101 | handle_exception(State = #thrift_processor{out_protocol = OProto, |
| 102 | service = Service}, |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 103 | Function, |
| 104 | Exception) -> |
| 105 | ExceptionType = element(1, Exception), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 106 | %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}}, |
| 107 | %% {-3, {struct, {Module, Type}}}]} |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 108 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 109 | ReplySpec = Service:function_info(Function, exceptions), |
| 110 | {struct, XInfo} = ReplySpec, |
| 111 | |
| 112 | true = is_list(XInfo), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 113 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 114 | %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined] |
| 115 | %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}] |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 116 | ExceptionList = [case Type of |
| 117 | ExceptionType -> Exception; |
| 118 | _ -> undefined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 119 | end |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 120 | || {_Fid, {struct, {_Module, Type}}} <- XInfo], |
| 121 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 122 | ExceptionTuple = list_to_tuple([Function | ExceptionList]), |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 123 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 124 | % Make sure we got at least one defined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 125 | case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of |
| 126 | true -> |
| 127 | ok = handle_unknown_exception(State, Function, Exception); |
| 128 | false -> |
| 129 | ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple}) |
| 130 | end. |
| 131 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 132 | %% |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 133 | %% Called when an exception has been explicitly thrown by the service, but it was |
| 134 | %% not one of the exceptions that was defined for the function. |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 135 | %% |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 136 | handle_unknown_exception(State, Function, Exception) -> |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 137 | handle_error(State, Function, {exception_not_declared_as_thrown, |
| 138 | Exception}). |
| 139 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 140 | handle_error(#thrift_processor{out_protocol = OProto}, Function, Error) -> |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 141 | Stack = erlang:get_stacktrace(), |
| 142 | error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]), |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 143 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 144 | Message = |
| 145 | case application:get_env(thrift, exceptions_include_traces) of |
| 146 | {ok, true} -> |
| 147 | lists:flatten(io_lib:format("An error occurred: ~p~n", |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 148 | [{Error, Stack}])); |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 149 | _ -> |
| 150 | "An unknown handler error occurred." |
| 151 | end, |
| 152 | Reply = {?TApplicationException_Structure, |
| 153 | #'TApplicationException'{ |
| 154 | message = Message, |
| 155 | type = ?TApplicationException_UNKNOWN}}, |
| 156 | send_reply(OProto, Function, ?tMessageType_EXCEPTION, Reply). |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 157 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 158 | send_reply(OProto, Function, ReplyMessageType, Reply) -> |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 159 | ok = thrift_protocol:write(OProto, #protocol_message_begin{ |
| 160 | name = atom_to_list(Function), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 161 | type = ReplyMessageType, |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 162 | seqid = 0}), |
| 163 | ok = thrift_protocol:write(OProto, Reply), |
| 164 | ok = thrift_protocol:write(OProto, message_end), |
David Reiss | 90b4083 | 2008-06-10 22:58:52 +0000 | [diff] [blame] | 165 | ok = thrift_protocol:flush_transport(OProto), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 166 | ok. |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 167 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 168 | after_reply(OProto) -> |
David Reiss | 7255ed4 | 2008-06-11 01:00:04 +0000 | [diff] [blame] | 169 | ok = thrift_protocol:flush_transport(OProto) |
| 170 | %% ok = thrift_protocol:close_transport(OProto) |
| 171 | . |