cowboy.erl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. %% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.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 Cowboy API to start and stop listeners.
  15. -module(cowboy).
  16. -export([start_listener/6, stop_listener/1, child_spec/6, accept_ack/1,
  17. get_protocol_options/1, set_protocol_options/2]).
  18. %% @doc Start a listener for the given transport and protocol.
  19. %%
  20. %% A listener is effectively a pool of <em>NbAcceptors</em> acceptors.
  21. %% Acceptors accept connections on the given <em>Transport</em> and forward
  22. %% requests to the given <em>Protocol</em> handler. Both transport and protocol
  23. %% modules can be given options through the <em>TransOpts</em> and the
  24. %% <em>ProtoOpts</em> arguments. Available options are documented in the
  25. %% <em>listen</em> transport function and in the protocol module of your choice.
  26. %%
  27. %% All acceptor and request processes are supervised by the listener.
  28. %%
  29. %% It is recommended to set a large enough number of acceptors to improve
  30. %% performance. The exact number depends of course on your hardware, on the
  31. %% protocol used and on the number of expected simultaneous connections.
  32. %%
  33. %% The <em>Transport</em> option <em>max_connections</em> allows you to define
  34. %% the maximum number of simultaneous connections for this listener. It defaults
  35. %% to 1024. See <em>cowboy_listener</em> for more details on limiting the number
  36. %% of connections.
  37. %%
  38. %% Although Cowboy includes a <em>cowboy_http_protocol</em> handler, other
  39. %% handlers can be created for different protocols like IRC, FTP and more.
  40. %%
  41. %% <em>Ref</em> can be used to stop the listener later on.
  42. -spec start_listener(any(), non_neg_integer(), module(), any(), module(), any())
  43. -> {ok, pid()}.
  44. start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  45. when is_integer(NbAcceptors) andalso is_atom(Transport)
  46. andalso is_atom(Protocol) ->
  47. supervisor:start_child(cowboy_sup, child_spec(Ref, NbAcceptors,
  48. Transport, TransOpts, Protocol, ProtoOpts)).
  49. %% @doc Stop a listener identified by <em>Ref</em>.
  50. -spec stop_listener(any()) -> ok | {error, not_found}.
  51. stop_listener(Ref) ->
  52. case supervisor:terminate_child(cowboy_sup, {cowboy_listener_sup, Ref}) of
  53. ok ->
  54. supervisor:delete_child(cowboy_sup, {cowboy_listener_sup, Ref});
  55. {error, Reason} ->
  56. {error, Reason}
  57. end.
  58. %% @doc Return a child spec suitable for embedding.
  59. %%
  60. %% When you want to embed cowboy in another application, you can use this
  61. %% function to create a <em>ChildSpec</em> suitable for use in a supervisor.
  62. %% The parameters are the same as in <em>start_listener/6</em> but rather
  63. %% than hooking the listener to the cowboy internal supervisor, it just returns
  64. %% the spec.
  65. -spec child_spec(any(), non_neg_integer(), module(), any(), module(), any())
  66. -> supervisor:child_spec().
  67. child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  68. when is_integer(NbAcceptors) andalso is_atom(Transport)
  69. andalso is_atom(Protocol) ->
  70. {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [
  71. NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
  72. ]}, permanent, 5000, supervisor, [cowboy_listener_sup]}.
  73. %% @doc Acknowledge the accepted connection.
  74. %%
  75. %% Effectively used to make sure the socket control has been given to
  76. %% the protocol process before starting to use it.
  77. -spec accept_ack(pid()) -> ok.
  78. accept_ack(ListenerPid) ->
  79. receive {shoot, ListenerPid} -> ok end.
  80. %% @doc Return the current protocol options for the given listener.
  81. -spec get_protocol_options(any()) -> any().
  82. get_protocol_options(Ref) ->
  83. ListenerPid = ref_to_listener_pid(Ref),
  84. {ok, ProtoOpts} = cowboy_listener:get_protocol_options(ListenerPid),
  85. ProtoOpts.
  86. %% @doc Upgrade the protocol options for the given listener.
  87. %%
  88. %% The upgrade takes place at the acceptor level, meaning that only the
  89. %% newly accepted connections receive the new protocol options. This has
  90. %% no effect on the currently opened connections.
  91. -spec set_protocol_options(any(), any()) -> ok.
  92. set_protocol_options(Ref, ProtoOpts) ->
  93. ListenerPid = ref_to_listener_pid(Ref),
  94. ok = cowboy_listener:set_protocol_options(ListenerPid, ProtoOpts).
  95. %% Internal.
  96. -spec ref_to_listener_pid(any()) -> pid().
  97. ref_to_listener_pid(Ref) ->
  98. Children = supervisor:which_children(cowboy_sup),
  99. {_, ListenerSupPid, _, _} = lists:keyfind(
  100. {cowboy_listener_sup, Ref}, 1, Children),
  101. ListenerSupChildren = supervisor:which_children(ListenerSupPid),
  102. {_, ListenerPid, _, _} = lists:keyfind(
  103. cowboy_listener, 1, ListenerSupChildren),
  104. ListenerPid.