blob: 4e3c052bffbc4d2742936c410d955568fcb7126e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
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%%
David Reiss80862312008-06-11 00:59:55 +000019
David Reissea2cba82009-03-30 21:35:00 +000020-module(thrift_socket_server).
David Reiss80862312008-06-11 00:59:55 +000021
22-behaviour(gen_server).
23
David Robakowskiae971ce2013-08-02 12:16:00 +020024-include ("thrift_constants.hrl").
David Reiss80862312008-06-11 00:59:55 +000025
David Robakowskiae971ce2013-08-02 12:16:00 +020026-ifdef(TEST).
27 -compile(export_all).
28 -export_records([thrift_socket_server]).
29-else.
30 -export([start/1, stop/1]).
David Reiss80862312008-06-11 00:59:55 +000031
David Robakowskiae971ce2013-08-02 12:16:00 +020032 -export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3,
33 handle_info/2]).
34
35 -export([acceptor_loop/1]).
36-endif.
David Reiss80862312008-06-11 00:59:55 +000037
38-record(thrift_socket_server,
David Reiss1a2f2182008-06-11 01:14:01 +000039 {port,
David Reiss80862312008-06-11 00:59:55 +000040 service,
41 handler,
David Reiss1a2f2182008-06-11 01:14:01 +000042 name,
43 max=2048,
44 ip=any,
45 listen=null,
46 acceptor=null,
David Reissb42361c2009-09-09 17:18:57 +000047 socket_opts=[{recv_timeout, 500}],
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090048 protocol=binary,
David Robakowskia7d6a972013-08-07 05:51:00 +020049 framed=false,
50 ssltransport=false,
51 ssloptions=[]
David Reissb7c88022008-06-11 01:00:20 +000052 }).
David Reiss80862312008-06-11 00:59:55 +000053
54start(State=#thrift_socket_server{}) ->
David Reiss80862312008-06-11 00:59:55 +000055 start_server(State);
56start(Options) ->
57 start(parse_options(Options)).
58
59stop(Name) when is_atom(Name) ->
60 gen_server:cast(Name, stop);
61stop(Pid) when is_pid(Pid) ->
62 gen_server:cast(Pid, stop);
63stop({local, Name}) ->
64 stop(Name);
65stop({global, Name}) ->
66 stop(Name);
67stop(Options) ->
68 State = parse_options(Options),
69 stop(State#thrift_socket_server.name).
70
71%% Internal API
72
73parse_options(Options) ->
74 parse_options(Options, #thrift_socket_server{}).
75
76parse_options([], State) ->
77 State;
78parse_options([{name, L} | Rest], State) when is_list(L) ->
79 Name = {local, list_to_atom(L)},
80 parse_options(Rest, State#thrift_socket_server{name=Name});
81parse_options([{name, A} | Rest], State) when is_atom(A) ->
82 Name = {local, A},
83 parse_options(Rest, State#thrift_socket_server{name=Name});
84parse_options([{name, Name} | Rest], State) ->
85 parse_options(Rest, State#thrift_socket_server{name=Name});
86parse_options([{port, L} | Rest], State) when is_list(L) ->
87 Port = list_to_integer(L),
88 parse_options(Rest, State#thrift_socket_server{port=Port});
89parse_options([{port, Port} | Rest], State) ->
90 parse_options(Rest, State#thrift_socket_server{port=Port});
91parse_options([{ip, Ip} | Rest], State) ->
92 ParsedIp = case Ip of
David Reiss1a2f2182008-06-11 01:14:01 +000093 any ->
94 any;
95 Ip when is_tuple(Ip) ->
96 Ip;
97 Ip when is_list(Ip) ->
98 {ok, IpTuple} = inet_parse:address(Ip),
99 IpTuple
100 end,
David Reiss80862312008-06-11 00:59:55 +0000101 parse_options(Rest, State#thrift_socket_server{ip=ParsedIp});
David Reissb7c88022008-06-11 01:00:20 +0000102parse_options([{socket_opts, L} | Rest], State) when is_list(L), length(L) > 0 ->
103 parse_options(Rest, State#thrift_socket_server{socket_opts=L});
David Robakowskiae971ce2013-08-02 12:16:00 +0200104
105parse_options([{handler, []} | _Rest], _State) ->
106 throw("At least an error handler must be defined.");
107parse_options([{handler, ServiceHandlerPropertyList} | Rest], State) when is_list(ServiceHandlerPropertyList) ->
108 ServiceHandlerMap =
109 case State#thrift_socket_server.handler of
110 undefined ->
111 lists:foldl(
112 fun ({ServiceName, ServiceHandler}, Acc) when is_list(ServiceName), is_atom(ServiceHandler) ->
113 thrift_multiplexed_map_wrapper:store(ServiceName, ServiceHandler, Acc);
114 (_, _Acc) ->
115 throw("The handler option is not properly configured for multiplexed services. It should be a kind of [{\"error_handler\", Module::atom()}, {SericeName::list(), Module::atom()}, ...]")
116 end, thrift_multiplexed_map_wrapper:new(), ServiceHandlerPropertyList);
117 _ -> throw("Error while parsing the handler option.")
118 end,
119 case thrift_multiplexed_map_wrapper:find(?MULTIPLEXED_ERROR_HANDLER_KEY, ServiceHandlerMap) of
120 {ok, _ErrorHandler} -> parse_options(Rest, State#thrift_socket_server{handler=ServiceHandlerMap});
121 error -> throw("The handler option is not properly configured for multiplexed services. It should be a kind of [{\"error_handler\", Module::atom()}, {SericeName::list(), Module::atom()}, ...]")
122 end;
123parse_options([{handler, Handler} | Rest], State) when State#thrift_socket_server.handler == undefined, is_atom(Handler) ->
David Reiss80862312008-06-11 00:59:55 +0000124 parse_options(Rest, State#thrift_socket_server{handler=Handler});
David Robakowskiae971ce2013-08-02 12:16:00 +0200125
126parse_options([{service, []} | _Rest], _State) ->
127 throw("At least one service module must be defined.");
128parse_options([{service, ServiceModulePropertyList} | Rest], State) when is_list(ServiceModulePropertyList) ->
129 ServiceModuleMap =
130 case State#thrift_socket_server.service of
131 undefined ->
132 lists:foldl(
133 fun ({ServiceName, ServiceModule}, Acc) when is_list(ServiceName), is_atom(ServiceModule) ->
134 thrift_multiplexed_map_wrapper:store(ServiceName, ServiceModule, Acc);
135 (_, _Acc) ->
136 throw("The service option is not properly configured for multiplexed services. It should be a kind of [{SericeName::list(), ServiceModule::atom()}, ...]")
137 end, thrift_multiplexed_map_wrapper:new(), ServiceModulePropertyList);
138 _ -> throw("Error while parsing the service option.")
139 end,
140 parse_options(Rest, State#thrift_socket_server{service=ServiceModuleMap});
141parse_options([{service, Service} | Rest], State) when State#thrift_socket_server.service == undefined, is_atom(Service) ->
David Reiss80862312008-06-11 00:59:55 +0000142 parse_options(Rest, State#thrift_socket_server{service=Service});
David Robakowskiae971ce2013-08-02 12:16:00 +0200143
David Reiss80862312008-06-11 00:59:55 +0000144parse_options([{max, Max} | Rest], State) ->
145 MaxInt = case Max of
David Reiss1a2f2182008-06-11 01:14:01 +0000146 Max when is_list(Max) ->
147 list_to_integer(Max);
148 Max when is_integer(Max) ->
149 Max
150 end,
David Reissb42361c2009-09-09 17:18:57 +0000151 parse_options(Rest, State#thrift_socket_server{max=MaxInt});
David Robakowskia7d6a972013-08-07 05:51:00 +0200152
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900153parse_options([{protocol, Proto} | Rest], State) when is_atom(Proto) ->
154 parse_options(Rest, State#thrift_socket_server{protocol=Proto});
155
David Reissb42361c2009-09-09 17:18:57 +0000156parse_options([{framed, Framed} | Rest], State) when is_boolean(Framed) ->
David Robakowskia7d6a972013-08-07 05:51:00 +0200157 parse_options(Rest, State#thrift_socket_server{framed=Framed});
158
159parse_options([{ssltransport, SSLTransport} | Rest], State) when is_boolean(SSLTransport) ->
160 parse_options(Rest, State#thrift_socket_server{ssltransport=SSLTransport});
161parse_options([{ssloptions, SSLOptions} | Rest], State) when is_list(SSLOptions) ->
162 parse_options(Rest, State#thrift_socket_server{ssloptions=SSLOptions}).
David Reiss80862312008-06-11 00:59:55 +0000163
164start_server(State=#thrift_socket_server{name=Name}) ->
David Reiss80862312008-06-11 00:59:55 +0000165 case Name of
David Reiss1a2f2182008-06-11 01:14:01 +0000166 undefined ->
167 gen_server:start_link(?MODULE, State, []);
168 _ ->
169 gen_server:start_link(Name, ?MODULE, State, [])
David Reiss80862312008-06-11 00:59:55 +0000170 end.
171
172init(State=#thrift_socket_server{ip=Ip, port=Port}) ->
David Reissd74b0232008-06-11 01:02:55 +0000173 process_flag(trap_exit, true),
David Reiss80862312008-06-11 00:59:55 +0000174 BaseOpts = [binary,
David Reiss1a2f2182008-06-11 01:14:01 +0000175 {reuseaddr, true},
176 {packet, 0},
177 {backlog, 4096},
178 {recbuf, 8192},
179 {active, false}],
David Reiss80862312008-06-11 00:59:55 +0000180 Opts = case Ip of
David Reiss1a2f2182008-06-11 01:14:01 +0000181 any ->
David Reiss80862312008-06-11 00:59:55 +0000182 BaseOpts;
David Reiss1a2f2182008-06-11 01:14:01 +0000183 Ip ->
184 [{ip, Ip} | BaseOpts]
185 end,
David Reiss80862312008-06-11 00:59:55 +0000186 case gen_tcp_listen(Port, Opts, State) of
187 {stop, eacces} ->
188 %% fdsrv module allows another shot to bind
189 %% ports which require root access
190 case Port < 1024 of
191 true ->
192 case fdsrv:start() of
193 {ok, _} ->
194 case fdsrv:bind_socket(tcp, Port) of
195 {ok, Fd} ->
196 gen_tcp_listen(Port, [{fd, Fd} | Opts], State);
197 _ ->
198 {stop, fdsrv_bind_failed}
199 end;
200 _ ->
201 {stop, fdsrv_start_failed}
202 end;
203 false ->
204 {stop, eacces}
205 end;
206 Other ->
207 error_logger:info_msg("thrift service listening on port ~p", [Port]),
208 Other
209 end.
210
211gen_tcp_listen(Port, Opts, State) ->
212 case gen_tcp:listen(Port, Opts) of
213 {ok, Listen} ->
David Reiss1a2f2182008-06-11 01:14:01 +0000214 {ok, ListenPort} = inet:port(Listen),
215 {ok, new_acceptor(State#thrift_socket_server{listen=Listen,
David Reiss80862312008-06-11 00:59:55 +0000216 port=ListenPort})};
David Reiss1a2f2182008-06-11 01:14:01 +0000217 {error, Reason} ->
218 {stop, Reason}
David Reiss80862312008-06-11 00:59:55 +0000219 end.
220
221new_acceptor(State=#thrift_socket_server{max=0}) ->
222 error_logger:error_msg("Not accepting new connections"),
223 State#thrift_socket_server{acceptor=null};
David Reiss5ed313d2010-08-30 22:05:57 +0000224new_acceptor(State=#thrift_socket_server{listen=Listen,
David Reissb7c88022008-06-11 01:00:20 +0000225 service=Service, handler=Handler,
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900226 socket_opts=Opts, framed=Framed, protocol=Proto,
David Robakowskia7d6a972013-08-07 05:51:00 +0200227 ssltransport=SslTransport, ssloptions=SslOptions
David Reissb7c88022008-06-11 01:00:20 +0000228 }) ->
David Reiss80862312008-06-11 00:59:55 +0000229 Pid = proc_lib:spawn_link(?MODULE, acceptor_loop,
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900230 [{self(), Listen, Service, Handler, Opts, Framed, SslTransport, SslOptions, Proto}]),
David Reiss80862312008-06-11 00:59:55 +0000231 State#thrift_socket_server{acceptor=Pid}.
232
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900233acceptor_loop({Server, Listen, Service, Handler, SocketOpts, Framed, SslTransport, SslOptions, Proto})
David Reissb7c88022008-06-11 01:00:20 +0000234 when is_pid(Server), is_list(SocketOpts) ->
David Reissd74b0232008-06-11 01:02:55 +0000235 case catch gen_tcp:accept(Listen) of % infinite timeout
David Reiss1a2f2182008-06-11 01:14:01 +0000236 {ok, Socket} ->
237 gen_server:cast(Server, {accepted, self()}),
David Reiss80862312008-06-11 00:59:55 +0000238 ProtoGen = fun() ->
David Robakowskia7d6a972013-08-07 05:51:00 +0200239 {ok, SocketTransport} = case SslTransport of
240 true -> thrift_sslsocket_transport:new(Socket, SocketOpts, SslOptions);
241 false -> thrift_socket_transport:new(Socket, SocketOpts)
242 end,
243 {ok, Transport} = case Framed of
244 true -> thrift_framed_transport:new(SocketTransport);
245 false -> thrift_buffered_transport:new(SocketTransport)
246 end,
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900247 {ok, Protocol} = case Proto of
248 compact -> thrift_compact_protocol:new(Transport);
249 json -> thrift_json_protocol:new(Transport);
250 _ -> thrift_binary_protocol:new(Transport)
251 end,
David Reiss035979f2010-08-30 22:05:38 +0000252 {ok, Protocol}
David Reiss80862312008-06-11 00:59:55 +0000253 end,
254 thrift_processor:init({Server, ProtoGen, Service, Handler});
David Reiss1a2f2182008-06-11 01:14:01 +0000255 {error, closed} ->
256 exit({error, closed});
257 Other ->
258 error_logger:error_report(
259 [{application, thrift},
260 "Accept failed error",
261 lists:flatten(io_lib:format("~p", [Other]))]),
262 exit({error, accept_failed})
David Reiss80862312008-06-11 00:59:55 +0000263 end.
264
265handle_call({get, port}, _From, State=#thrift_socket_server{port=Port}) ->
266 {reply, Port, State};
267handle_call(_Message, _From, State) ->
268 Res = error,
269 {reply, Res, State}.
270
271handle_cast({accepted, Pid},
David Reiss1a2f2182008-06-11 01:14:01 +0000272 State=#thrift_socket_server{acceptor=Pid, max=Max}) ->
David Reiss80862312008-06-11 00:59:55 +0000273 % io:format("accepted ~p~n", [Pid]),
274 State1 = State#thrift_socket_server{max=Max - 1},
275 {noreply, new_acceptor(State1)};
276handle_cast(stop, State) ->
277 {stop, normal, State}.
278
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900279terminate(Reason, #thrift_socket_server{listen=Listen, port=Port}) ->
David Reiss80862312008-06-11 00:59:55 +0000280 gen_tcp:close(Listen),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900281 {backtrace, Bt} = erlang:process_info(self(), backtrace),
282 error_logger:error_report({?MODULE, ?LINE,
283 {child_error, Reason, Bt}}),
David Reiss80862312008-06-11 00:59:55 +0000284 case Port < 1024 of
285 true ->
286 catch fdsrv:stop(),
287 ok;
288 false ->
289 ok
290 end.
291
292code_change(_OldVsn, State, _Extra) ->
293 State.
294
295handle_info({'EXIT', Pid, normal},
David Reiss1a2f2182008-06-11 01:14:01 +0000296 State=#thrift_socket_server{acceptor=Pid}) ->
David Reiss80862312008-06-11 00:59:55 +0000297 {noreply, new_acceptor(State)};
298handle_info({'EXIT', Pid, Reason},
David Reiss1a2f2182008-06-11 01:14:01 +0000299 State=#thrift_socket_server{acceptor=Pid}) ->
David Reiss80862312008-06-11 00:59:55 +0000300 error_logger:error_report({?MODULE, ?LINE,
David Reiss1a2f2182008-06-11 01:14:01 +0000301 {acceptor_error, Reason}}),
David Reiss80862312008-06-11 00:59:55 +0000302 timer:sleep(100),
303 {noreply, new_acceptor(State)};
304handle_info({'EXIT', _LoopPid, Reason},
David Reiss1a2f2182008-06-11 01:14:01 +0000305 State=#thrift_socket_server{acceptor=Pid, max=Max}) ->
David Reiss80862312008-06-11 00:59:55 +0000306 case Reason of
David Reiss1a2f2182008-06-11 01:14:01 +0000307 normal -> ok;
David Reiss4cf5a6a2008-06-11 01:00:59 +0000308 shutdown -> ok;
David Reiss1a2f2182008-06-11 01:14:01 +0000309 _ -> error_logger:error_report({?MODULE, ?LINE,
David Reiss80862312008-06-11 00:59:55 +0000310 {child_error, Reason, erlang:get_stacktrace()}})
311 end,
312 State1 = State#thrift_socket_server{max=Max + 1},
313 State2 = case Pid of
David Reiss1a2f2182008-06-11 01:14:01 +0000314 null -> new_acceptor(State1);
315 _ -> State1
316 end,
David Reiss80862312008-06-11 00:59:55 +0000317 {noreply, State2};
318handle_info(Info, State) ->
319 error_logger:info_report([{'INFO', Info}, {'State', State}]),
320 {noreply, State}.