ranch_server.erl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. %% Copyright (c) 2012-2017, 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. -module(ranch_server).
  15. -behaviour(gen_server).
  16. %% API.
  17. -export([start_link/0]).
  18. -export([set_new_listener_opts/3]).
  19. -export([cleanup_listener_opts/1]).
  20. -export([set_connections_sup/2]).
  21. -export([get_connections_sup/1]).
  22. -export([set_addr/2]).
  23. -export([get_addr/1]).
  24. -export([set_max_connections/2]).
  25. -export([get_max_connections/1]).
  26. -export([set_protocol_options/2]).
  27. -export([get_protocol_options/1]).
  28. -export([count_connections/1]).
  29. %% gen_server.
  30. -export([init/1]).
  31. -export([handle_call/3]).
  32. -export([handle_cast/2]).
  33. -export([handle_info/2]).
  34. -export([terminate/2]).
  35. -export([code_change/3]).
  36. -define(TAB, ?MODULE).
  37. -type monitors() :: [{{reference(), pid()}, any()}].
  38. -record(state, {
  39. monitors = [] :: monitors()
  40. }).
  41. %% API.
  42. -spec start_link() -> {ok, pid()}.
  43. start_link() ->
  44. gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  45. -spec set_new_listener_opts(ranch:ref(), ranch:max_conns(), any()) -> ok.
  46. set_new_listener_opts(Ref, MaxConns, Opts) ->
  47. gen_server:call(?MODULE, {set_new_listener_opts, Ref, MaxConns, Opts}).
  48. -spec cleanup_listener_opts(ranch:ref()) -> ok.
  49. cleanup_listener_opts(Ref) ->
  50. _ = ets:delete(?TAB, {addr, Ref}),
  51. _ = ets:delete(?TAB, {max_conns, Ref}),
  52. _ = ets:delete(?TAB, {opts, Ref}),
  53. %% We also remove the pid of the connections supervisor.
  54. %% Depending on the timing, it might already have been deleted
  55. %% when we handled the monitor DOWN message. However, in some
  56. %% cases when calling stop_listener followed by get_connections_sup,
  57. %% we could end up with the pid still being returned, when we
  58. %% expected a crash (because the listener was stopped).
  59. %% Deleting it explictly here removes any possible confusion.
  60. _ = ets:delete(?TAB, {conns_sup, Ref}),
  61. ok.
  62. -spec set_connections_sup(ranch:ref(), pid()) -> ok.
  63. set_connections_sup(Ref, Pid) ->
  64. true = gen_server:call(?MODULE, {set_connections_sup, Ref, Pid}),
  65. ok.
  66. -spec get_connections_sup(ranch:ref()) -> pid().
  67. get_connections_sup(Ref) ->
  68. ets:lookup_element(?TAB, {conns_sup, Ref}, 2).
  69. -spec set_addr(ranch:ref(), {inet:ip_address(), inet:port_number()}) -> ok.
  70. set_addr(Ref, Addr) ->
  71. gen_server:call(?MODULE, {set_addr, Ref, Addr}).
  72. -spec get_addr(ranch:ref()) -> {inet:ip_address(), inet:port_number()}.
  73. get_addr(Ref) ->
  74. ets:lookup_element(?TAB, {addr, Ref}, 2).
  75. -spec set_max_connections(ranch:ref(), ranch:max_conns()) -> ok.
  76. set_max_connections(Ref, MaxConnections) ->
  77. gen_server:call(?MODULE, {set_max_conns, Ref, MaxConnections}).
  78. -spec get_max_connections(ranch:ref()) -> ranch:max_conns().
  79. get_max_connections(Ref) ->
  80. ets:lookup_element(?TAB, {max_conns, Ref}, 2).
  81. -spec set_protocol_options(ranch:ref(), any()) -> ok.
  82. set_protocol_options(Ref, ProtoOpts) ->
  83. gen_server:call(?MODULE, {set_opts, Ref, ProtoOpts}).
  84. -spec get_protocol_options(ranch:ref()) -> any().
  85. get_protocol_options(Ref) ->
  86. ets:lookup_element(?TAB, {opts, Ref}, 2).
  87. -spec count_connections(ranch:ref()) -> non_neg_integer().
  88. count_connections(Ref) ->
  89. ranch_conns_sup:active_connections(get_connections_sup(Ref)).
  90. %% gen_server.
  91. init([]) ->
  92. Monitors = [{{erlang:monitor(process, Pid), Pid}, Ref} ||
  93. [Ref, Pid] <- ets:match(?TAB, {{conns_sup, '$1'}, '$2'})],
  94. {ok, #state{monitors=Monitors}}.
  95. handle_call({set_new_listener_opts, Ref, MaxConns, Opts}, _, State) ->
  96. ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
  97. ets:insert(?TAB, {{opts, Ref}, Opts}),
  98. {reply, ok, State};
  99. handle_call({set_connections_sup, Ref, Pid}, _,
  100. State=#state{monitors=Monitors}) ->
  101. case ets:insert_new(?TAB, {{conns_sup, Ref}, Pid}) of
  102. true ->
  103. MonitorRef = erlang:monitor(process, Pid),
  104. {reply, true,
  105. State#state{monitors=[{{MonitorRef, Pid}, Ref}|Monitors]}};
  106. false ->
  107. {reply, false, State}
  108. end;
  109. handle_call({set_addr, Ref, Addr}, _, State) ->
  110. true = ets:insert(?TAB, {{addr, Ref}, Addr}),
  111. {reply, ok, State};
  112. handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
  113. ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
  114. ConnsSup = get_connections_sup(Ref),
  115. ConnsSup ! {set_max_conns, MaxConns},
  116. {reply, ok, State};
  117. handle_call({set_opts, Ref, Opts}, _, State) ->
  118. ets:insert(?TAB, {{opts, Ref}, Opts}),
  119. ConnsSup = get_connections_sup(Ref),
  120. ConnsSup ! {set_opts, Opts},
  121. {reply, ok, State};
  122. handle_call(_Request, _From, State) ->
  123. {reply, ignore, State}.
  124. handle_cast(_Request, State) ->
  125. {noreply, State}.
  126. handle_info({'DOWN', MonitorRef, process, Pid, _},
  127. State=#state{monitors=Monitors}) ->
  128. {_, Ref} = lists:keyfind({MonitorRef, Pid}, 1, Monitors),
  129. _ = ets:delete(?TAB, {conns_sup, Ref}),
  130. Monitors2 = lists:keydelete({MonitorRef, Pid}, 1, Monitors),
  131. {noreply, State#state{monitors=Monitors2}};
  132. handle_info(_Info, State) ->
  133. {noreply, State}.
  134. terminate(_Reason, _State) ->
  135. ok.
  136. code_change(_OldVsn, State, _Extra) ->
  137. {ok, State}.