ranch_server.erl 5.0 KB

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