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()

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:accept_ack/1 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

Ref = ref()

Listener name.

Acknowledge that the connection is accepted.

This function 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.

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.

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.

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.

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.

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.

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.

Ranch 1.5 Function Reference

Navigation

Version select

Like my work? Donate!

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.