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