ranch.erl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. %% Copyright (c) 2011-2014, 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_port/1]).
  21. -export([get_max_connections/1]).
  22. -export([set_max_connections/2]).
  23. -export([get_protocol_options/1]).
  24. -export([set_protocol_options/2]).
  25. -export([filter_options/3]).
  26. -export([set_option_default/3]).
  27. -export([require/1]).
  28. -type max_conns() :: non_neg_integer() | infinity.
  29. -export_type([max_conns/0]).
  30. -type ref() :: any().
  31. -export_type([ref/0]).
  32. -spec start_listener(ref(), non_neg_integer(), module(), any(), module(), any())
  33. -> {ok, pid()} | {error, badarg}.
  34. start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  35. when is_integer(NbAcceptors) andalso is_atom(Transport)
  36. andalso is_atom(Protocol) ->
  37. _ = code:ensure_loaded(Transport),
  38. case erlang:function_exported(Transport, name, 0) of
  39. false ->
  40. {error, badarg};
  41. true ->
  42. Res = supervisor:start_child(ranch_sup, child_spec(Ref, NbAcceptors,
  43. Transport, TransOpts, Protocol, ProtoOpts)),
  44. Socket = proplists:get_value(socket, TransOpts),
  45. case Res of
  46. {ok, Pid} when Socket =/= undefined ->
  47. %% Give ownership of the socket to ranch_acceptors_sup
  48. %% to make sure the socket stays open as long as the
  49. %% listener is alive. If the socket closes however there
  50. %% will be no way to recover because we don't know how
  51. %% to open it again.
  52. Children = supervisor:which_children(Pid),
  53. {_, AcceptorsSup, _, _}
  54. = lists:keyfind(ranch_acceptors_sup, 1, Children),
  55. %%% Note: the catch is here because SSL crashes when you change
  56. %%% the controlling process of a listen socket because of a bug.
  57. %%% The bug will be fixed in R16.
  58. catch Transport:controlling_process(Socket, AcceptorsSup);
  59. _ ->
  60. ok
  61. end,
  62. Res
  63. end.
  64. -spec stop_listener(ref()) -> ok | {error, not_found}.
  65. stop_listener(Ref) ->
  66. case supervisor:terminate_child(ranch_sup, {ranch_listener_sup, Ref}) of
  67. ok ->
  68. _ = supervisor:delete_child(ranch_sup, {ranch_listener_sup, Ref}),
  69. ranch_server:cleanup_listener_opts(Ref);
  70. {error, Reason} ->
  71. {error, Reason}
  72. end.
  73. -spec child_spec(ref(), non_neg_integer(), module(), any(), module(), any())
  74. -> supervisor:child_spec().
  75. child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  76. when is_integer(NbAcceptors) andalso is_atom(Transport)
  77. andalso is_atom(Protocol) ->
  78. {{ranch_listener_sup, Ref}, {ranch_listener_sup, start_link, [
  79. Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
  80. ]}, permanent, infinity, supervisor, [ranch_listener_sup]}.
  81. -spec accept_ack(ref()) -> ok.
  82. accept_ack(Ref) ->
  83. receive {shoot, Ref, Transport, Socket, AckTimeout} ->
  84. Transport:accept_ack(Socket, AckTimeout)
  85. end.
  86. -spec remove_connection(ref()) -> ok.
  87. remove_connection(Ref) ->
  88. ConnsSup = ranch_server:get_connections_sup(Ref),
  89. ConnsSup ! {remove_connection, Ref},
  90. ok.
  91. -spec get_port(ref()) -> inet:port_number().
  92. get_port(Ref) ->
  93. ranch_server:get_port(Ref).
  94. -spec get_max_connections(ref()) -> max_conns().
  95. get_max_connections(Ref) ->
  96. ranch_server:get_max_connections(Ref).
  97. -spec set_max_connections(ref(), max_conns()) -> ok.
  98. set_max_connections(Ref, MaxConnections) ->
  99. ranch_server:set_max_connections(Ref, MaxConnections).
  100. -spec get_protocol_options(ref()) -> any().
  101. get_protocol_options(Ref) ->
  102. ranch_server:get_protocol_options(Ref).
  103. -spec set_protocol_options(ref(), any()) -> ok.
  104. set_protocol_options(Ref, Opts) ->
  105. ranch_server:set_protocol_options(Ref, Opts).
  106. -spec filter_options([{atom(), any()} | {raw, any(), any(), any()}],
  107. [atom()], Acc) -> Acc when Acc :: [any()].
  108. filter_options(UserOptions, AllowedKeys, DefaultOptions) ->
  109. AllowedOptions = filter_user_options(UserOptions, AllowedKeys),
  110. lists:foldl(fun merge_options/2, DefaultOptions, AllowedOptions).
  111. filter_user_options([Opt = {Key, _}|Tail], AllowedKeys) ->
  112. case lists:member(Key, AllowedKeys) of
  113. true -> [Opt|filter_user_options(Tail, AllowedKeys)];
  114. false -> filter_user_options(Tail, AllowedKeys)
  115. end;
  116. filter_user_options([Opt = {raw, _, _, _}|Tail], AllowedKeys) ->
  117. case lists:member(raw, AllowedKeys) of
  118. true -> [Opt|filter_user_options(Tail, AllowedKeys)];
  119. false -> filter_user_options(Tail, AllowedKeys)
  120. end;
  121. filter_user_options([], _) ->
  122. [].
  123. merge_options({Key, _} = Option, OptionList) ->
  124. lists:keystore(Key, 1, OptionList, Option);
  125. merge_options(Option, OptionList) ->
  126. [Option|OptionList].
  127. -spec set_option_default(Opts, atom(), any())
  128. -> Opts when Opts :: [{atom(), any()}].
  129. set_option_default(Opts, Key, Value) ->
  130. case lists:keymember(Key, 1, Opts) of
  131. true -> Opts;
  132. false -> [{Key, Value}|Opts]
  133. end.
  134. -spec require([atom()]) -> ok.
  135. require([]) ->
  136. ok;
  137. require([App|Tail]) ->
  138. case application:start(App) of
  139. ok -> ok;
  140. {error, {already_started, App}} -> ok
  141. end,
  142. require(Tail).