listeners.asciidoc 16 KB

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