cowboy.erl 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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]).
  17. %% @doc Start a listener for the given transport and protocol.
  18. %%
  19. %% A listener is effectively a pool of <em>NbAcceptors</em> acceptors.
  20. %% Acceptors accept connections on the given <em>Transport</em> and forward
  21. %% requests to the given <em>Protocol</em> handler. Both transport and protocol
  22. %% modules can be given options through the <em>TransOpts</em> and the
  23. %% <em>ProtoOpts</em> arguments. Available options are documented in the
  24. %% <em>listen</em> transport function and in the protocol module of your choice.
  25. %%
  26. %% All acceptor and request processes are supervised by the listener.
  27. %%
  28. %% It is recommended to set a large enough number of acceptors to improve
  29. %% performance. The exact number depends of course on your hardware, on the
  30. %% protocol used and on the number of expected simultaneous connections.
  31. %%
  32. %% The <em>Transport</em> option <em>max_connections</em> allows you to define
  33. %% the maximum number of simultaneous connections for this listener. It defaults
  34. %% to 1024. See <em>cowboy_listener</em> for more details on limiting the number
  35. %% of connections.
  36. %%
  37. %% Although Cowboy includes a <em>cowboy_http_protocol</em> handler, other
  38. %% handlers can be created for different protocols like IRC, FTP and more.
  39. %%
  40. %% <em>Ref</em> can be used to stop the listener later on.
  41. -spec start_listener(any(), non_neg_integer(), module(), any(), module(), any())
  42. -> {ok, pid()}.
  43. start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  44. when is_integer(NbAcceptors) andalso is_atom(Transport)
  45. andalso is_atom(Protocol) ->
  46. supervisor:start_child(cowboy_sup, child_spec(Ref, NbAcceptors,
  47. Transport, TransOpts, Protocol, ProtoOpts)).
  48. %% @doc Stop a listener identified by <em>Ref</em>.
  49. %% @todo Currently request processes aren't terminated with the listener.
  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. %% When you want to embed cowboy in another application, you can use this
  60. %% function to create a <em>ChildSpec</em> suitable for use in a supervisor.
  61. %% The parameters are the same as in <em>start_listener/6</em> but rather
  62. %% than hooking the listener to the cowboy internal supervisor, it just returns
  63. %% the spec.
  64. -spec child_spec(any(), non_neg_integer(), module(), any(), module(), any())
  65. -> supervisor:child_spec().
  66. child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
  67. when is_integer(NbAcceptors) andalso is_atom(Transport)
  68. andalso is_atom(Protocol) ->
  69. {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [
  70. NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
  71. ]}, permanent, 5000, supervisor, [cowboy_listener_sup]}.