qmux
An Erlang implementation of QMux (draft-ietf-quic-qmux-02): QUIC's streams, flow control, stream limits and datagrams carried over a reliable byte stream such as TLS over TCP, or over WebSocket messages (draft-lcurley-qmux-websocket-00). Applications built on QUIC streams can use it as a fallback for networks that block UDP. Its API and events mirror erlang_quic's, so an application can drive native QUIC and QMux the same way.
It needs only OTP (Erlang/OTP 29 or later). TrevRPC uses it for its TCP fallback. TrevRPC's interop suite runs its bidirectional streams against the qmux crate and TrevRPC's Go and JavaScript implementations, and all of them are checked against the same golden vectors. Unidirectional streams and datagrams are covered by this package's own tests only.
%% rebar.config
{deps, [{qmux, "~> 1.12"}]}.
gleam add qmux and {:qmux, "~> 1.12"} in mix.exs work too.
Versioning: the package has a version of its own, and is released only when it changes. QMux is still an Internet-Draft, so a new draft revision may change the API and the wire format.
Use
QMux has no ALPN identifier of its own. The application defines one per draft version, such as myapp-qmux-02, and negotiates it with TLS ALPN, or as the WebSocket subprotocol, before it starts the session.
{ok, Socket} = ssl:connect("example.com", 443, [
binary, {active, false}, {alpn_advertised_protocols, [<<"myapp-qmux-02">>]}
]),
{ok, Session, Queue} = qmux:start({qmux_ssl, Socket}, client, self(), #{
parameters => #{
max_idle_timeout => 30000,
initial_max_data => 1 bsl 20,
initial_max_stream_data_bidi_local => 256 bsl 10,
initial_max_stream_data_uni => 256 bsl 10,
initial_max_streams_uni => 16,
max_datagram_frame_size => 1200,
max_record_size => 64 bsl 10
},
keep_alive => 10000
}),
receive
{qmux, Session, {connected, _}} -> ok
end,
{ok, Stream} = qmux:open_stream(Session),
ok = qmux:send_data(Session, Queue, Stream, <<"hello">>, true).
A server does the same with server on each accepted connection, and receives {qmux, Session, {stream_opened, Id}}, {stream_data, Id, Data, Fin} and {datagram, Data} events.
- Sessions.
qmux:start/4takes the socket over (the caller must be its controlling process) and sends the owner{qmux, Session, Event}messages.close/2sends an application CONNECTION_CLOSE, and the owner's exit closes the session too. - Streams.
open_stream/1opens a bidirectional stream,open_unidirectional_stream/1a send-only one; both fail withstream_limituntil the peer allows more, and{streams_available, bidi | uni}follows when it does. The peer's streams arrive asstream_openedevents; those it opens withId band 2 =/= 0are receive-only. As over erlang_quic,send_data/5writes and finishes a stream,reset_stream/3andstop_sending/3send RESET_STREAM and STOP_SENDING. - Backpressure. Writers count what they write in the session's send queue themselves, without waiting for the session:
send_data/5fails withsend_queue_fullpastsend_queue_limit, andnotify_send_ready/1asks for asend_readyevent once it drains.send_data_async/5always queues. The session writes from a process of its own, so it reads on while the peer is slow to read. - Flow control. Streams grant credit
automatically as data is delivered, while the owner's mailbox holds no more thanmax_owner_queuemessages, ormanually for whatconsume/3returns, so an owner that stops reading stops the peer after one window.stream_flow_controlsets it for new streams andset_stream_flow_control/3for one. - Datagrams (RFC 9221) go each way only to an endpoint that set
max_datagram_frame_size: a session sends them once the peer advertised it, and accepts them if it advertised it itself, so both set it for datagrams both ways.send_datagram/2fails withdatagrams_not_supportedordatagram_too_large(seedatagram_max_size/1). As over QUIC, a datagram may be dropped: when too many wait to be sent, or when the owner is behind as it arrives. Unlike over QUIC, one that is sent is delivered reliably and in order. - Transports.
qmux_sslruns a session over TLS andqmux_tcpover plain TCP. Other byte streams implement theqmux_transportbehaviour: send, active mode, controlling process, shutdown, close and peername. - Framing. Records are Size-prefixed by default (
qmux_stream_framing). Theframingoption takes anotherqmux_framingmodule, such as one that carries each record in a binary WebSocket message: itsdecode/2parses the messages that arrive into records and returns Pongs for the peer's Pings, and itsclose/2appends a Close frame to the last record. TrevRPC's QMux over WebSocket (trevrpc_qmux_websocket.erl) works that way. - Idle timeout. As draft-ietf-quic-qmux-02 §7.1 defines it, every record received or sent restarts it, keep-alives included, so a session that only sends stays open. A peer that vanishes while the connection still takes writes is left for the underlying transport to detect.
- Errors. A session ends with
{closed, Reason}:{peer_closed, application, Code, Reason}or{peer_closed, transport, ...}for the peer's CONNECTION_CLOSE,{transport_error, Code, Reason}for a QMux protocol error,idle_timeout,handshake_timeout, orconnection_lost.
Not supported: 0-RTT, and the RESET_STREAM_AT extension (draft-02 §9.2). Neither is advertised, so peers do not use them.
Development
rebar3 eunit # the shared golden vectors in ../../testdata, and sessions against a hand-written peer over TLS and TCP
rebar3 xref
rebar3 dialyzer
nix build .#trevrpc-erlang-qmux runs all three, and the trevrpc-erlang-qmux-lint check runs erlfmt. trevrpc depends on this package through _checkouts/qmux (rebar3) and a path dependency (Gleam); when this package is released with trevrpc, flake-release publishes it to Hex first with rebar3, and otherwise trevrpc requires the version it is already released at; either way the path dependency becomes a Hex requirement.