Skip to main content

Lifecycle

ZmqQueue::new records the endpoint, connect checks it, each subscription and each publisher on the bind side attach a socket at startup, a publisher that dials on its first publish. ZmqFanout and ZmqRpc go through the same steps.

The broker's three states as types, and what each transition does on ZeroMQBrokerOn ZeroMQZmqQueue::new(endpoint)ConnectedZmqQueueClosed = ()Endpoint recorded,no I/OAddress checked:tcp:// or ipc://Each subscriptionbinds or dialsa socket of its ownPublishers attach:bind side at startup,dial side on first publishMarked closed,nothing to flushA publisher that outlives the connection answersZmqError::NotConnected, never a silent successA dropped subscription stops its taskand frees the endpoint it bound
The diagram as text
  1. Broker: ZmqQueue::new (endpoint); ConnectedZmqQueue; Closed = ()
  2. On ZeroMQ: Endpoint recorded, no I/O; Address checked: tcp:// or ipc://; Each subscription binds or dials a socket of its own; Publishers attach: bind side at startup, dial side on first publish; Marked closed, nothing to flush
  3. ZmqQueue::new (endpoint) → ConnectedZmqQueue: connect(self)
  4. ConnectedZmqQueue → Closed = (): shutdown(self)
  5. Endpoint recorded, no I/O → Address checked: tcp:// or ipc://
  6. Address checked: tcp:// or ipc:// → Each subscription binds or dials a socket of its own
  7. Each subscription binds or dials a socket of its own → Publishers attach: bind side at startup, dial side on first publish
  8. Publishers attach: bind side at startup, dial side on first publish → Marked closed, nothing to flush
  9. A publisher that outlives the connection answers ZmqError::NotConnected, never a silent success
  10. A dropped subscription stops its task and frees the endpoint it bound

new does no I/O, so the service's builder stays synchronous. The runtime connects the broker when the app starts. Connect checks that the address is tcp:// or ipc:// and opens no socket. Another scheme fails the start, with an error naming the address.

Each subscription attaches its socket as it opens. On an endpoint this service binds, the subscription listens there, and a bind the system refuses fails the start. One subscription holds a bound endpoint: a second one on the same broker is refused with an error naming both. On an endpoint this service connects to, the subscription dials it. Its peer is the sending end of the pattern, so from then on a publish through the same broker is refused before it dials.

A service that only consumes attaches no publisher socket. A publisher on the bind side attaches its own at startup, one that dials on its first publish, and a send retries for up to five seconds while the connection to its peer finishes the handshake.

Shutdown marks the connection closed and returns. Nothing is buffered in this crate, so nothing is flushed, and the closed form carries no report. A publisher handed out earlier answers ZmqError::NotConnected from then on.

A subscription stops when the runtime drops it. Dropping it stops the task that reads its socket, since a receive on a socket with no peer never returns by itself. A subscription that bound its endpoint frees it at that moment, so a new one can bind the same address.