ranch_listener.erl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. %% Copyright (c) 2011-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. %% @doc Public API for managing listeners.
  15. -module(ranch_listener).
  16. -behaviour(gen_server).
  17. %% API.
  18. -export([start_link/3]).
  19. -export([stop/1]).
  20. -export([add_connection/2]).
  21. -export([remove_connection/1]).
  22. -export([get_port/1]).
  23. -export([set_port/2]).
  24. -export([get_max_connections/1]).
  25. -export([set_max_connections/2]).
  26. -export([get_protocol_options/1]).
  27. -export([set_protocol_options/2]).
  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. -record(state, {
  36. ref :: any(),
  37. max_conns = undefined :: ranch:max_conns(),
  38. port = undefined :: undefined | inet:port_number(),
  39. proto_opts = undefined :: any(),
  40. rm_diff = 0 :: non_neg_integer()
  41. }).
  42. %% API.
  43. %% @private
  44. -spec start_link(any(), non_neg_integer(), any()) -> {ok, pid()}.
  45. start_link(Ref, MaxConns, ProtoOpts) ->
  46. gen_server:start_link(?MODULE, [Ref, MaxConns, ProtoOpts], []).
  47. %% @private
  48. -spec stop(pid()) -> stopped.
  49. stop(ServerPid) ->
  50. gen_server:call(ServerPid, stop).
  51. %% @doc Add a connection to the listener's pool.
  52. -spec add_connection(pid(), pid()) -> non_neg_integer().
  53. add_connection(ServerPid, ConnPid) ->
  54. ok = gen_server:cast(ServerPid, {add_connection, ConnPid}),
  55. ranch_server:add_connection(ServerPid).
  56. %% @doc Remove this process' connection from the pool.
  57. %%
  58. %% Useful if you have long-lived connections that aren't taking up
  59. %% resources and shouldn't be counted in the limited number of running
  60. %% connections.
  61. -spec remove_connection(pid()) -> non_neg_integer().
  62. remove_connection(ServerPid) ->
  63. ok = gen_server:cast(ServerPid, remove_connection),
  64. ranch_server:remove_connection(ServerPid).
  65. %% @doc Return the listener's port.
  66. -spec get_port(pid()) -> {ok, inet:port_number()}.
  67. get_port(ServerPid) ->
  68. gen_server:call(ServerPid, get_port).
  69. %% @private
  70. -spec set_port(pid(), inet:port_number()) -> ok.
  71. set_port(ServerPid, Port) ->
  72. gen_server:cast(ServerPid, {set_port, Port}).
  73. %% @doc Return the max number of connections allowed concurrently.
  74. -spec get_max_connections(pid()) -> {ok, ranch:max_conns()}.
  75. get_max_connections(ServerPid) ->
  76. gen_server:call(ServerPid, get_max_connections).
  77. %% @doc Set the max number of connections allowed concurrently.
  78. -spec set_max_connections(pid(), ranch:max_conns()) -> ok.
  79. set_max_connections(ServerPid, MaxConnections) ->
  80. gen_server:call(ServerPid, {set_max_connections, MaxConnections}).
  81. %% @doc Return the current protocol options.
  82. -spec get_protocol_options(pid()) -> {ok, any()}.
  83. get_protocol_options(ServerPid) ->
  84. gen_server:call(ServerPid, get_protocol_options).
  85. %% @doc Upgrade the protocol options.
  86. -spec set_protocol_options(pid(), any()) -> ok.
  87. set_protocol_options(ServerPid, ProtoOpts) ->
  88. gen_server:call(ServerPid, {set_protocol_options, ProtoOpts}).
  89. %% gen_server.
  90. %% @private
  91. init([Ref, MaxConns, ProtoOpts]) ->
  92. ok = ranch_server:insert_listener(Ref, self()),
  93. {ok, #state{ref=Ref, max_conns=MaxConns, proto_opts=ProtoOpts}}.
  94. %% @private
  95. handle_call(get_port, _From, State=#state{port=Port}) ->
  96. {reply, {ok, Port}, State};
  97. handle_call(get_max_connections, _From, State=#state{max_conns=MaxConns}) ->
  98. {reply, {ok, MaxConns}, State};
  99. handle_call({set_max_connections, MaxConnections}, _From,
  100. State=#state{ref=Ref}) ->
  101. ranch_server:send_to_acceptors(Ref, {set_max_conns, MaxConnections}),
  102. {reply, ok, State#state{max_conns=MaxConnections}};
  103. handle_call(get_protocol_options, _From, State=#state{proto_opts=ProtoOpts}) ->
  104. {reply, {ok, ProtoOpts}, State};
  105. handle_call({set_protocol_options, ProtoOpts}, _From, State=#state{ref=Ref}) ->
  106. ranch_server:send_to_acceptors(Ref, {set_opts, ProtoOpts}),
  107. {reply, ok, State#state{proto_opts=ProtoOpts}};
  108. handle_call(stop, _From, State) ->
  109. {stop, normal, stopped, State};
  110. handle_call(_, _From, State) ->
  111. {reply, ignored, State}.
  112. %% @private
  113. handle_cast({add_connection, ConnPid}, State) ->
  114. _ = erlang:monitor(process, ConnPid),
  115. {noreply, State};
  116. handle_cast(remove_connection, State=#state{rm_diff=RmDiff}) ->
  117. {noreply, State#state{rm_diff=RmDiff + 1}};
  118. handle_cast({set_port, Port}, State) ->
  119. {noreply, State#state{port=Port}};
  120. handle_cast(_Msg, State) ->
  121. {noreply, State}.
  122. %% @private
  123. handle_info({'DOWN', _, process, _, _}, State=#state{rm_diff=0}) ->
  124. _ = ranch_server:remove_connection(self()),
  125. {noreply, State};
  126. handle_info({'DOWN', _, process, _, _}, State=#state{rm_diff=RmDiff}) ->
  127. {noreply, State#state{rm_diff=RmDiff - 1}};
  128. handle_info(_Info, State) ->
  129. {noreply, State}.
  130. %% @private
  131. terminate(_Reason, _State) ->
  132. ok.
  133. %% @private
  134. code_change(_OldVsn, State, _Extra) ->
  135. {ok, State}.