ranch_server.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. %% Copyright (c) 2012-2015, 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_port/2]).
  23. -export([get_port/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, {port, Ref}),
  51. _ = ets:delete(?TAB, {max_conns, Ref}),
  52. _ = ets:delete(?TAB, {opts, Ref}),
  53. ok.
  54. -spec set_connections_sup(ranch:ref(), pid()) -> ok.
  55. set_connections_sup(Ref, Pid) ->
  56. true = gen_server:call(?MODULE, {set_connections_sup, Ref, Pid}),
  57. ok.
  58. -spec get_connections_sup(ranch:ref()) -> pid().
  59. get_connections_sup(Ref) ->
  60. ets:lookup_element(?TAB, {conns_sup, Ref}, 2).
  61. -spec set_port(ranch:ref(), inet:port_number()) -> ok.
  62. set_port(Ref, Port) ->
  63. gen_server:call(?MODULE, {set_port, Ref, Port}).
  64. -spec get_port(ranch:ref()) -> inet:port_number().
  65. get_port(Ref) ->
  66. ets:lookup_element(?TAB, {port, Ref}, 2).
  67. -spec set_max_connections(ranch:ref(), ranch:max_conns()) -> ok.
  68. set_max_connections(Ref, MaxConnections) ->
  69. gen_server:call(?MODULE, {set_max_conns, Ref, MaxConnections}).
  70. -spec get_max_connections(ranch:ref()) -> ranch:max_conns().
  71. get_max_connections(Ref) ->
  72. ets:lookup_element(?TAB, {max_conns, Ref}, 2).
  73. -spec set_protocol_options(ranch:ref(), any()) -> ok.
  74. set_protocol_options(Ref, ProtoOpts) ->
  75. gen_server:call(?MODULE, {set_opts, Ref, ProtoOpts}).
  76. -spec get_protocol_options(ranch:ref()) -> any().
  77. get_protocol_options(Ref) ->
  78. ets:lookup_element(?TAB, {opts, Ref}, 2).
  79. -spec count_connections(ranch:ref()) -> non_neg_integer().
  80. count_connections(Ref) ->
  81. ranch_conns_sup:active_connections(get_connections_sup(Ref)).
  82. %% gen_server.
  83. init([]) ->
  84. Monitors = [{{erlang:monitor(process, Pid), Pid}, Ref} ||
  85. [Ref, Pid] <- ets:match(?TAB, {{conns_sup, '$1'}, '$2'})],
  86. {ok, #state{monitors=Monitors}}.
  87. handle_call({set_new_listener_opts, Ref, MaxConns, Opts}, _, State) ->
  88. ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
  89. ets:insert(?TAB, {{opts, Ref}, Opts}),
  90. {reply, ok, State};
  91. handle_call({set_connections_sup, Ref, Pid}, _,
  92. State=#state{monitors=Monitors}) ->
  93. case ets:insert_new(?TAB, {{conns_sup, Ref}, Pid}) of
  94. true ->
  95. MonitorRef = erlang:monitor(process, Pid),
  96. {reply, true,
  97. State#state{monitors=[{{MonitorRef, Pid}, Ref}|Monitors]}};
  98. false ->
  99. {reply, false, State}
  100. end;
  101. handle_call({set_port, Ref, Port}, _, State) ->
  102. true = ets:insert(?TAB, {{port, Ref}, Port}),
  103. {reply, ok, State};
  104. handle_call({set_max_conns, Ref, MaxConns}, _, State) ->
  105. ets:insert(?TAB, {{max_conns, Ref}, MaxConns}),
  106. ConnsSup = get_connections_sup(Ref),
  107. ConnsSup ! {set_max_conns, MaxConns},
  108. {reply, ok, State};
  109. handle_call({set_opts, Ref, Opts}, _, State) ->
  110. ets:insert(?TAB, {{opts, Ref}, Opts}),
  111. ConnsSup = get_connections_sup(Ref),
  112. ConnsSup ! {set_opts, Opts},
  113. {reply, ok, State};
  114. handle_call(_Request, _From, State) ->
  115. {reply, ignore, State}.
  116. handle_cast(_Request, State) ->
  117. {noreply, State}.
  118. handle_info({'DOWN', MonitorRef, process, Pid, _},
  119. State=#state{monitors=Monitors}) ->
  120. {_, Ref} = lists:keyfind({MonitorRef, Pid}, 1, Monitors),
  121. true = ets:delete(?TAB, {conns_sup, Ref}),
  122. Monitors2 = lists:keydelete({MonitorRef, Pid}, 1, Monitors),
  123. {noreply, State#state{monitors=Monitors2}};
  124. handle_info(_Info, State) ->
  125. {noreply, State}.
  126. terminate(_Reason, _State) ->
  127. ok.
  128. code_change(_OldVsn, State, _Extra) ->
  129. {ok, State}.