Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 148 lines (126 sloc) 4.91 kb
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
1 %% Copyright (c) 2011-2012, Loïc Hoguin <essen@ninenines.eu>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 %% @doc Public API for managing listeners.
16 -module(ranch_listener).
17 -behaviour(gen_server).
18
46ada7f Add Transport:connect/3 and remove types unneeded by R15B+
Loïc Hoguin authored
19 %% API.
6b354c1 @essen Make accept asynchronous
essen authored
20 -export([start_link/3]).
46ada7f Add Transport:connect/3 and remove types unneeded by R15B+
Loïc Hoguin authored
21 -export([stop/1]).
6b354c1 @essen Make accept asynchronous
essen authored
22 -export([add_connection/2]).
23 -export([remove_connection/1]).
4534817 @essen Add support for listening on random port numbers (port 0)
essen authored
24 -export([get_port/1]).
25 -export([set_port/2]).
6b354c1 @essen Make accept asynchronous
essen authored
26 -export([get_max_connections/1]).
46ada7f Add Transport:connect/3 and remove types unneeded by R15B+
Loïc Hoguin authored
27 -export([get_protocol_options/1]).
28 -export([set_protocol_options/2]).
29
30 %% gen_server.
31 -export([init/1]).
32 -export([handle_call/3]).
33 -export([handle_cast/2]).
34 -export([handle_info/2]).
35 -export([terminate/2]).
36 -export([code_change/3]).
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
37
38 -record(state, {
6b354c1 @essen Make accept asynchronous
essen authored
39 ref :: any(),
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
40 max_conns = undefined :: non_neg_integer(),
4534817 @essen Add support for listening on random port numbers (port 0)
essen authored
41 port = undefined :: undefined | inet:port_number(),
6b354c1 @essen Make accept asynchronous
essen authored
42 proto_opts = undefined :: any(),
43 rm_diff = 0 :: non_neg_integer()
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
44 }).
45
46 %% API.
47
48 %% @private
6b354c1 @essen Make accept asynchronous
essen authored
49 -spec start_link(any(), non_neg_integer(), any()) -> {ok, pid()}.
50 start_link(Ref, MaxConns, ProtoOpts) ->
51 gen_server:start_link(?MODULE, [Ref, MaxConns, ProtoOpts], []).
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
52
53 %% @private
54 -spec stop(pid()) -> stopped.
55 stop(ServerPid) ->
56 gen_server:call(ServerPid, stop).
57
6b354c1 @essen Make accept asynchronous
essen authored
58 %% @doc Add a connection to the listener's pool.
59 -spec add_connection(pid(), pid()) -> non_neg_integer().
60 add_connection(ServerPid, ConnPid) ->
61 ok = gen_server:cast(ServerPid, {add_connection, ConnPid}),
62 ranch_server:add_connection(ServerPid).
63
64 %% @doc Remove this process' connection from the pool.
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
65 %%
6b354c1 @essen Make accept asynchronous
essen authored
66 %% Useful if you have long-lived connections that aren't taking up
67 %% resources and shouldn't be counted in the limited number of running
68 %% connections.
69 -spec remove_connection(pid()) -> non_neg_integer().
70 remove_connection(ServerPid) ->
71 ok = gen_server:cast(ServerPid, remove_connection),
72 ranch_server:remove_connection(ServerPid).
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
73
4534817 @essen Add support for listening on random port numbers (port 0)
essen authored
74 %% @doc Return the listener's port.
75 -spec get_port(pid()) -> {ok, inet:port_number()}.
76 get_port(ServerPid) ->
77 gen_server:call(ServerPid, get_port).
78
79 %% @private
80 -spec set_port(pid(), inet:port_number()) -> ok.
81 set_port(ServerPid, Port) ->
82 gen_server:cast(ServerPid, {set_port, Port}).
83
6b354c1 @essen Make accept asynchronous
essen authored
84 %% @doc Return the max number of connections allowed concurrently.
85 %% @todo Add set_max_connections.
86 -spec get_max_connections(pid()) -> {ok, non_neg_integer()}.
87 get_max_connections(ServerPid) ->
88 gen_server:call(ServerPid, get_max_connections).
89
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
90 %% @doc Return the current protocol options.
91 -spec get_protocol_options(pid()) -> {ok, any()}.
92 get_protocol_options(ServerPid) ->
93 gen_server:call(ServerPid, get_protocol_options).
94
95 %% @doc Upgrade the protocol options.
96 -spec set_protocol_options(pid(), any()) -> ok.
97 set_protocol_options(ServerPid, ProtoOpts) ->
98 gen_server:call(ServerPid, {set_protocol_options, ProtoOpts}).
99
100 %% gen_server.
101
102 %% @private
6b354c1 @essen Make accept asynchronous
essen authored
103 init([Ref, MaxConns, ProtoOpts]) ->
104 {ok, #state{ref=Ref, max_conns=MaxConns, proto_opts=ProtoOpts}}.
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
105
106 %% @private
4534817 @essen Add support for listening on random port numbers (port 0)
essen authored
107 handle_call(get_port, _From, State=#state{port=Port}) ->
108 {reply, {ok, Port}, State};
6b354c1 @essen Make accept asynchronous
essen authored
109 handle_call(get_max_connections, _From, State=#state{max_conns=MaxConns}) ->
110 {reply, {ok, MaxConns}, State};
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
111 handle_call(get_protocol_options, _From, State=#state{proto_opts=ProtoOpts}) ->
112 {reply, {ok, ProtoOpts}, State};
6b354c1 @essen Make accept asynchronous
essen authored
113 handle_call({set_protocol_options, ProtoOpts}, _From, State=#state{ref=Ref}) ->
114 ranch_server:send_to_acceptors(Ref, {set_opts, ProtoOpts}),
115 {reply, ok, State#state{proto_opts=ProtoOpts}};
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
116 handle_call(stop, _From, State) ->
117 {stop, normal, stopped, State};
118 handle_call(_, _From, State) ->
119 {reply, ignored, State}.
120
121 %% @private
6b354c1 @essen Make accept asynchronous
essen authored
122 handle_cast({add_connection, ConnPid}, State) ->
123 _ = erlang:monitor(process, ConnPid),
124 {noreply, State};
125 handle_cast(remove_connection, State=#state{rm_diff=RmDiff}) ->
126 {noreply, State#state{rm_diff=RmDiff + 1}};
4534817 @essen Add support for listening on random port numbers (port 0)
essen authored
127 handle_cast({set_port, Port}, State) ->
128 {noreply, State#state{port=Port}};
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
129 handle_cast(_Msg, State) ->
130 {noreply, State}.
131
132 %% @private
6b354c1 @essen Make accept asynchronous
essen authored
133 handle_info({'DOWN', _, process, _, _}, State=#state{rm_diff=0}) ->
134 _ = ranch_server:remove_connection(self()),
135 {noreply, State};
136 handle_info({'DOWN', _, process, _, _}, State=#state{rm_diff=RmDiff}) ->
137 {noreply, State#state{rm_diff=RmDiff - 1}};
4156fa3 Import the acceptor code from Cowboy
Loïc Hoguin authored
138 handle_info(_Info, State) ->
139 {noreply, State}.
140
141 %% @private
142 terminate(_Reason, _State) ->
143 ok.
144
145 %% @private
146 code_change(_OldVsn, State, _Extra) ->
147 {ok, State}.
Something went wrong with that request. Please try again.