aboutsummaryrefslogtreecommitdiffstats
path: root/doc/src/manual/ranch.asciidoc
blob: 238064665818227730f597d621496b9f8a4b1d5a (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
= ranch(3)

== Name

ranch - socket acceptor pool

== Description

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

== Types

=== max_conns() = non_neg_integer() | infinity

Maximum number of connections allowed on this listener.

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.

=== opt()

[source,erlang]
----
opt() = {ack_timeout, timeout()}
	| {connection_type, worker | supervisor}
	| {max_connections, max_conns()}
	| {num_acceptors, pos_integer()}
	| {shutdown, timeout() | brutal_kill}
	| {socket, any()}
----

Ranch-specific transport options.

These options are not passed on to the transports.
They are used by Ranch while setting up the listeners.

=== ref() = any()

Unique name used to refer to a listener.

== Option descriptions

None of the options are required.

ack_timeout (5000)::
	Maximum allowed time for the `ranch:handshake/{1,2}` call to finish.
connection_type (worker)::
	Type of process that will handle the connection.
max_connections (1024)::
	Maximum number of active connections. Soft limit. Using `infinity` will disable the limit entirely.
num_acceptors (10)::
	Number of processes that accept connections.
shutdown (5000)::
	Maximum allowed time for children to stop on listener shutdown.
socket::
	Listening socket opened externally to be used instead of calling `Transport:listen/1`.

== Exports

=== accept_ack(Ref) -> ok

This function is deprecated in favor of `ranch:handshake/1,2`.

=== child_spec(Ref, NumAcceptors, Transport, TransOpts, Protocol, ProtoOpts) -> supervisor:child_spec()

Ref = ref():: Listener name.
NumAcceptors = non_neg_integer():: Number of acceptor processes.
Transport = module():: Transport module.
TransOpts = any():: Transport options.
Protocol = module():: Protocol module.
ProtoOpts = any():: Protocol options.

Return child specifications for a new listener.

This function can be used to embed a listener directly
in an application instead of letting Ranch handle it.

=== get_addr(Ref) -> {IP, Port}

Ref = ref():: Listener name.
IP = inet:ip_address():: IP of the interface used by this listener.
Port = inet:port_number():: Port number used by this listener.

Return the IP address and port for the given listener.

=== get_max_connections(Ref) -> MaxConns

Ref = ref():: Listener name.
MaxConns = max_conns():: Current maximum number of connections.

Return the max number of connections allowed for the given listener.

=== get_port(Ref) -> Port

Ref = ref():: Listener name.
Port = inet:port_number():: Port number used by this listener.

Return the port for the given listener.

=== get_protocol_options(Ref) -> ProtoOpts

Ref = ref():: Listener name.
ProtoOpts = any():: Current protocol options.

Return the protocol options set for the given listener.

=== get_status(Ref) -> running | suspended

Ref = ref():: Listener name.

Return the status of the given listener.

=== get_transport_options(Ref) -> TransOpts

Ref = ref():: Listener name.
TransOpts = any():: Current transport options.

Return the transport options set for the given listener.

=== handshake(Ref) -> {ok, Socket}

Ref = ref():: Listener name.
Socket = any():: Initialized socket.

Acknowledge that the connection is accepted.
Returns a socket that is ready to use.

One of the `ranch:handshake/{1,2}` functions MUST be used
by a connection process to inform Ranch that it initialized
properly and let it perform any additional operations before
the socket can be safely used.

=== handshake(Ref, Opts) -> {ok, Socket}

Ref = ref():: Listener name.
Opts = any():: Initialization options.
Socket = any():: Initialized socket.

Acknowledge that the connection is accepted.
Additional options can be provided for socket initialization.
Returns a socket that is ready to use.

One of the `ranch:handshake/{1,2}` functions MUST be used
by a connection process to inform Ranch that it initialized
properly and let it perform any additional operations before
the socket can be safely used.

=== info() -> [{Ref, [{Key, Value}]}]

Ref = ref():: Listener name.
Key = atom():: Information key.
Value = any():: Information value.

Return detailed information about all Ranch listeners.

The following keys are defined:

pid:: Pid of the listener's top-level supervisor.
status:: Listener status, either running or suspended.
ip:: Interface Ranch listens on.
port:: Port number Ranch listens on.
num_acceptors:: Number of acceptor processes.
max_connections:: Maximum number of connections.
active_connections:: Number of active connections.
all_connections:: Number of connections, including those removed from the count.
transport:: Transport module.
transport_options:: Transport options.
protocol:: Protocol module.
protocol_options:: Protocol options.

=== info(Ref) -> [{Key, Value}]

Ref = ref():: Listener name.
Key = atom():: Information key.
Value = any():: Information value.

Return detailed information about a specific Ranch listener.

See `info/0` for a description of the defined keys.

=== procs(Ref, acceptors | connections) -> [pid()]

Ref = ref():: Listener name.

Return all acceptor or connection processes for one listener.

=== remove_connection(Ref) -> ok

Ref = ref():: Listener name.

Do not count this connection when limiting the number of connections.

You can use this function for long-running connection processes
which spend most of their time idling rather than consuming
resources. This allows Ranch to accept a lot more connections
without sacrificing the latency of the system.

This function may only be called from a connection process.

=== resume_listener(Ref) -> ok

Ref = ref():: Listener name.

Resume the given listener if it is suspended.
If the listener is already running, nothing will happen.

The listener will be started with the transport options
currently set for it.

=== set_max_connections(Ref, MaxConns) -> ok

Ref = ref():: Listener name.
MaxConns = max_conns():: New maximum number of connections.

Set the max number of connections for the given listener.

The change will be applied immediately. If the new value is
smaller than the previous one, Ranch will not kill the extra
connections, but will wait for them to terminate properly.

=== set_protocol_options(Ref, ProtoOpts) -> ok

Ref = ref():: Listener name.
ProtoOpts = any():: New protocol options.

Set the protocol options for the given listener.

The change will be applied immediately for all new connections.
Old connections will not receive the new options.

=== set_transport_options(Ref, TransOpts) -> ok | {error, running}

Ref = ref():: Listener name.
ProtoOpts = any():: New transport options.

Set the transport options for the given listener.

The listener must be suspended for this call to succeed.
If the listener is running, `{error, running}` will be returned.

The change will take effect when the listener is being resumed.

=== start_listener(Ref, NumAcceptors, Transport, TransOpts, Protocol, ProtoOpts) -> {ok, pid()} | {error, badarg}

Ref = ref():: Listener name.
NumAcceptors = non_neg_integer():: Number of acceptor processes.
Transport = module():: Transport module.
TransOpts = any():: Transport options.
Protocol = module():: Protocol module.
ProtoOpts = any():: Protocol options.

Start listening for connections using the given transport
and protocol. Returns the pid for this listener's supervisor.

There are additional transport options that apply
regardless of transport. They allow configuring how the
connections are supervised, rate limited and more. Please
consult the previous section for more details.

=== stop_listener(Ref) -> ok | {error, not_found}

Ref = ref():: Listener name.

Stop the given listener.

The listener is stopped gracefully, first by closing the
listening port, then by stopping the connection processes.
These processes are stopped according to the `shutdown`
transport option, which may be set to brutally kill all
connection processes or give them some time to stop properly.

This function does not return until the listener is
completely stopped.

=== suspend_listener(Ref) -> ok

Ref = ref():: Listener name.

Suspend the given listener if it is running.
If the listener is already suspended, nothing will happen.

The listener will stop listening and accepting connections by
closing the listening port, but will not stop running connection
processes.

=== wait_for_connections(Ref, Operator, NumConnections) -> ok

Ref = ref():: Listener name.
Operator = '>' | '>=' | '==' | '=<' | '<':: Comparison operator.
NumConnections = non_neg_integer():: Number of connections to wait for.

Wait until the number of connections on the given listener matches
the given operator and number of connections.