ranch.erl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. %% Copyright (c) 2011-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).
  15. -export([start_listener/6]).
  16. -export([stop_listener/1]).
  17. -export([child_spec/6]).
  18. -export([accept_ack/1]).
  19. -export([remove_connection/1]).
  20. -export([get_addr/1]).
  21. -export([get_port/1]).
  22. -export([get_max_connections/1]).
  23. -export([set_max_connections/2]).
  24. -export([get_protocol_options/1]).
  25. -export([set_protocol_options/2]).
  26. -export([filter_options/3]).
  27. -export([set_option_default/3]).
  28. -export([require/1]).
  29. -type max_conns() :: non_neg_integer() | infinity.
  30. -export_type([max_conns/0]).
  31. -type opt() :: {ack_timeout, timeout()}
  32. | {connection_type, worker | supervisor}
  33. | {max_connections, max_conns()}
  34. | {shutdown, timeout() | brutal_kill}
  35. | {socket, any()}.
  36. -export_type([opt/0]).
  37. -type ref() :: any().
  38. -export_type([ref/0]).
  39. -spec start_listener(ref(), non_neg_integer(), module(), any(), module(), any())
  40. -> supervisor:startchild_ret().
  41. start_listener(Ref, NumAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  42. when is_integer(NumAcceptors) andalso is_atom(Transport)
  43. andalso is_atom(Protocol) ->
  44. _ = code:ensure_loaded(Transport),
  45. %% @todo Remove in Ranch 2.0 and simply require ssl.
  46. _ = ensure_ssl(Transport),
  47. case erlang:function_exported(Transport, name, 0) of
  48. false ->
  49. {error, badarg};
  50. true ->
  51. Res = supervisor:start_child(ranch_sup, child_spec(Ref, NumAcceptors,
  52. Transport, TransOpts, Protocol, ProtoOpts)),
  53. Socket = proplists:get_value(socket, TransOpts),
  54. case Res of
  55. {ok, Pid} when Socket =/= undefined ->
  56. %% Give ownership of the socket to ranch_acceptors_sup
  57. %% to make sure the socket stays open as long as the
  58. %% listener is alive. If the socket closes however there
  59. %% will be no way to recover because we don't know how
  60. %% to open it again.
  61. Children = supervisor:which_children(Pid),
  62. {_, AcceptorsSup, _, _}
  63. = lists:keyfind(ranch_acceptors_sup, 1, Children),
  64. %%% Note: the catch is here because SSL crashes when you change
  65. %%% the controlling process of a listen socket because of a bug.
  66. %%% The bug will be fixed in R16.
  67. catch Transport:controlling_process(Socket, AcceptorsSup);
  68. _ ->
  69. ok
  70. end,
  71. Res
  72. end.
  73. -spec stop_listener(ref()) -> ok | {error, not_found}.
  74. stop_listener(Ref) ->
  75. case supervisor:terminate_child(ranch_sup, {ranch_listener_sup, Ref}) of
  76. ok ->
  77. _ = supervisor:delete_child(ranch_sup, {ranch_listener_sup, Ref}),
  78. ranch_server:cleanup_listener_opts(Ref);
  79. {error, Reason} ->
  80. {error, Reason}
  81. end.
  82. -spec child_spec(ref(), non_neg_integer(), module(), any(), module(), any())
  83. -> supervisor:child_spec().
  84. child_spec(Ref, NumAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  85. when is_integer(NumAcceptors) andalso is_atom(Transport)
  86. andalso is_atom(Protocol) ->
  87. %% @todo Remove in Ranch 2.0 and simply require ssl.
  88. _ = ensure_ssl(Transport),
  89. {{ranch_listener_sup, Ref}, {ranch_listener_sup, start_link, [
  90. Ref, NumAcceptors, Transport, TransOpts, Protocol, ProtoOpts
  91. ]}, permanent, infinity, supervisor, [ranch_listener_sup]}.
  92. %% @todo Remove in Ranch 2.0 and simply require ssl.
  93. ensure_ssl(ranch_ssl) ->
  94. require([crypto, asn1, public_key, ssl]);
  95. ensure_ssl(_) ->
  96. ok.
  97. -spec accept_ack(ref()) -> ok.
  98. accept_ack(Ref) ->
  99. receive {shoot, Ref, Transport, Socket, AckTimeout} ->
  100. Transport:accept_ack(Socket, AckTimeout)
  101. end.
  102. -spec remove_connection(ref()) -> ok.
  103. remove_connection(Ref) ->
  104. ConnsSup = ranch_server:get_connections_sup(Ref),
  105. ConnsSup ! {remove_connection, Ref},
  106. ok.
  107. -spec get_addr(ref()) -> {inet:ip_address(), inet:port_number()}.
  108. get_addr(Ref) ->
  109. ranch_server:get_addr(Ref).
  110. -spec get_port(ref()) -> inet:port_number().
  111. get_port(Ref) ->
  112. {_, Port} = get_addr(Ref),
  113. Port.
  114. -spec get_max_connections(ref()) -> max_conns().
  115. get_max_connections(Ref) ->
  116. ranch_server:get_max_connections(Ref).
  117. -spec set_max_connections(ref(), max_conns()) -> ok.
  118. set_max_connections(Ref, MaxConnections) ->
  119. ranch_server:set_max_connections(Ref, MaxConnections).
  120. -spec get_protocol_options(ref()) -> any().
  121. get_protocol_options(Ref) ->
  122. ranch_server:get_protocol_options(Ref).
  123. -spec set_protocol_options(ref(), any()) -> ok.
  124. set_protocol_options(Ref, Opts) ->
  125. ranch_server:set_protocol_options(Ref, Opts).
  126. -spec filter_options([inet | inet6 | {atom(), any()} | {raw, any(), any(), any()}],
  127. [atom()], Acc) -> Acc when Acc :: [any()].
  128. filter_options(UserOptions, AllowedKeys, DefaultOptions) ->
  129. AllowedOptions = filter_user_options(UserOptions, AllowedKeys),
  130. lists:foldl(fun merge_options/2, DefaultOptions, AllowedOptions).
  131. %% 2-tuple options.
  132. filter_user_options([Opt = {Key, _}|Tail], AllowedKeys) ->
  133. case lists:member(Key, AllowedKeys) of
  134. true ->
  135. [Opt|filter_user_options(Tail, AllowedKeys)];
  136. false ->
  137. filter_options_warning(Opt),
  138. filter_user_options(Tail, AllowedKeys)
  139. end;
  140. %% Special option forms.
  141. filter_user_options([inet|Tail], AllowedKeys) ->
  142. [inet|filter_user_options(Tail, AllowedKeys)];
  143. filter_user_options([inet6|Tail], AllowedKeys) ->
  144. [inet6|filter_user_options(Tail, AllowedKeys)];
  145. filter_user_options([Opt = {raw, _, _, _}|Tail], AllowedKeys) ->
  146. [Opt|filter_user_options(Tail, AllowedKeys)];
  147. filter_user_options([Opt|Tail], AllowedKeys) ->
  148. filter_options_warning(Opt),
  149. filter_user_options(Tail, AllowedKeys);
  150. filter_user_options([], _) ->
  151. [].
  152. filter_options_warning(Opt) ->
  153. error_logger:warning_msg("Transport option ~p unknown or invalid.~n", [Opt]).
  154. merge_options({Key, _} = Option, OptionList) ->
  155. lists:keystore(Key, 1, OptionList, Option);
  156. merge_options(Option, OptionList) ->
  157. [Option|OptionList].
  158. -spec set_option_default(Opts, atom(), any())
  159. -> Opts when Opts :: [{atom(), any()}].
  160. set_option_default(Opts, Key, Value) ->
  161. case lists:keymember(Key, 1, Opts) of
  162. true -> Opts;
  163. false -> [{Key, Value}|Opts]
  164. end.
  165. -spec require([atom()]) -> ok.
  166. require([]) ->
  167. ok;
  168. require([App|Tail]) ->
  169. case application:start(App) of
  170. ok -> ok;
  171. {error, {already_started, App}} -> ok
  172. end,
  173. require(Tail).