ranch_server.erl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. %% Copyright (c) 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. %% @private
  15. -module(ranch_server).
  16. -behaviour(gen_server).
  17. %% API.
  18. -export([start_link/0]).
  19. -export([set_new_listener_opts/3]).
  20. -export([cleanup_listener_opts/1]).
  21. -export([set_connections_sup/2]).
  22. -export([get_connections_sup/1]).
  23. -export([set_port/2]).
  24. -export([get_port/1]).
  25. -export([set_max_connections/2]).
  26. -export([get_max_connections/1]).
  27. -export([set_protocol_options/2]).
  28. -export([get_protocol_options/1]).
  29. -export([count_connections/1]).
  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]).
  37. -define(TAB, ?MODULE).
  38. -type monitors() :: [{{reference(), pid()}, any()}].
  39. -record(state, {
  40. monitors = [] :: monitors()
  41. }).
  42. %% API.
  43. %% @doc Start the ranch_server gen_server.
  44. -spec start_link() -> {ok, pid()}.
  45. start_link() ->
  46. gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  47. %% @private
  48. -spec set_new_listener_opts(any(), ranch:max_conns(), any()) -> ok.
  49. set_new_listener_opts(Ref, MaxConns, Opts) ->
  50. gen_server:call(?MODULE, {set_new_listener_opts, Ref, MaxConns, Opts}).
  51. %% @doc Cleanup listener options after it has been stopped.
  52. -spec cleanup_listener_opts(any()) -> ok.
  53. cleanup_listener_opts(Ref) ->
  54. _ = ets:delete(?TAB, {port, Ref}),
  55. _ = ets:delete(?TAB, {max_conns, Ref}),
  56. _ = ets:delete(?TAB, {opts, Ref}),
  57. ok.
  58. %% @doc Set a connection supervisor associated with specific listener.
  59. -spec set_connections_sup(any(), pid()) -> ok.
  60. set_connections_sup(Ref, Pid) ->
  61. true = gen_server:call(?MODULE, {set_connections_sup, Ref, Pid}),
  62. ok.
  63. %% @doc Return the connection supervisor used by specific listener.
  64. -spec get_connections_sup(any()) -> pid().
  65. get_connections_sup(Ref) ->
  66. ets:lookup_element(?TAB, {conns_sup, Ref}, 2).
  67. %% @private
  68. -spec set_port(any(), inet:port_number()) -> ok.
  69. set_port(Ref, Port) ->
  70. gen_server:call(?MODULE, {set_port, Ref, Port}).
  71. %% @doc Return the listener's port.
  72. -spec get_port(any()) -> inet:port_number().
  73. get_port(Ref) ->
  74. ets:lookup_element(?TAB, {port, Ref}, 2).
  75. %% @doc Set the max number of connections allowed concurrently.
  76. -spec set_max_connections(any(), ranch:max_conns()) -> ok.
  77. set_max_connections(Ref, MaxConnections) ->
  78. gen_server:call(?MODULE, {set_max_conns, Ref, MaxConnections}).
  79. %% @doc Return the max number of connections allowed concurrently.
  80. -spec get_max_connections(any()) -> ranch:max_conns().
  81. get_max_connections(Ref) ->
  82. ets:lookup_element(?TAB, {max_conns, Ref}, 2).
  83. %% @doc Upgrade the protocol options.
  84. -spec set_protocol_options(any(), any()) -> ok.
  85. set_protocol_options(Ref, ProtoOpts) ->
  86. gen_server:call(?MODULE, {set_opts, Ref, ProtoOpts}).
  87. %% @doc Return the current protocol options.
  88. -spec get_protocol_options(any()) -> any().
  89. get_protocol_options(Ref) ->
  90. ets:lookup_element(?TAB, {opts, Ref}, 2).
  91. %% @doc Count the number of connections in the connection pool.
  92. -spec count_connections(any()) -> non_neg_integer().
  93. count_connections(Ref) ->
  94. ranch_conns_sup:active_connections(get_connections_sup(Ref)).
  95. %% gen_server.
  96. %% @private
  97. init([]) ->
  98. Monitors = [{{erlang:monitor(process, Pid), Pid}, Ref} ||
  99. [Ref, Pid] <- ets:match(?TAB, {{conns_sup, '$1'}, '$2'})],
  100. {ok, #state{monitors=Monitors}}.
  101. %% @private
  102. handle_call({set_new_listener_opts, Ref, MaxConns, Opts}, _, State) ->
  103. ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
  104. ets:insert(?TAB, {{opts, Ref}, Opts}),
  105. {reply, ok, State};
  106. handle_call({set_connections_sup, Ref, Pid}, _,
  107. State=#state{monitors=Monitors}) ->
  108. case ets:insert_new(?TAB, {{conns_sup, Ref}, Pid}) of
  109. true ->
  110. MonitorRef = erlang:monitor(process, Pid),
  111. {reply, true,
  112. State#state{monitors=[{{MonitorRef, Pid}, Ref}|Monitors]}};
  113. false ->
  114. {reply, false, State}
  115. end;
  116. handle_call({set_port, Ref, Port}, _, State) ->
  117. true = ets:insert(?TAB, {{port, Ref}, Port}),
  118. {reply, ok, State};
  119. handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
  120. ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
  121. ConnsSup = get_connections_sup(Ref),
  122. ConnsSup ! {set_max_conns, MaxConns},
  123. {reply, ok, State};
  124. handle_call({set_opts, Ref, Opts}, _, State) ->
  125. ets:insert(?TAB, {{opts, Ref}, Opts}),
  126. ConnsSup = get_connections_sup(Ref),
  127. ConnsSup ! {set_opts, Opts},
  128. {reply, ok, State};
  129. handle_call(_Request, _From, State) ->
  130. {reply, ignore, State}.
  131. %% @private
  132. handle_cast(_Request, State) ->
  133. {noreply, State}.
  134. %% @private
  135. handle_info({'DOWN', MonitorRef, process, Pid, _},
  136. State=#state{monitors=Monitors}) ->
  137. {_, Ref} = lists:keyfind({MonitorRef, Pid}, 1, Monitors),
  138. true = ets:delete(?TAB, {conns_sup, Ref}),
  139. Monitors2 = lists:keydelete({MonitorRef, Pid}, 1, Monitors),
  140. {noreply, State#state{monitors=Monitors2}};
  141. handle_info(_Info, State) ->
  142. {noreply, State}.
  143. %% @private
  144. terminate(_Reason, _State) ->
  145. ok.
  146. %% @private
  147. code_change(_OldVsn, State, _Extra) ->
  148. {ok, State}.