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