Module event_loop
Expand description
Single-connection driver.
A connection runs as one task (run_connection) that owns the
Client state machine, both stream halves, and the sans-io framing
(PacketReader) and encoding
(PacketWriter) buffers. Reading, writing, command intake, message
delivery, and keep-alive are all driven from a single select!: a stalled
write never blocks reads (no half-duplex deadlock), and a slow application
or peer turns into backpressure rather than unbounded buffering.
Because the codec keeps all parse/encode state in its buffers rather than in
a future, every select! arm is cancellation-safe and the buffers are
reused for the life of the connection. There is no per-packet or per-batch
allocation on the read or write path. See the loop in run_connection for
the per-arm cancellation reasoning.
ModulesΒ§
- aliases π
- Topic-alias bookkeeping for a single connection (Β§3.3.2.3.4).
- backoff π
- Deterministic jitter for reconnect backoff.
- connect π
- Connection establishment: transport connect, CONNECT/CONNACK handshake, and classification of broker rejections.
- inbound π
- Inbound packet processing: framing, dispatch through the client state machine, and buffering received messages for delivery.
- offline π
- Handling of user commands while the client is between connections.
- outbound π
- Outbound command handling: turning user commands and stored session state into wire packets, gated by the serverβs Receive Maximum and Maximum Packet Size.
- pending π
- Tracking and resolution of in-flight operation futures.
- write π
- Write-side helpers: the cancellation-safe write arm and the teardown delivery/flush.
StructsΒ§
- Connection
Result π - Result of a single connection attempt.
- Loop
State π - Mutable state that persists across reconnection attempts.
EnumsΒ§
- Disconnect
Reason π - Why a single connection attempt ended.
ConstantsΒ§
- MSG_
QUEUE_ πBOUND - Received messages buffered for the application before the coordinator stops reading the socket. Decouples application delivery from protocol processing: acknowledgements and keep-alive keep flowing while the application drains, and only a full buffer pauses inbound reads (which applies backpressure down to TCP).
- OFFLINE_
QUEUE_ πLIMIT - Maximum operations buffered while offline before new ones are rejected
with
Error::QueueFull, bounding memory during a long outage. - OUTBOUND_
BYTE_ πLIMIT - Soft cap, in bytes, on the pending outbound buffer; new commands are not accepted while this many encoded bytes are still waiting to be written. Bounds memory when the peer stalls. Soft because a single in-progress packet may push the buffer past the limit before intake pauses.
- READ_
BUFFER_ πCAPACITY - Initial capacity of the inbound framing buffer, reused from the handshake through the steady-state read loop.
- READ_
CHUNK π - Size of the scratch buffer each socket read fills before the bytes are fed into the framing buffer.
FunctionsΒ§
- disconnect_
packet π - enqueue π
- Encodes a packet into the pending outbound buffer for the writer task.
- jitter_
seed π - Seeds the backoff-jitter PRNG from wall-clock nanoseconds and the client id, so distinct client instances draw distinct jitter sequences.
- persist_
and_ πack - Persists pending session ops and, on success, resolves the
queued()futures the write covers. On failure both are retained for the retry after the backoff-driven reconnect. - run
- run_
connection π - shutdown_
requested π trueonceEventLoopHandlewas stopped or dropped. Consumes the signal; the caller must exit without pollingrxagain.
Type AliasesΒ§
- Outbound π
- The coordinatorβs in-place outbound encoder: packets are encoded into one reusable buffer and written from it directly, with no per-packet allocation and no per-batch handoff.