listeners.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. == Listeners
  2. A listener is a set of processes whose role is to listen on a port
  3. for new connections. It manages a pool of acceptor processes, each
  4. of them indefinitely accepting connections. When it does, it starts
  5. a new process executing the protocol handler code. All the socket
  6. programming is abstracted through the use of transport handlers.
  7. The listener takes care of supervising all the acceptor and connection
  8. processes, allowing developers to focus on building their application.
  9. === Starting a listener
  10. Ranch does nothing by default. It is up to the application developer
  11. to request that Ranch listens for connections.
  12. A listener can be started and stopped at will.
  13. When starting a listener, a number of different settings are required:
  14. * A name to identify it locally and be able to interact with it.
  15. * The number of acceptors in the pool.
  16. * A transport handler and its associated options.
  17. * A protocol handler and its associated options.
  18. Ranch includes both TCP and SSL transport handlers, respectively
  19. `ranch_tcp` and `ranch_ssl`.
  20. A listener can be started by calling the `ranch:start_listener/5`
  21. function. Before doing so however, you must ensure that the `ranch`
  22. application is started.
  23. .Starting the Ranch application
  24. [source,erlang]
  25. ok = application:start(ranch).
  26. You are then ready to start a listener. Let's call it `tcp_echo`. It will
  27. have a pool of 100 acceptors, use a TCP transport and forward connections
  28. to the `echo_protocol` handler.
  29. .Starting a listener for TCP connections on port 5555
  30. [source,erlang]
  31. {ok, _} = ranch:start_listener(tcp_echo,
  32. ranch_tcp, [{port, 5555}],
  33. echo_protocol, []
  34. ).
  35. You can try this out by compiling and running the `tcp_echo` example in the
  36. examples directory. To do so, open a shell in the 'examples/tcp_echo/'
  37. directory and run the following command:
  38. .Building and starting a Ranch example
  39. [source,bash]
  40. $ make run
  41. You can then connect to it using telnet and see the echo server reply
  42. everything you send to it. Then when you're done testing, you can use
  43. the `Ctrl+]` key to escape to the telnet command line and type
  44. `quit` to exit.
  45. .Connecting to the example listener with telnet
  46. [source,bash]
  47. ----
  48. $ telnet localhost 5555
  49. Trying 127.0.0.1...
  50. Connected to localhost.
  51. Escape character is '^]'.
  52. Hello!
  53. Hello!
  54. It works!
  55. It works!
  56. ^]
  57. telnet> quit
  58. Connection closed.
  59. ----
  60. === Stopping a listener
  61. All you need to stop a Ranch listener is to call the
  62. `ranch:stop_listener/1` function with the listener's name
  63. as argument. In the previous section we started the listener
  64. named `tcp_echo`. We can now stop it.
  65. .Stopping a listener
  66. [source,erlang]
  67. ranch:stop_listener(tcp_echo).
  68. === Suspending and resuming a listener
  69. Listeners can be suspended and resumed by calling
  70. `ranch:suspend_listener/1` and `ranch:resume_listener/1`,
  71. respectively, with the name of the listener as argument.
  72. Suspending a listener will cause it to stop listening and not accept
  73. new connections, but existing connection processes will not be stopped.
  74. .Suspending a listener
  75. [source,erlang]
  76. ranch:suspend_listener(tcp_echo).
  77. Resuming a listener will cause it to start listening and accept new
  78. connections again.
  79. It is worth mentioning, however, that if the listener is configured
  80. to listen on a random port, it will listen on a different port than
  81. before it was suspended.
  82. .Resuming a listener
  83. [source,erlang]
  84. ranch:resume_listener(tcp_echo).
  85. Whether a listener is currently running or suspended can be queried
  86. by calling `ranch:get_status/1` with the listener name as argument.
  87. === Default transport options
  88. By default the socket will be set to return `binary` data, with the
  89. options `{active, false}`, `{packet, raw}`, `{reuseaddr, true}` set.
  90. These values can't be overriden when starting the listener, but
  91. they can be overriden using `Transport:setopts/2` in the protocol.
  92. It will also set `{backlog, 1024}` and `{nodelay, true}`, which
  93. can be overriden at listener startup.
  94. === Listening on a random port
  95. You do not have to specify a specific port to listen on. If you give
  96. the port number 0, or if you omit the port number entirely, Ranch will
  97. start listening on a random port.
  98. You can retrieve this port number by calling `ranch:get_port/1`. The
  99. argument is the name of the listener you gave in `ranch:start_listener/5`.
  100. .Starting a listener for TCP connections on a random port
  101. [source,erlang]
  102. {ok, _} = ranch:start_listener(tcp_echo,
  103. ranch_tcp, [{port, 0}],
  104. echo_protocol, []
  105. ).
  106. Port = ranch:get_port(tcp_echo).
  107. === Listening on privileged ports
  108. Some systems limit access to ports below 1024 for security reasons.
  109. This can easily be identified by an `{error, eacces}` error when trying
  110. to open a listening socket on such a port.
  111. The methods for listening on privileged ports vary between systems,
  112. please refer to your system's documentation for more information.
  113. We recommend the use of port rewriting for systems with a single server,
  114. and load balancing for systems with multiple servers. Documenting these
  115. solutions is however out of the scope of this guide.
  116. === Listening on a UNIX Domain socket
  117. On UNIX systems, it is also possible to use Ranch to listen on a UNIX
  118. domain socket by specifying `{local, SocketFile}` for the `ip` socket
  119. option. In this case, the port must be set to 0 or omitted. The given
  120. file must not exist: Ranch must be able to create it.
  121. .Starting a listener for TCP connections on a UNIX Domain socket
  122. [source,erlang]
  123. {ok, _} = ranch:start_listener(tcp_echo,
  124. ranch_tcp, #{socket_opts => [
  125. {ip, {local, "/tmp/ranch_echo.sock"}},
  126. {port, 0}
  127. ]}, echo_protocol, []
  128. ).
  129. === Accepting connections on an existing socket
  130. If you want to accept connections on an existing socket, you can write
  131. a custom `ranch_transport` implementation that fetches or otherwise
  132. acquires a listening socket in the `listen/1` callback and returns it
  133. in the form of `{ok, ListenSocket}`.
  134. The custom `listen/1` function must ensure that the listener process
  135. (usually the process calling it) is also made the controlling process
  136. of the socket it returns. Failing to do so will result in stop/start
  137. and suspend/resume not working properly for that listener.
  138. === Limiting the number of concurrent connections
  139. The `max_connections` transport option allows you to limit the number
  140. of concurrent connections per connection supervisor (see below).
  141. It defaults to 1024. Its purpose is to prevent your system from being
  142. overloaded and ensuring all the connections are handled optimally.
  143. .Customizing the maximum number of concurrent connections
  144. [source,erlang]
  145. {ok, _} = ranch:start_listener(tcp_echo,
  146. ranch_tcp, [{port, 5555}, {max_connections, 100}],
  147. echo_protocol, []
  148. ).
  149. You can disable this limit by setting its value to the atom `infinity`.
  150. .Disabling the limit for the number of connections
  151. [source,erlang]
  152. {ok, _} = ranch:start_listener(tcp_echo,
  153. ranch_tcp, [{port, 5555}, {max_connections, infinity}],
  154. echo_protocol, []
  155. ).
  156. The maximum number of connections is a soft limit. In practice, it
  157. can reach `max_connections` + the number of acceptors.
  158. When the maximum number of connections is reached, Ranch will stop
  159. accepting connections. This will not result in further connections
  160. being rejected, as the kernel option allows queueing incoming
  161. connections. The size of this queue is determined by the `backlog`
  162. option and defaults to 1024. Ranch does not know about the number
  163. of connections that are in the backlog.
  164. You may not always want connections to be counted when checking for
  165. `max_connections`. For example you might have a protocol where both
  166. short-lived and long-lived connections are possible. If the long-lived
  167. connections are mostly waiting for messages, then they don't consume
  168. much resources and can safely be removed from the count.
  169. To remove the connection from the count, you must call the
  170. `ranch:remove_connection/1` from within the connection process,
  171. with the name of the listener as the only argument.
  172. .Removing a connection from the count of connections
  173. [source,erlang]
  174. ranch:remove_connection(Ref).
  175. As seen in the chapter covering protocols, this pid is received as the
  176. first argument of the protocol's `start_link/4` callback.
  177. You can modify the `max_connections` value on a running listener by
  178. using the `ranch:set_max_connections/2` function, with the name of the
  179. listener as first argument and the new value as the second.
  180. .Upgrading the maximum number of connections
  181. [source,erlang]
  182. ranch:set_max_connections(tcp_echo, MaxConns).
  183. The change will occur immediately.
  184. === Customizing the number of acceptor processes
  185. By default Ranch will use 10 acceptor processes. Their role is
  186. to accept connections and spawn a connection process for every
  187. new connection.
  188. This number can be tweaked to improve performance. A good
  189. number is typically between 10 or 100 acceptors. You must
  190. measure to find the best value for your application.
  191. .Specifying a custom number of acceptor processes
  192. [source,erlang]
  193. {ok, _} = ranch:start_listener(tcp_echo,
  194. ranch_tcp, [{port, 5555}, {num_acceptors, 42}],
  195. echo_protocol, []
  196. ).
  197. === Customizing the number of connection supervisors
  198. By default Ranch will use one connection supervisor for each
  199. acceptor process (but not vice versa). Their task is to
  200. supervise the connection processes started by an acceptor.
  201. The number of connection supervisors can be tweaked.
  202. Note that the association between the individual acceptors and
  203. connection supervisors is fixed, meaning that acceptors will
  204. always use the same connection supervisor to start connection
  205. processes.
  206. .Specifying a custom number of connection supervisors
  207. [source,erlang]
  208. {ok, _} = ranch:start_listener(tcp_echo,
  209. ranch_tcp, #{socket_opts => [{port, 5555}], num_conns_sups => 42}],
  210. echo_protocol, []
  211. ).
  212. === When running out of file descriptors
  213. Operating systems have limits on the number of sockets
  214. which can be opened by applications. When this maximum is
  215. reached the listener can no longer accept new connections. The
  216. accept rate of the listener will be automatically reduced, and a
  217. warning message will be logged.
  218. ----
  219. =ERROR REPORT==== 13-Jan-2016::12:24:38 ===
  220. Ranch acceptor reducing accept rate: out of file descriptors
  221. ----
  222. If you notice messages like this you should increase the number
  223. of file-descriptors which can be opened by your application. How
  224. this should be done is operating-system dependent. Please consult
  225. the documentation of your operating system.
  226. === Using a supervisor for connection processes
  227. Ranch allows you to define the type of process that will be used
  228. for the connection processes. By default it expects a `worker`.
  229. When the `connection_type` configuration value is set to `supervisor`,
  230. Ranch will consider that the connection process it manages is a
  231. supervisor and will reflect that in its supervision tree.
  232. Connection processes of type `supervisor` can either handle the
  233. socket directly or through one of their children. In the latter
  234. case the start function for the connection process must return
  235. two pids: the pid of the supervisor you created (that will be
  236. supervised) and the pid of the protocol handling process (that
  237. will receive the socket).
  238. Instead of returning `{ok, ConnPid}`, simply return
  239. `{ok, SupPid, ConnPid}`.
  240. It is very important that the connection process be created
  241. under the supervisor process so that everything works as intended.
  242. If not, you will most likely experience issues when the supervised
  243. process is stopped.
  244. === Upgrading
  245. Ranch allows you to upgrade the protocol options. This takes effect
  246. immediately and for all subsequent connections.
  247. To upgrade the protocol options, call `ranch:set_protocol_options/2`
  248. with the name of the listener as first argument and the new options
  249. as the second.
  250. .Upgrading the protocol options
  251. [source,erlang]
  252. ranch:set_protocol_options(tcp_echo, NewOpts).
  253. All future connections will use the new options.
  254. You can also retrieve the current options similarly by
  255. calling `ranch:get_protocol_options/1`.
  256. .Retrieving the current protocol options
  257. [source,erlang]
  258. Opts = ranch:get_protocol_options(tcp_echo).
  259. === Changing transport options
  260. Ranch allows you to change the transport options of a listener with
  261. the `ranch:set_transport_options/2` function, for example to change the
  262. number of acceptors or to make it listen on a different port.
  263. .Changing the transport options
  264. [source,erlang]
  265. ranch:set_transport_options(tcp_echo, NewOpts).
  266. You can retrieve the current transport options by calling
  267. `ranch:get_transport_options/1`.
  268. .Retrieving the current transport options
  269. [source,erlang]
  270. Opts = ranch:get_transport_options(tcp_echo).
  271. === Obtaining information about listeners
  272. Ranch provides two functions for retrieving information about the
  273. listeners, for reporting and diagnostic purposes.
  274. The `ranch:info/0` function will return detailed information
  275. about all listeners.
  276. .Retrieving detailed information
  277. [source,erlang]
  278. ranch:info().
  279. The `ranch:procs/2` function will return all acceptor or listener
  280. processes for a given listener.
  281. .Get all acceptor processes
  282. [source,erlang]
  283. ranch:procs(tcp_echo, acceptors).
  284. .Get all connection processes
  285. [source,erlang]
  286. ranch:procs(tcp_echo, connections).