Module event_loop
tokio only.Expand description
Single-connection driver.
The EventLoop runs as one task that owns the Client state
machine. Each Connection owns 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 one 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
Connection for the per-arm cancellation
reasoning.
ModulesΒ§
- aliases π
- Topic-alias bookkeeping for one 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.
- connection π
- The state of an established connection and the
select!loop that drives it. - 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, limited 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 one connection attempt.
- Endpoints π
- The channel ends the event loop serves.
- Event
Loop π - The connection event loop. It owns the client state machine and the state
that persists across reconnects, and runs one
Connectionat a time.
EnumsΒ§
- Disconnect
Reason π - Why one 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. Inbound reads pause only when the buffer is full, 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 one 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Β§
- defer π
- Resets
timerto firedelayfrom now. - enqueue π
- Encodes a packet into the pending outbound buffer for the writer task.
- enqueue_
disconnect π - Enqueues a DISCONNECT with
reason_code. Returns whether it can be flushed. - jitter_
seed π - Seeds the backoff-jitter PRNG from wall-clock nanoseconds and the client id, so distinct client instances draw distinct jitter sequences.
- shutdown_
requested π trueonceEventLoopHandlewas stopped or dropped. Consumes the signal. The caller must exit without pollingrxagain.