ranch.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. = ranch(3)
  2. == Name
  3. ranch - Socket acceptor pool
  4. == Description
  5. The module `ranch` provides functions for starting and
  6. manipulating Ranch listeners.
  7. == Exports
  8. Start/stop:
  9. * link:man:ranch:start_listener(3)[ranch:start_listener(3)] - Start a listener
  10. * link:man:ranch:stop_listener(3)[ranch:stop_listener(3)] - Stop a listener
  11. * link:man:ranch:child_spec(3)[ranch:child_spec(3)] - Build child specifications for a new listener
  12. Suspend/resume:
  13. * link:man:ranch:suspend_listener(3)[ranch:suspend_listener(3)] - Suspend a running listener
  14. * link:man:ranch:resume_listener(3)[ranch:resume_listener(3)] - Resume a suspended listener
  15. * link:man:ranch:get_status(3)[ranch:get_status(3)] - Get a listener's running state
  16. Connections:
  17. * link:man:ranch:handshake(3)[ranch:handshake(3)] - Perform the transport handshake
  18. * link:man:ranch:handshake_continue(3)[ranch:handshake_continue(3)] - Resume the paused transport handshake
  19. * link:man:ranch:handshake_cancel(3)[ranch:handshake_cancel(3)] - Cancel the paused transport handshake
  20. * link:man:ranch:recv_proxy_header(3)[ranch:recv_proxy_header(3)] - Receive the PROXY protocol header
  21. * link:man:ranch:remove_connection(3)[ranch:remove_connection(3)] - Remove connection from the count
  22. Options:
  23. * link:man:ranch:get_max_connections(3)[ranch:get_max_connections(3)] - Get the max number of connections per connection supervisor
  24. * link:man:ranch:get_protocol_options(3)[ranch:get_protocol_options(3)] - Get the current protocol options
  25. * link:man:ranch:get_transport_options(3)[ranch:get_transport_options(3)] - Get the current transport options
  26. * link:man:ranch:set_max_connections(3)[ranch:set_max_connections(3)] - Set the max number of connections per connection supervisor
  27. * link:man:ranch:set_protocol_options(3)[ranch:set_protocol_options(3)] - Set the protocol options
  28. * link:man:ranch:set_transport_options(3)[ranch:set_transport_options(3)] - Set the transport options
  29. Introspection:
  30. * link:man:ranch:get_addr(3)[ranch:get_addr(3)] - Get the listening address
  31. * link:man:ranch:get_port(3)[ranch:get_port(3)] - Get the listening port
  32. * link:man:ranch:info(3)[ranch:info(3)] - Overview of Ranch listeners
  33. * link:man:ranch:procs(3)[ranch:procs(3)] - Retrieve pids from a listener
  34. * link:man:ranch:wait_for_connections(3)[ranch:wait_for_connections(3)] - Wait for a specific number of connections
  35. == Types
  36. === max_conns()
  37. [source,erlang]
  38. ----
  39. max_conns() = non_neg_integer() | infinity
  40. ----
  41. Maximum number of connections allowed per connection supervisor.
  42. This is a soft limit. The actual number of connections
  43. might be slightly above the limit due to concurrency
  44. when accepting new connections. Some connections may
  45. also be removed from this count explicitly by the user
  46. code.
  47. === opts()
  48. [source,erlang]
  49. ----
  50. opts() = any() | transport_opts(any())
  51. ----
  52. Transport or socket options.
  53. === ref()
  54. [source,erlang]
  55. ----
  56. ref() = any()
  57. ----
  58. Unique name used to refer to a listener.
  59. === transport_opts(SocketOpts)
  60. [source,erlang]
  61. ----
  62. transport_opts(SocketOpts) = #{
  63. alarms => #{
  64. term() => #{
  65. type := num_connections,
  66. treshold := non_neg_integer(),
  67. callback := fun((ref(), term(), pid(), [pid()]) -> any()),
  68. cooldown => non_neg_integer()
  69. }
  70. },
  71. connection_type => worker | supervisor,
  72. handshake_timeout => timeout(),
  73. max_connections => max_conns(),
  74. logger => module(),
  75. num_acceptors => pos_integer(),
  76. num_conns_sups => pos_integer(),
  77. post_listen_callback => fun((term()) -> ok | {error, term()}),
  78. shutdown => timeout() | brutal_kill,
  79. socket_opts => SocketOpts
  80. }
  81. ----
  82. Transport options.
  83. The transport options are a combination of Ranch-specific
  84. options and transport-specific socket options.
  85. None of the options are required.
  86. alarms (#{})::
  87. Alarms to call a function when the number of connections tracked
  88. by one connection supervisor reaches or exceeds a defined treshold.
  89. +
  90. The map keys are the alarm names, which can be any `term`. The
  91. associated values are the respective alarm options, again in a map
  92. with the following keys:
  93. type:::
  94. Must be set to `num_connections`.
  95. treshold:::
  96. Treshold value, which must be a `non_neg_integer`. When the
  97. number of connections tracked by a single connection supervisor
  98. reaches or exceeds this value, The alarm will trigger and call
  99. the function defined in the `callback` key (see below).
  100. callback:::
  101. The alarm function, which takes the listener name, the alarm
  102. name, the pid of the connection supervisor and a list of the pids
  103. of all connection processes under that supervisor as arguments.
  104. The return value is ignored.
  105. cooldown (5000):::
  106. The minimum time after which the alarm can be triggered again,
  107. in milliseconds.
  108. connection_type (worker)::
  109. Type of process that will handle the connection.
  110. handshake_timeout (5000)::
  111. Maximum allowed time for the `ranch:handshake/1,2` call to finish.
  112. logger (logger)::
  113. The module that will be used to write log messages.
  114. max_connections (1024)::
  115. Maximum number of active connections per connection supervisor.
  116. Soft limit. Use `infinity` to disable the limit entirely.
  117. num_acceptors (10)::
  118. Number of processes that accept connections.
  119. num_conns_sups - see below::
  120. Number of processes that supervise connection processes.
  121. If not specified, defaults to be equal to `num_acceptors`.
  122. post_listen_callback (fun(_ListenSock) -> ok end)::
  123. A function which will be called after a listen socket has been successfully
  124. created, with the socket as argument. It can be used to perform any
  125. necessary setup steps on the socket.
  126. +
  127. If the callback function returns `ok`, the listener will start accepting
  128. connections on the socket. If it returns `{error, Reason}`, the listener
  129. will fail to start.
  130. shutdown (5000)::
  131. Maximum allowed time for children to stop on listener shutdown.
  132. socket_opts::
  133. Socket options to be used by `Transport:listen/1`. Please refer to the
  134. documentation of the transport module you are using for more details.
  135. == Changelog
  136. * *2.0*: The type `transport_opts(SocketOpts)` was added.
  137. * *2.0*: The function `ranch:accept_ack/1` was removed in favor of
  138. link:man:ranch:handshake(3)[ranch:handshake(3)].
  139. * *2.0*: The option `max_connections` is now per connection supervisor.
  140. * *2.0*: The `num_conns_sup` option was added.
  141. * *2.0*: The `socket` option was removed.
  142. * *2.0*: The `logger` option is no longer experimental. It now defaults
  143. to `logger` instead of `error_logger`.
  144. * *2.0*: The `opt()` type was removed.
  145. * *1.6*: The experimental `logger` option was added.
  146. * *1.6*: The `opt()` type was deprecated in favor of the new `opts()` type.
  147. == See also
  148. link:man:ranch(7)[ranch(7)]