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 | %% |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 19 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 20 | -module(thrift_socket_server). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 21 | |
| 22 | -behaviour(gen_server). |
| 23 | |
| 24 | -export([start/1, stop/1]). |
| 25 | |
| 26 | -export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 27 | handle_info/2]). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 28 | |
| 29 | -export([acceptor_loop/1]). |
| 30 | |
| 31 | -record(thrift_socket_server, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 32 | {port, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 33 | service, |
| 34 | handler, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 35 | name, |
| 36 | max=2048, |
| 37 | ip=any, |
| 38 | listen=null, |
| 39 | acceptor=null, |
David Reiss | b42361c | 2009-09-09 17:18:57 +0000 | [diff] [blame] | 40 | socket_opts=[{recv_timeout, 500}], |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 41 | protocol=binary, |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 42 | framed=false, |
| 43 | ssltransport=false, |
| 44 | ssloptions=[] |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 45 | }). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 46 | |
| 47 | start(State=#thrift_socket_server{}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 48 | start_server(State); |
| 49 | start(Options) -> |
| 50 | start(parse_options(Options)). |
| 51 | |
| 52 | stop(Name) when is_atom(Name) -> |
| 53 | gen_server:cast(Name, stop); |
| 54 | stop(Pid) when is_pid(Pid) -> |
| 55 | gen_server:cast(Pid, stop); |
| 56 | stop({local, Name}) -> |
| 57 | stop(Name); |
| 58 | stop({global, Name}) -> |
| 59 | stop(Name); |
| 60 | stop(Options) -> |
| 61 | State = parse_options(Options), |
| 62 | stop(State#thrift_socket_server.name). |
| 63 | |
| 64 | %% Internal API |
| 65 | |
| 66 | parse_options(Options) -> |
| 67 | parse_options(Options, #thrift_socket_server{}). |
| 68 | |
| 69 | parse_options([], State) -> |
| 70 | State; |
| 71 | parse_options([{name, L} | Rest], State) when is_list(L) -> |
| 72 | Name = {local, list_to_atom(L)}, |
| 73 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 74 | parse_options([{name, A} | Rest], State) when is_atom(A) -> |
| 75 | Name = {local, A}, |
| 76 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 77 | parse_options([{name, Name} | Rest], State) -> |
| 78 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 79 | parse_options([{port, L} | Rest], State) when is_list(L) -> |
| 80 | Port = list_to_integer(L), |
| 81 | parse_options(Rest, State#thrift_socket_server{port=Port}); |
| 82 | parse_options([{port, Port} | Rest], State) -> |
| 83 | parse_options(Rest, State#thrift_socket_server{port=Port}); |
| 84 | parse_options([{ip, Ip} | Rest], State) -> |
| 85 | ParsedIp = case Ip of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 86 | any -> |
| 87 | any; |
| 88 | Ip when is_tuple(Ip) -> |
| 89 | Ip; |
| 90 | Ip when is_list(Ip) -> |
| 91 | {ok, IpTuple} = inet_parse:address(Ip), |
| 92 | IpTuple |
| 93 | end, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 94 | parse_options(Rest, State#thrift_socket_server{ip=ParsedIp}); |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 95 | parse_options([{socket_opts, L} | Rest], State) when is_list(L), length(L) > 0 -> |
| 96 | parse_options(Rest, State#thrift_socket_server{socket_opts=L}); |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 97 | parse_options([{handler, Handler} | Rest], State) -> |
| 98 | parse_options(Rest, State#thrift_socket_server{handler=Handler}); |
| 99 | parse_options([{service, Service} | Rest], State) -> |
| 100 | parse_options(Rest, State#thrift_socket_server{service=Service}); |
| 101 | parse_options([{max, Max} | Rest], State) -> |
| 102 | MaxInt = case Max of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 103 | Max when is_list(Max) -> |
| 104 | list_to_integer(Max); |
| 105 | Max when is_integer(Max) -> |
| 106 | Max |
| 107 | end, |
David Reiss | b42361c | 2009-09-09 17:18:57 +0000 | [diff] [blame] | 108 | parse_options(Rest, State#thrift_socket_server{max=MaxInt}); |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 109 | |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 110 | parse_options([{protocol, Proto} | Rest], State) when is_atom(Proto) -> |
| 111 | parse_options(Rest, State#thrift_socket_server{protocol=Proto}); |
| 112 | |
David Reiss | b42361c | 2009-09-09 17:18:57 +0000 | [diff] [blame] | 113 | parse_options([{framed, Framed} | Rest], State) when is_boolean(Framed) -> |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 114 | parse_options(Rest, State#thrift_socket_server{framed=Framed}); |
| 115 | |
| 116 | parse_options([{ssltransport, SSLTransport} | Rest], State) when is_boolean(SSLTransport) -> |
| 117 | parse_options(Rest, State#thrift_socket_server{ssltransport=SSLTransport}); |
| 118 | parse_options([{ssloptions, SSLOptions} | Rest], State) when is_list(SSLOptions) -> |
| 119 | parse_options(Rest, State#thrift_socket_server{ssloptions=SSLOptions}). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 120 | |
| 121 | start_server(State=#thrift_socket_server{name=Name}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 122 | case Name of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 123 | undefined -> |
| 124 | gen_server:start_link(?MODULE, State, []); |
| 125 | _ -> |
| 126 | gen_server:start_link(Name, ?MODULE, State, []) |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 127 | end. |
| 128 | |
| 129 | init(State=#thrift_socket_server{ip=Ip, port=Port}) -> |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame] | 130 | process_flag(trap_exit, true), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 131 | BaseOpts = [binary, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 132 | {reuseaddr, true}, |
| 133 | {packet, 0}, |
| 134 | {backlog, 4096}, |
| 135 | {recbuf, 8192}, |
| 136 | {active, false}], |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 137 | Opts = case Ip of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 138 | any -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 139 | BaseOpts; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 140 | Ip -> |
| 141 | [{ip, Ip} | BaseOpts] |
| 142 | end, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 143 | case gen_tcp_listen(Port, Opts, State) of |
| 144 | {stop, eacces} -> |
| 145 | %% fdsrv module allows another shot to bind |
| 146 | %% ports which require root access |
| 147 | case Port < 1024 of |
| 148 | true -> |
| 149 | case fdsrv:start() of |
| 150 | {ok, _} -> |
| 151 | case fdsrv:bind_socket(tcp, Port) of |
| 152 | {ok, Fd} -> |
| 153 | gen_tcp_listen(Port, [{fd, Fd} | Opts], State); |
| 154 | _ -> |
| 155 | {stop, fdsrv_bind_failed} |
| 156 | end; |
| 157 | _ -> |
| 158 | {stop, fdsrv_start_failed} |
| 159 | end; |
| 160 | false -> |
| 161 | {stop, eacces} |
| 162 | end; |
| 163 | Other -> |
| 164 | error_logger:info_msg("thrift service listening on port ~p", [Port]), |
| 165 | Other |
| 166 | end. |
| 167 | |
| 168 | gen_tcp_listen(Port, Opts, State) -> |
| 169 | case gen_tcp:listen(Port, Opts) of |
| 170 | {ok, Listen} -> |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 171 | {ok, ListenPort} = inet:port(Listen), |
| 172 | {ok, new_acceptor(State#thrift_socket_server{listen=Listen, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 173 | port=ListenPort})}; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 174 | {error, Reason} -> |
| 175 | {stop, Reason} |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 176 | end. |
| 177 | |
| 178 | new_acceptor(State=#thrift_socket_server{max=0}) -> |
| 179 | error_logger:error_msg("Not accepting new connections"), |
| 180 | State#thrift_socket_server{acceptor=null}; |
David Reiss | 5ed313d | 2010-08-30 22:05:57 +0000 | [diff] [blame] | 181 | new_acceptor(State=#thrift_socket_server{listen=Listen, |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 182 | service=Service, handler=Handler, |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 183 | socket_opts=Opts, framed=Framed, protocol=Proto, |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 184 | ssltransport=SslTransport, ssloptions=SslOptions |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 185 | }) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 186 | Pid = proc_lib:spawn_link(?MODULE, acceptor_loop, |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 187 | [{self(), Listen, Service, Handler, Opts, Framed, SslTransport, SslOptions, Proto}]), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 188 | State#thrift_socket_server{acceptor=Pid}. |
| 189 | |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 190 | acceptor_loop({Server, Listen, Service, Handler, SocketOpts, Framed, SslTransport, SslOptions, Proto}) |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 191 | when is_pid(Server), is_list(SocketOpts) -> |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame] | 192 | case catch gen_tcp:accept(Listen) of % infinite timeout |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 193 | {ok, Socket} -> |
| 194 | gen_server:cast(Server, {accepted, self()}), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 195 | ProtoGen = fun() -> |
David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 196 | {ok, SocketTransport} = case SslTransport of |
| 197 | true -> thrift_sslsocket_transport:new(Socket, SocketOpts, SslOptions); |
| 198 | false -> thrift_socket_transport:new(Socket, SocketOpts) |
| 199 | end, |
| 200 | {ok, Transport} = case Framed of |
| 201 | true -> thrift_framed_transport:new(SocketTransport); |
| 202 | false -> thrift_buffered_transport:new(SocketTransport) |
| 203 | end, |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 204 | {ok, Protocol} = case Proto of |
| 205 | compact -> thrift_compact_protocol:new(Transport); |
| 206 | json -> thrift_json_protocol:new(Transport); |
| 207 | _ -> thrift_binary_protocol:new(Transport) |
| 208 | end, |
David Reiss | 035979f | 2010-08-30 22:05:38 +0000 | [diff] [blame] | 209 | {ok, Protocol} |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 210 | end, |
| 211 | thrift_processor:init({Server, ProtoGen, Service, Handler}); |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 212 | {error, closed} -> |
| 213 | exit({error, closed}); |
| 214 | Other -> |
| 215 | error_logger:error_report( |
| 216 | [{application, thrift}, |
| 217 | "Accept failed error", |
| 218 | lists:flatten(io_lib:format("~p", [Other]))]), |
| 219 | exit({error, accept_failed}) |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 220 | end. |
| 221 | |
| 222 | handle_call({get, port}, _From, State=#thrift_socket_server{port=Port}) -> |
| 223 | {reply, Port, State}; |
| 224 | handle_call(_Message, _From, State) -> |
| 225 | Res = error, |
| 226 | {reply, Res, State}. |
| 227 | |
| 228 | handle_cast({accepted, Pid}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 229 | State=#thrift_socket_server{acceptor=Pid, max=Max}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 230 | % io:format("accepted ~p~n", [Pid]), |
| 231 | State1 = State#thrift_socket_server{max=Max - 1}, |
| 232 | {noreply, new_acceptor(State1)}; |
| 233 | handle_cast(stop, State) -> |
| 234 | {stop, normal, State}. |
| 235 | |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 236 | terminate(Reason, #thrift_socket_server{listen=Listen, port=Port}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 237 | gen_tcp:close(Listen), |
Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 238 | {backtrace, Bt} = erlang:process_info(self(), backtrace), |
| 239 | error_logger:error_report({?MODULE, ?LINE, |
| 240 | {child_error, Reason, Bt}}), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 241 | case Port < 1024 of |
| 242 | true -> |
| 243 | catch fdsrv:stop(), |
| 244 | ok; |
| 245 | false -> |
| 246 | ok |
| 247 | end. |
| 248 | |
| 249 | code_change(_OldVsn, State, _Extra) -> |
| 250 | State. |
| 251 | |
| 252 | handle_info({'EXIT', Pid, normal}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 253 | State=#thrift_socket_server{acceptor=Pid}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 254 | {noreply, new_acceptor(State)}; |
| 255 | handle_info({'EXIT', Pid, Reason}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 256 | State=#thrift_socket_server{acceptor=Pid}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 257 | error_logger:error_report({?MODULE, ?LINE, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 258 | {acceptor_error, Reason}}), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 259 | timer:sleep(100), |
| 260 | {noreply, new_acceptor(State)}; |
| 261 | handle_info({'EXIT', _LoopPid, Reason}, |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 262 | State=#thrift_socket_server{acceptor=Pid, max=Max}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 263 | case Reason of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 264 | normal -> ok; |
David Reiss | 4cf5a6a | 2008-06-11 01:00:59 +0000 | [diff] [blame] | 265 | shutdown -> ok; |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 266 | _ -> error_logger:error_report({?MODULE, ?LINE, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 267 | {child_error, Reason, erlang:get_stacktrace()}}) |
| 268 | end, |
| 269 | State1 = State#thrift_socket_server{max=Max + 1}, |
| 270 | State2 = case Pid of |
David Reiss | 1a2f218 | 2008-06-11 01:14:01 +0000 | [diff] [blame] | 271 | null -> new_acceptor(State1); |
| 272 | _ -> State1 |
| 273 | end, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 274 | {noreply, State2}; |
| 275 | handle_info(Info, State) -> |
| 276 | error_logger:info_report([{'INFO', Info}, {'State', State}]), |
| 277 | {noreply, State}. |