ranch_server.erl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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([insert_listener/2]).
  20. -export([lookup_listener/1]).
  21. -export([set_connections_sup/2]).
  22. -export([lookup_connections_sup/1]).
  23. -export([add_acceptor/2]).
  24. -export([send_to_acceptors/2]).
  25. -export([add_connection/1]).
  26. -export([count_connections/1]).
  27. -export([remove_connection/1]).
  28. %% gen_server.
  29. -export([init/1]).
  30. -export([handle_call/3]).
  31. -export([handle_cast/2]).
  32. -export([handle_info/2]).
  33. -export([terminate/2]).
  34. -export([code_change/3]).
  35. -define(TAB, ?MODULE).
  36. -type key() :: {listener | acceptors, any()}.
  37. -type monitors() :: [{{reference(), pid()}, key()}].
  38. -record(state, {
  39. monitors = [] :: monitors()
  40. }).
  41. %% API.
  42. %% @doc Start the ranch_server gen_server.
  43. -spec start_link() -> {ok, pid()}.
  44. start_link() ->
  45. gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  46. %% @doc Insert a listener into the database.
  47. -spec insert_listener(any(), pid()) -> ok.
  48. insert_listener(Ref, Pid) ->
  49. true = ets:insert_new(?TAB, {{listener, Ref}, Pid, undefined}),
  50. gen_server:cast(?MODULE, {insert_listener, Ref, Pid}).
  51. %% @doc Lookup a listener in the database.
  52. -spec lookup_listener(any()) -> pid().
  53. lookup_listener(Ref) ->
  54. ets:lookup_element(?TAB, {listener, Ref}, 2).
  55. %% @doc Set a connection supervisor associated with specific listener.
  56. -spec set_connections_sup(any(), pid()) -> ok.
  57. set_connections_sup(Ref, Pid) ->
  58. true = ets:update_element(?TAB, {listener, Ref}, {3, Pid}),
  59. ok.
  60. %% @doc Lookup a connection supervisor used by specific listener.
  61. -spec lookup_connections_sup(any()) -> pid() | undefined.
  62. lookup_connections_sup(Ref) ->
  63. ets:lookup_element(?TAB, {listener, Ref}, 3).
  64. %% @doc Add an acceptor for the given listener.
  65. -spec add_acceptor(any(), pid()) -> ok.
  66. add_acceptor(Ref, Pid) ->
  67. gen_server:cast(?MODULE, {add_acceptor, Ref, Pid}).
  68. %% @doc Send a message to all acceptors of the given listener.
  69. -spec send_to_acceptors(any(), any()) -> ok.
  70. send_to_acceptors(Ref, Msg) ->
  71. Acceptors = ets:lookup_element(?TAB, {acceptors, Ref}, 2),
  72. _ = [Pid ! Msg || Pid <- Acceptors],
  73. ok.
  74. %% @doc Add a connection to the connection pool.
  75. %%
  76. %% Also return the number of connections in the pool after this operation.
  77. -spec add_connection(pid()) -> non_neg_integer().
  78. add_connection(ListenerPid) ->
  79. ets:update_counter(?TAB, {connections, ListenerPid}, 1).
  80. %% @doc Count the number of connections in the connection pool.
  81. -spec count_connections(pid()) -> non_neg_integer().
  82. count_connections(ListenerPid) ->
  83. ets:update_counter(?TAB, {connections, ListenerPid}, 0).
  84. %% @doc Remove a connection from the connection pool.
  85. %%
  86. %% Also return the number of connections in the pool after this operation.
  87. -spec remove_connection(pid()) -> non_neg_integer().
  88. remove_connection(ListenerPid) ->
  89. ets:update_counter(?TAB, {connections, ListenerPid}, -1).
  90. %% gen_server.
  91. %% @private
  92. init([]) ->
  93. {ok, #state{}}.
  94. %% @private
  95. handle_call(_Request, _From, State) ->
  96. {reply, ignore, State}.
  97. %% @private
  98. handle_cast({insert_listener, Ref, Pid}, State=#state{monitors=Monitors}) ->
  99. true = ets:insert_new(?TAB, {{acceptors, Ref}, []}),
  100. true = ets:insert_new(?TAB, {{connections, Pid}, 0}),
  101. MonitorRef = erlang:monitor(process, Pid),
  102. {noreply, State#state{
  103. monitors=[{{MonitorRef, Pid}, {listener, Ref}}|Monitors]}};
  104. handle_cast({add_acceptor, Ref, Pid}, State=#state{monitors=Monitors}) ->
  105. MonitorRef = erlang:monitor(process, Pid),
  106. Acceptors = ets:lookup_element(?TAB, {acceptors, Ref}, 2),
  107. true = ets:insert(?TAB, {{acceptors, Ref}, [Pid|Acceptors]}),
  108. {noreply, State#state{
  109. monitors=[{{MonitorRef, Pid}, {acceptors, Ref}}|Monitors]}};
  110. handle_cast({add_connection, Pid}, State) ->
  111. _ = erlang:monitor(process, Pid),
  112. {noreply, State};
  113. handle_cast(_Request, State) ->
  114. {noreply, State}.
  115. %% @private
  116. handle_info({'DOWN', MonitorRef, process, Pid, _},
  117. State=#state{monitors=Monitors}) ->
  118. {_, Key} = lists:keyfind({MonitorRef, Pid}, 1, Monitors),
  119. Monitors2 = remove_process(Key, MonitorRef, Pid, Monitors),
  120. {noreply, State#state{monitors=Monitors2}};
  121. handle_info(_Info, State) ->
  122. {noreply, State}.
  123. %% @private
  124. terminate(_Reason, _State) ->
  125. ok.
  126. %% @private
  127. code_change(_OldVsn, State, _Extra) ->
  128. {ok, State}.
  129. %% Internal.
  130. -spec remove_process(key(), reference(), pid(), Monitors)
  131. -> Monitors when Monitors::monitors() .
  132. remove_process(Key = {listener, Ref}, MonitorRef, Pid, Monitors) ->
  133. true = ets:delete(?TAB, Key),
  134. true = ets:delete(?TAB, {acceptors, Ref}),
  135. true = ets:delete(?TAB, {connections, Pid}),
  136. lists:keydelete({MonitorRef, Pid}, 1, Monitors);
  137. remove_process(Key = {acceptors, _}, MonitorRef, Pid, Monitors) ->
  138. try
  139. Acceptors = ets:lookup_element(?TAB, Key, 2),
  140. true = ets:update_element(?TAB, Key, {2, lists:delete(Pid, Acceptors)})
  141. catch
  142. error:_ ->
  143. ok
  144. end,
  145. lists:keydelete({MonitorRef, Pid}, 1, Monitors).