aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/ranch.asciidoc
blob: af006a2a0961727de206b475aaf59a6171e8c590 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
= ranch(3)

== Name

ranch - Socket acceptor pool

== Description

The module `ranch` provides functions for starting and
manipulating Ranch listeners.

== Exports

Start/stop:

* link:man:ranch:start_listener(3)[ranch:start_listener(3)] - Start a listener
* link:man:ranch:stop_listener(3)[ranch:stop_listener(3)] - Stop a listener
* link:man:ranch:child_spec(3)[ranch:child_spec(3)] - Build child specifications for a new listener

Suspend/resume:

* link:man:ranch:suspend_listener(3)[ranch:suspend_listener(3)] - Suspend a running listener
* link:man:ranch:resume_listener(3)[ranch:resume_listener(3)] - Resume a suspended listener
* link:man:ranch:get_status(3)[ranch:get_status(3)] - Get a listener's running state

Connections:

* link:man:ranch:handshake(3)[ranch:handshake(3)] - Perform the transport handshake
* link:man:ranch:handshake_continue(3)[ranch:handshake_continue(3)] - Resume the paused transport handshake
* link:man:ranch:handshake_cancel(3)[ranch:handshake_cancel(3)] - Cancel the paused transport handshake
* link:man:ranch:recv_proxy_header(3)[ranch:recv_proxy_header(3)] - Receive the PROXY protocol header
* link:man:ranch:remove_connection(3)[ranch:remove_connection(3)] - Remove connection from the count

Options:

* link:man:ranch:get_max_connections(3)[ranch:get_max_connections(3)] - Get the max number of connections per connection supervisor
* link:man:ranch:get_protocol_options(3)[ranch:get_protocol_options(3)] - Get the current protocol options
* link:man:ranch:get_transport_options(3)[ranch:get_transport_options(3)] - Get the current transport options
* link:man:ranch:set_max_connections(3)[ranch:set_max_connections(3)] - Set the max number of connections per connection supervisor
* link:man:ranch:set_protocol_options(3)[ranch:set_protocol_options(3)] - Set the protocol options
* link:man:ranch:set_transport_options(3)[ranch:set_transport_options(3)] - Set the transport options

Introspection:

* link:man:ranch:get_addr(3)[ranch:get_addr(3)] - Get the listening address
* link:man:ranch:get_port(3)[ranch:get_port(3)] - Get the listening port
* link:man:ranch:info(3)[ranch:info(3)] - Overview of Ranch listeners
* link:man:ranch:procs(3)[ranch:procs(3)] - Retrieve pids from a listener
* link:man:ranch:wait_for_connections(3)[ranch:wait_for_connections(3)] - Wait for a specific number of connections

== Types

=== max_conns()

[source,erlang]
----
max_conns() = non_neg_integer() | infinity
----

Maximum number of connections allowed per connection supervisor.

This is a soft limit. The actual number of connections
might be slightly above the limit due to concurrency
when accepting new connections. Some connections may
also be removed from this count explicitly by the user
code.

=== opts()

[source,erlang]
----
opts() = any() | transport_opts(any())
----

Transport or socket options.

=== ref()

[source,erlang]
----
ref() = any()
----

Unique name used to refer to a listener.

=== transport_opts(SocketOpts)

[source,erlang]
----
transport_opts(SocketOpts) = #{
    alarms               => #{
                                term() => #{
                                    type := num_connections,
                                    treshold := non_neg_integer(),
                                    callback := fun((ref(), term(), pid(), [pid()]) -> any()),
                                    cooldown => non_neg_integer()
                                }
                            },
    connection_type      => worker | supervisor,
    handshake_timeout    => timeout(),
    max_connections      => max_conns(),
    logger               => module(),
    num_acceptors        => pos_integer(),
    num_conns_sups       => pos_integer(),
    post_listen_callback => fun((term()) -> ok | {error, term()}),
    shutdown             => timeout() | brutal_kill,
    socket_opts          => SocketOpts
}
----

Transport options.

The transport options are a combination of Ranch-specific
options and transport-specific socket options.

None of the options are required.

alarms (#{})::

Alarms to call a function when the number of connections under a
connection supervisor reaches or exceeds a defined treshold.
+
The map keys are the alarm names, which can be any `term`. The
associated values are the respective alarm options, again in a map
with the following keys:

type:::

Must be set to `num_connections`.

treshold:::

Treshold value, which must be a `non_neg_integer`. When the
number of connections under a connection supervisor reaches
or exceeds this value, The alarm will trigger and call
the function defined in the `callback` key (see below).

callback:::

The alarm function, which takes the listener name, the alarm
name, the pid of the connection supervisor and a list of the pids
of all connection processes under that supervisor as arguments.
The return value is ignored.

cooldown (5000):::

The minimum time after which the alarm can be triggered again,
in milliseconds.

connection_type (worker)::

Type of process that will handle the connection.

handshake_timeout (5000)::

Maximum allowed time for the `ranch:handshake/1,2` call to finish.

logger (logger)::

The module that will be used to write log messages.

max_connections (1024)::

Maximum number of active connections per connection supervisor.
Soft limit. Use `infinity` to disable the limit entirely.

num_acceptors (10)::

Number of processes that accept connections.

num_conns_sups - see below::

Number of processes that supervise connection processes.
If not specified, defaults to be equal to `num_acceptors`.

post_listen_callback (fun(_ListenSock) -> ok end)::

A function which will be called after a listen socket has been successfully
created, with the socket as argument. It can be used to perform any
necessary setup steps on the socket.
+
If the callback function returns `ok`, the listener will start accepting
connections on the socket. If it returns `{error, Reason}`, the listener
will fail to start.

shutdown (5000)::

Maximum allowed time for children to stop on listener shutdown.

socket_opts::

Socket options to be used by `Transport:listen/1`. Please refer to the
documentation of the transport module you are using for more details.

== Changelog

* *2.0*: The type `transport_opts(SocketOpts)` was added.
* *2.0*: The function `ranch:accept_ack/1` was removed in favor of
         link:man:ranch:handshake(3)[ranch:handshake(3)].
* *2.0*: The option `max_connections` is now per connection supervisor.
* *2.0*: The `num_conns_sup` option was added.
* *2.0*: The `socket` option was removed.
* *2.0*: The `logger` option is no longer experimental. It now defaults
         to `logger` instead of `error_logger`.
* *2.0*: The `opt()` type was removed.
* *1.6*: The experimental `logger` option was added.
* *1.6*: The `opt()` type was deprecated in favor of the new `opts()` type.

== See also

link:man:ranch(7)[ranch(7)]