David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 1 | %%%------------------------------------------------------------------- |
| 2 | %%% File : thrift_socket_server.erl |
| 3 | %%% Author : eugene letuchy <eletuchy@facebook.com> |
| 4 | %%% Description : A rewrite of thrift_server, based quite heavily |
| 5 | %%% on the mochiweb_socket_server module of mochiweb |
| 6 | %%% Created : 3 Mar 2008 by eugene letuchy <eletuchy@facebook.com> |
| 7 | %%%------------------------------------------------------------------- |
| 8 | -module(thrift_socket_server). |
| 9 | |
| 10 | -author('eletuchy@facebook.com'). |
| 11 | -author('todd@lipcon.org'). |
| 12 | |
| 13 | -behaviour(gen_server). |
| 14 | |
| 15 | -export([start/1, stop/1]). |
| 16 | |
| 17 | -export([init/1, handle_call/3, handle_cast/2, terminate/2, code_change/3, |
| 18 | handle_info/2]). |
| 19 | |
| 20 | -export([acceptor_loop/1]). |
| 21 | |
| 22 | -record(thrift_socket_server, |
| 23 | {port, |
| 24 | service, |
| 25 | handler, |
| 26 | name, |
| 27 | max=2048, |
| 28 | ip=any, |
| 29 | listen=null, |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 30 | acceptor=null, |
| 31 | socket_opts=[{recv_timeout, 500}] |
| 32 | }). |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 33 | |
| 34 | start(State=#thrift_socket_server{}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 35 | start_server(State); |
| 36 | start(Options) -> |
| 37 | start(parse_options(Options)). |
| 38 | |
| 39 | stop(Name) when is_atom(Name) -> |
| 40 | gen_server:cast(Name, stop); |
| 41 | stop(Pid) when is_pid(Pid) -> |
| 42 | gen_server:cast(Pid, stop); |
| 43 | stop({local, Name}) -> |
| 44 | stop(Name); |
| 45 | stop({global, Name}) -> |
| 46 | stop(Name); |
| 47 | stop(Options) -> |
| 48 | State = parse_options(Options), |
| 49 | stop(State#thrift_socket_server.name). |
| 50 | |
| 51 | %% Internal API |
| 52 | |
| 53 | parse_options(Options) -> |
| 54 | parse_options(Options, #thrift_socket_server{}). |
| 55 | |
| 56 | parse_options([], State) -> |
| 57 | State; |
| 58 | parse_options([{name, L} | Rest], State) when is_list(L) -> |
| 59 | Name = {local, list_to_atom(L)}, |
| 60 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 61 | parse_options([{name, A} | Rest], State) when is_atom(A) -> |
| 62 | Name = {local, A}, |
| 63 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 64 | parse_options([{name, Name} | Rest], State) -> |
| 65 | parse_options(Rest, State#thrift_socket_server{name=Name}); |
| 66 | parse_options([{port, L} | Rest], State) when is_list(L) -> |
| 67 | Port = list_to_integer(L), |
| 68 | parse_options(Rest, State#thrift_socket_server{port=Port}); |
| 69 | parse_options([{port, Port} | Rest], State) -> |
| 70 | parse_options(Rest, State#thrift_socket_server{port=Port}); |
| 71 | parse_options([{ip, Ip} | Rest], State) -> |
| 72 | ParsedIp = case Ip of |
| 73 | any -> |
| 74 | any; |
| 75 | Ip when is_tuple(Ip) -> |
| 76 | Ip; |
| 77 | Ip when is_list(Ip) -> |
| 78 | {ok, IpTuple} = inet_parse:address(Ip), |
| 79 | IpTuple |
| 80 | end, |
| 81 | parse_options(Rest, State#thrift_socket_server{ip=ParsedIp}); |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 82 | parse_options([{socket_opts, L} | Rest], State) when is_list(L), length(L) > 0 -> |
| 83 | parse_options(Rest, State#thrift_socket_server{socket_opts=L}); |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 84 | parse_options([{handler, Handler} | Rest], State) -> |
| 85 | parse_options(Rest, State#thrift_socket_server{handler=Handler}); |
| 86 | parse_options([{service, Service} | Rest], State) -> |
| 87 | parse_options(Rest, State#thrift_socket_server{service=Service}); |
| 88 | parse_options([{max, Max} | Rest], State) -> |
| 89 | MaxInt = case Max of |
| 90 | Max when is_list(Max) -> |
| 91 | list_to_integer(Max); |
| 92 | Max when is_integer(Max) -> |
| 93 | Max |
| 94 | end, |
| 95 | parse_options(Rest, State#thrift_socket_server{max=MaxInt}). |
| 96 | |
| 97 | start_server(State=#thrift_socket_server{name=Name}) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 98 | case Name of |
| 99 | undefined -> |
| 100 | gen_server:start_link(?MODULE, State, []); |
| 101 | _ -> |
| 102 | gen_server:start_link(Name, ?MODULE, State, []) |
| 103 | end. |
| 104 | |
| 105 | init(State=#thrift_socket_server{ip=Ip, port=Port}) -> |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame^] | 106 | process_flag(trap_exit, true), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 107 | BaseOpts = [binary, |
| 108 | {reuseaddr, true}, |
| 109 | {packet, 0}, |
David Reiss | 2fe905e | 2008-06-11 01:02:23 +0000 | [diff] [blame] | 110 | {backlog, 4096}, |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 111 | {recbuf, 8192}, |
| 112 | {active, false}], |
| 113 | Opts = case Ip of |
| 114 | any -> |
| 115 | BaseOpts; |
| 116 | Ip -> |
| 117 | [{ip, Ip} | BaseOpts] |
| 118 | end, |
| 119 | case gen_tcp_listen(Port, Opts, State) of |
| 120 | {stop, eacces} -> |
| 121 | %% fdsrv module allows another shot to bind |
| 122 | %% ports which require root access |
| 123 | case Port < 1024 of |
| 124 | true -> |
| 125 | case fdsrv:start() of |
| 126 | {ok, _} -> |
| 127 | case fdsrv:bind_socket(tcp, Port) of |
| 128 | {ok, Fd} -> |
| 129 | gen_tcp_listen(Port, [{fd, Fd} | Opts], State); |
| 130 | _ -> |
| 131 | {stop, fdsrv_bind_failed} |
| 132 | end; |
| 133 | _ -> |
| 134 | {stop, fdsrv_start_failed} |
| 135 | end; |
| 136 | false -> |
| 137 | {stop, eacces} |
| 138 | end; |
| 139 | Other -> |
| 140 | error_logger:info_msg("thrift service listening on port ~p", [Port]), |
| 141 | Other |
| 142 | end. |
| 143 | |
| 144 | gen_tcp_listen(Port, Opts, State) -> |
| 145 | case gen_tcp:listen(Port, Opts) of |
| 146 | {ok, Listen} -> |
| 147 | {ok, ListenPort} = inet:port(Listen), |
| 148 | {ok, new_acceptor(State#thrift_socket_server{listen=Listen, |
| 149 | port=ListenPort})}; |
| 150 | {error, Reason} -> |
| 151 | {stop, Reason} |
| 152 | end. |
| 153 | |
| 154 | new_acceptor(State=#thrift_socket_server{max=0}) -> |
| 155 | error_logger:error_msg("Not accepting new connections"), |
| 156 | State#thrift_socket_server{acceptor=null}; |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 157 | new_acceptor(State=#thrift_socket_server{acceptor=OldPid, listen=Listen, |
| 158 | service=Service, handler=Handler, |
| 159 | socket_opts=Opts |
| 160 | }) -> |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 161 | Pid = proc_lib:spawn_link(?MODULE, acceptor_loop, |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 162 | [{self(), Listen, Service, Handler, Opts}]), |
David Reiss | 919a801 | 2008-06-11 01:00:12 +0000 | [diff] [blame] | 163 | %% error_logger:info_msg("Spawning new acceptor: ~p => ~p", [OldPid, Pid]), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 164 | State#thrift_socket_server{acceptor=Pid}. |
| 165 | |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 166 | acceptor_loop({Server, Listen, Service, Handler, SocketOpts}) |
| 167 | when is_pid(Server), is_list(SocketOpts) -> |
David Reiss | d74b023 | 2008-06-11 01:02:55 +0000 | [diff] [blame^] | 168 | case catch gen_tcp:accept(Listen) of % infinite timeout |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 169 | {ok, Socket} -> |
| 170 | gen_server:cast(Server, {accepted, self()}), |
| 171 | ProtoGen = fun() -> |
David Reiss | b7c8802 | 2008-06-11 01:00:20 +0000 | [diff] [blame] | 172 | {ok, SocketTransport} = thrift_socket_transport:new(Socket, SocketOpts), |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 173 | {ok, BufferedTransport} = thrift_buffered_transport:new(SocketTransport), |
| 174 | {ok, Protocol} = thrift_binary_protocol:new(BufferedTransport), |
| 175 | {ok, IProt=Protocol, OProt=Protocol} |
| 176 | end, |
| 177 | thrift_processor:init({Server, ProtoGen, Service, Handler}); |
| 178 | {error, closed} -> |
| 179 | exit({error, closed}); |
| 180 | Other -> |
| 181 | error_logger:error_report( |
| 182 | [{application, thrift}, |
| 183 | "Accept failed error", |
| 184 | lists:flatten(io_lib:format("~p", [Other]))]), |
| 185 | exit({error, accept_failed}) |
| 186 | end. |
| 187 | |
| 188 | handle_call({get, port}, _From, State=#thrift_socket_server{port=Port}) -> |
| 189 | {reply, Port, State}; |
| 190 | handle_call(_Message, _From, State) -> |
| 191 | Res = error, |
| 192 | {reply, Res, State}. |
| 193 | |
| 194 | handle_cast({accepted, Pid}, |
| 195 | State=#thrift_socket_server{acceptor=Pid, max=Max}) -> |
| 196 | % io:format("accepted ~p~n", [Pid]), |
| 197 | State1 = State#thrift_socket_server{max=Max - 1}, |
| 198 | {noreply, new_acceptor(State1)}; |
| 199 | handle_cast(stop, State) -> |
| 200 | {stop, normal, State}. |
| 201 | |
| 202 | terminate(_Reason, #thrift_socket_server{listen=Listen, port=Port}) -> |
| 203 | gen_tcp:close(Listen), |
| 204 | case Port < 1024 of |
| 205 | true -> |
| 206 | catch fdsrv:stop(), |
| 207 | ok; |
| 208 | false -> |
| 209 | ok |
| 210 | end. |
| 211 | |
| 212 | code_change(_OldVsn, State, _Extra) -> |
| 213 | State. |
| 214 | |
| 215 | handle_info({'EXIT', Pid, normal}, |
| 216 | State=#thrift_socket_server{acceptor=Pid}) -> |
| 217 | {noreply, new_acceptor(State)}; |
| 218 | handle_info({'EXIT', Pid, Reason}, |
| 219 | State=#thrift_socket_server{acceptor=Pid}) -> |
| 220 | error_logger:error_report({?MODULE, ?LINE, |
| 221 | {acceptor_error, Reason}}), |
| 222 | timer:sleep(100), |
| 223 | {noreply, new_acceptor(State)}; |
| 224 | handle_info({'EXIT', _LoopPid, Reason}, |
| 225 | State=#thrift_socket_server{acceptor=Pid, max=Max}) -> |
| 226 | case Reason of |
| 227 | normal -> ok; |
David Reiss | 4cf5a6a | 2008-06-11 01:00:59 +0000 | [diff] [blame] | 228 | shutdown -> ok; |
David Reiss | 8086231 | 2008-06-11 00:59:55 +0000 | [diff] [blame] | 229 | _ -> error_logger:error_report({?MODULE, ?LINE, |
| 230 | {child_error, Reason, erlang:get_stacktrace()}}) |
| 231 | end, |
| 232 | State1 = State#thrift_socket_server{max=Max + 1}, |
| 233 | State2 = case Pid of |
| 234 | null -> new_acceptor(State1); |
| 235 | _ -> State1 |
| 236 | end, |
| 237 | {noreply, State2}; |
| 238 | handle_info(Info, State) -> |
| 239 | error_logger:info_report([{'INFO', Info}, {'State', State}]), |
| 240 | {noreply, State}. |