Teals.Ranch (teals v0.1.0)

Copy Markdown View Source

A Ranch transport for Teals sockets: the server side of :ranch_transport, on TCP or Unix domain listeners.

Give the configuration from Teals.config/1 as config in the listener's socket_opts, beside the usual :ranch_tcp options:

{:ok, config} = Teals.config(certfile: "server.pem", keyfile: "key.pem")

:ranch.start_listener(:api, Teals.Ranch,
  %{socket_opts: [port: 8443, config: config]},
  :cowboy_clear, %{
    env: %{dispatch: dispatch},
    stream_handlers: [Teals.Cowboy.StreamHandler, :cowboy_stream_h]
  })

Under Cowboy, Teals.Cowboy.StreamHandler makes the client certificate readable through :cowboy_req.cert/1 and Plug.Conn.get_peer_data/1.

A Unix domain listener takes ip: {:local, path} and port: 0.

Listening and accepting are :ranch_tcp's, with its option defaults: the TCP socket beneath each connection has send_timeout 30 s with send_timeout_close, so writing fails if the peer reads nothing for that long. Teals.setopts/2's send_timeout is separate and applies to each send.

The handshake runs in handshake/2 (:ranch.handshake/1,2), which returns a Teals.Socket; the protocol process becomes its owner. A failed handshake closes the connection, and the listener keeps accepting. recv_proxy_header/2 reads a PROXY protocol header before it.

messages/0 returns {:teals, :teals_closed, :teals_error, :teals_passive}; the owner also gets {:teals_read_closed, socket} when the peer ends writing first (see Teals).

setopts/2 takes the options Teals.setopts/2 takes (active, send_timeout, send_timeout_close, half_close and buffer), and accepts and ignores linger and exit_on_close, so that a protocol setting zero linger while it closes cannot drop data already sent. Any other option returns {:error, :einval}. getopts/2 supports none.

Client-side callbacks return {:error, :not_supported}.

To change the TLS configuration of a running listener, suspend it, set transport options with the new config and resume it; connections already accepted keep the configuration they started with.