David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | %% |
| 2 | %% Licensed to the Apache Software Foundation (ASF) under one |
| 3 | %% or more contributor license agreements. See the NOTICE file |
| 4 | %% distributed with this work for additional information |
| 5 | %% regarding copyright ownership. The ASF licenses this file |
| 6 | %% to you under the Apache License, Version 2.0 (the |
| 7 | %% "License"); you may not use this file except in compliance |
| 8 | %% with the License. You may obtain a copy of the License at |
| 9 | %% |
| 10 | %% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | %% |
| 12 | %% Unless required by applicable law or agreed to in writing, |
| 13 | %% software distributed under the License is distributed on an |
| 14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | %% KIND, either express or implied. See the License for the |
| 16 | %% specific language governing permissions and limitations |
| 17 | %% under the License. |
| 18 | %% |
| 19 | |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 20 | -module(thrift_processor). |
| 21 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 22 | -export([init/1]). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 23 | |
| 24 | -include("thrift_constants.hrl"). |
| 25 | -include("thrift_protocol.hrl"). |
| 26 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 27 | -record(thrift_processor, { |
| 28 | handler :: module(), |
| 29 | protocol :: term(), |
| 30 | service :: atom() |
| 31 | }). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 32 | |
David Reiss | 5ed313d | 2010-08-30 22:05:57 +0000 | [diff] [blame] | 33 | init({_Server, ProtoGen, Service, Handler}) when is_function(ProtoGen, 0) -> |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 34 | {ok, Proto} = ProtoGen(), |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 35 | loop(#thrift_processor{ |
| 36 | protocol = Proto, |
| 37 | service = Service, |
| 38 | handler = Handler |
| 39 | }). |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 40 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 41 | loop( |
| 42 | State0 = #thrift_processor{ |
| 43 | protocol = Proto0, |
| 44 | handler = Handler, |
| 45 | service = Service |
| 46 | } |
| 47 | ) -> |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 48 | {Proto1, MessageBegin} = thrift_protocol:read(Proto0, message_begin), |
| 49 | State1 = State0#thrift_processor{protocol = Proto1}, |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 50 | |
| 51 | ErrorHandler = fun |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 52 | (HandlerModules) when is_list(HandlerModules) -> |
| 53 | thrift_multiplexed_map_wrapper:fetch(?MULTIPLEXED_ERROR_HANDLER_KEY, HandlerModules); |
| 54 | (HandlerModule) -> |
| 55 | HandlerModule |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 56 | end, |
| 57 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 58 | case MessageBegin of |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 59 | #protocol_message_begin{ |
| 60 | name = Function, |
| 61 | type = Type, |
| 62 | seqid = Seqid |
| 63 | } when Type =:= ?tMessageType_CALL; Type =:= ?tMessageType_ONEWAY -> |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 64 | case string:tokens(Function, ?MULTIPLEXED_SERVICE_SEPARATOR) of |
| 65 | [ServiceName, FunctionName] -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 66 | ServiceModule = thrift_multiplexed_map_wrapper:fetch(ServiceName, Service), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 67 | ServiceHandler = thrift_multiplexed_map_wrapper:fetch(ServiceName, Handler), |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 68 | case |
| 69 | handle_function( |
| 70 | State1#thrift_processor{ |
| 71 | service = ServiceModule, handler = ServiceHandler |
| 72 | }, |
| 73 | list_to_existing_atom(FunctionName), |
| 74 | Seqid |
| 75 | ) |
| 76 | of |
| 77 | {State2, ok} -> |
| 78 | loop(State2#thrift_processor{service = Service, handler = Handler}); |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 79 | {_State2, {error, Reason}} -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 80 | apply(ErrorHandler(Handler), handle_error, [ |
| 81 | list_to_existing_atom(Function), Reason |
| 82 | ]), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 83 | thrift_protocol:close_transport(Proto1), |
| 84 | ok |
| 85 | end; |
| 86 | _ -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 87 | case handle_function(State1, list_to_existing_atom(Function), Seqid) of |
| 88 | {State2, ok} -> |
| 89 | loop(State2); |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 90 | {_State2, {error, Reason}} -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 91 | apply(ErrorHandler(Handler), handle_error, [ |
| 92 | list_to_existing_atom(Function), Reason |
| 93 | ]), |
David Robakowski | ae971ce | 2013-08-02 12:16:00 +0200 | [diff] [blame] | 94 | thrift_protocol:close_transport(Proto1), |
| 95 | ok |
| 96 | end |
Roger Meier | 3f972b1 | 2012-05-18 11:35:51 +0000 | [diff] [blame] | 97 | end; |
| 98 | {error, timeout = Reason} -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 99 | apply(ErrorHandler(Handler), handle_error, [undefined, Reason]), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 100 | thrift_protocol:close_transport(Proto1), |
David Reiss | c49dd1e | 2008-06-11 01:02:39 +0000 | [diff] [blame] | 101 | ok; |
Roger Meier | 3f972b1 | 2012-05-18 11:35:51 +0000 | [diff] [blame] | 102 | {error, closed = Reason} -> |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 103 | %% error_logger:info_msg("Client disconnected~n"), |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 104 | apply(ErrorHandler(Handler), handle_error, [undefined, Reason]), |
Roger Meier | 3f972b1 | 2012-05-18 11:35:51 +0000 | [diff] [blame] | 105 | thrift_protocol:close_transport(Proto1), |
| 106 | exit(shutdown); |
| 107 | {error, Reason} -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 108 | apply(ErrorHandler(Handler), handle_error, [undefined, Reason]), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 109 | thrift_protocol:close_transport(Proto1), |
David Reiss | 80664fe | 2008-06-11 01:00:30 +0000 | [diff] [blame] | 110 | exit(shutdown) |
David Reiss | ac54955 | 2008-06-10 22:56:59 +0000 | [diff] [blame] | 111 | end. |
David Reiss | ae756f4 | 2008-06-10 22:57:11 +0000 | [diff] [blame] | 112 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 113 | handle_function( |
| 114 | State0 = #thrift_processor{ |
| 115 | protocol = Proto0, |
| 116 | handler = Handler, |
| 117 | service = Service |
| 118 | }, |
| 119 | Function, |
| 120 | Seqid |
| 121 | ) -> |
David Reiss | 1c1ca74 | 2008-06-10 22:57:21 +0000 | [diff] [blame] | 122 | InParams = Service:function_info(Function, params_type), |
| 123 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 124 | {Proto1, {ok, Params}} = thrift_protocol:read(Proto0, InParams), |
| 125 | State1 = State0#thrift_processor{protocol = Proto1}, |
David Reiss | 76f0d11 | 2008-06-10 22:57:35 +0000 | [diff] [blame] | 126 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 127 | try |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 128 | Result = Handler:handle_function(Function, Params), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 129 | %% {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), |
| 130 | %% error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", |
| 131 | %% [Function, Params, Micro/1000.0]), |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame] | 132 | handle_success(State1, Function, Result, Seqid) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 133 | catch |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 134 | Type:Data:Stack when Type =:= throw orelse Type =:= error -> |
| 135 | handle_function_catch(State1, Function, Type, Data, Seqid, Stack) |
David Reiss | 4b0534a | 2010-08-30 22:05:37 +0000 | [diff] [blame] | 136 | end. |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 137 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 138 | handle_function_catch( |
| 139 | State = #thrift_processor{service = Service}, |
| 140 | Function, |
| 141 | ErrType, |
| 142 | ErrData, |
| 143 | Seqid, |
| 144 | Stack |
| 145 | ) -> |
David Reiss | fe931d1 | 2009-03-24 20:02:08 +0000 | [diff] [blame] | 146 | IsOneway = Service:function_info(Function, reply_type) =:= oneway_void, |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 147 | |
| 148 | case {ErrType, ErrData} of |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 149 | _ when IsOneway -> |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 150 | error_logger:warning_msg( |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 151 | "oneway void ~p threw error which must be ignored: ~p", |
| 152 | [Function, {ErrType, ErrData, Stack}] |
| 153 | ), |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 154 | {State, ok}; |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 155 | {throw, Exception} when is_tuple(Exception), size(Exception) > 0 -> |
David Reiss | c0e4b5b | 2010-08-30 22:05:12 +0000 | [diff] [blame] | 156 | %error_logger:warning_msg("~p threw exception: ~p~n", [Function, Exception]), |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 157 | handle_exception(State, Function, Exception, Seqid, Stack); |
| 158 | % we still want to accept more requests from this client |
David Reiss | 5541d65 | 2008-06-11 00:57:42 +0000 | [diff] [blame] | 159 | |
| 160 | {error, Error} -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 161 | handle_error(State, Function, Error, Seqid, Stack) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 162 | end. |
| 163 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 164 | handle_success( |
| 165 | State = #thrift_processor{service = Service}, |
| 166 | Function, |
| 167 | Result, |
| 168 | Seqid |
| 169 | ) -> |
| 170 | ReplyType = Service:function_info(Function, reply_type), |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 171 | StructName = atom_to_list(Function) ++ "_result", |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 172 | |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 173 | case Result of |
| 174 | {reply, ReplyData} -> |
| 175 | Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame] | 176 | send_reply(State, Function, ?tMessageType_REPLY, Reply, Seqid); |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 177 | ok when ReplyType == {struct, []} -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame] | 178 | send_reply(State, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}, Seqid); |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 179 | ok when ReplyType == oneway_void -> |
| 180 | %% no reply for oneway void |
| 181 | {State, ok} |
| 182 | end. |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 183 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 184 | handle_exception( |
| 185 | State = #thrift_processor{service = Service}, |
| 186 | Function, |
| 187 | Exception, |
| 188 | Seqid, |
| 189 | Stack |
| 190 | ) -> |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 191 | ExceptionType = element(1, Exception), |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 192 | %% Fetch a structure like {struct, [{-2, {struct, {Module, Type}}}, |
| 193 | %% {-3, {struct, {Module, Type}}}]} |
David Reiss | e1a7998 | 2008-06-10 22:58:14 +0000 | [diff] [blame] | 194 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 195 | ReplySpec = Service:function_info(Function, exceptions), |
| 196 | {struct, XInfo} = ReplySpec, |
| 197 | |
| 198 | true = is_list(XInfo), |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 199 | |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 200 | %% Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined] |
| 201 | %% e.g.: [{-1, type0}, {-2, type1}, {-3, type2}] |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 202 | ExceptionList = [ |
| 203 | case Type of |
| 204 | ExceptionType -> Exception; |
| 205 | _ -> undefined |
| 206 | end |
| 207 | || {_Fid, {struct, {_Module, Type}}} <- XInfo |
| 208 | ], |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 209 | |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 210 | ExceptionTuple = list_to_tuple([Function | ExceptionList]), |
David Reiss | 11d855c | 2008-06-11 00:59:12 +0000 | [diff] [blame] | 211 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 212 | % Make sure we got at least one defined |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 213 | case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of |
| 214 | true -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 215 | handle_unknown_exception(State, Function, Exception, Seqid, Stack); |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 216 | false -> |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame] | 217 | send_reply(State, Function, ?tMessageType_REPLY, {ReplySpec, ExceptionTuple}, Seqid) |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 218 | end. |
| 219 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 220 | %% |
David Reiss | c11734e | 2008-06-11 00:59:48 +0000 | [diff] [blame] | 221 | %% Called when an exception has been explicitly thrown by the service, but it was |
| 222 | %% not one of the exceptions that was defined for the function. |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 223 | %% |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 224 | handle_unknown_exception(State, Function, Exception, Seqid, Stack) -> |
| 225 | handle_error(State, Function, {exception_not_declared_as_thrown, Exception}, Seqid, Stack). |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 226 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 227 | handle_error(State, Function, Error, Seqid, Stack) -> |
David Reiss | da07067 | 2008-06-11 00:56:42 +0000 | [diff] [blame] | 228 | 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] | 229 | |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 230 | Message = |
| 231 | case application:get_env(thrift, exceptions_include_traces) of |
| 232 | {ok, true} -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 233 | lists:flatten( |
| 234 | io_lib:format( |
| 235 | "An error occurred: ~p~n", |
| 236 | [{Error, Stack}] |
| 237 | ) |
| 238 | ); |
David Reiss | 920959a | 2008-06-11 00:56:35 +0000 | [diff] [blame] | 239 | _ -> |
| 240 | "An unknown handler error occurred." |
| 241 | end, |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 242 | Reply = |
| 243 | {?TApplicationException_Structure, #'TApplicationException'{ |
| 244 | message = Message, |
| 245 | type = ?TApplicationException_UNKNOWN |
| 246 | }}, |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame] | 247 | send_reply(State, Function, ?tMessageType_EXCEPTION, Reply, Seqid). |
David Reiss | 982c72d | 2008-06-10 22:58:33 +0000 | [diff] [blame] | 248 | |
Bryan Duxbury | a2cceb4 | 2011-03-02 18:25:24 +0000 | [diff] [blame] | 249 | send_reply(State = #thrift_processor{protocol = Proto0}, Function, ReplyMessageType, Reply, Seqid) -> |
Roger Meier | 3f972b1 | 2012-05-18 11:35:51 +0000 | [diff] [blame] | 250 | try |
| 251 | {Proto1, ok} = thrift_protocol:write(Proto0, #protocol_message_begin{ |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame^] | 252 | name = atom_to_list(Function), |
| 253 | type = ReplyMessageType, |
| 254 | seqid = Seqid |
| 255 | }), |
Roger Meier | 3f972b1 | 2012-05-18 11:35:51 +0000 | [diff] [blame] | 256 | {Proto2, ok} = thrift_protocol:write(Proto1, Reply), |
| 257 | {Proto3, ok} = thrift_protocol:write(Proto2, message_end), |
| 258 | {Proto4, ok} = thrift_protocol:flush_transport(Proto3), |
| 259 | {State#thrift_processor{protocol = Proto4}, ok} |
| 260 | catch |
| 261 | error:{badmatch, {_, {error, _} = Error}} -> |
| 262 | {State, Error} |
| 263 | end. |