Skip to main content

Lifecycle

RedisBroker records configuration, connect opens the pool, each subscription opens a client of its own, and shutdown unblocks and closes them.

The broker's three states as types, and what each transition does on RedisBrokerOn RedisRedisBroker::standalone(url)ConnectedRedisBrokerClosedRedisBrokerconnections_closedConfiguration only,no I/OA pool of 4 connections:publishes, settlesEach subscription:its group created,a client of its ownSubscription dropped:CLIENT UNBLOCK, QUITWaits for every readclient, QUITs the poolA publisher or delivery that outlives the connectionanswers RedisError::ShutDown at once
The diagram as text
  1. Broker: RedisBroker:: standalone(url); ConnectedRedisBroker; ClosedRedisBroker connections_closed
  2. On Redis: Configuration only, no I/O; A pool of 4 connections: publishes, settles; Each subscription: its group created, a client of its own; Subscription dropped: CLIENT UNBLOCK, QUIT; Waits for every read client, QUITs the pool
  3. RedisBroker:: standalone(url) → ConnectedRedisBroker: connect(self)
  4. ConnectedRedisBroker → ClosedRedisBroker connections_closed: shutdown(self)
  5. Configuration only, no I/O → A pool of 4 connections: publishes, settles
  6. A pool of 4 connections: publishes, settles → Each subscription: its group created, a client of its own
  7. Each subscription: its group created, a client of its own → Subscription dropped: CLIENT UNBLOCK, QUIT
  8. Subscription dropped: CLIENT UNBLOCK, QUIT → Waits for every read client, QUITs the pool
  9. A publisher or delivery that outlives the connection answers RedisError::ShutDown at once

RedisBroker::standalone records the URL and does no I/O, as do cluster and sentinel, so the service's builder stays synchronous. The runtime connects the broker when the app starts. Connect turns the topology, the credentials and the TLS settings into a fred configuration, and opens a pool of pool connections, four by default. An unreachable server fails the start. A broker built with from_pool adopts the pool it was given and dials it here, unless the service connected it already.

Publishes, settlements and the commands a handler sends go out on the pool. Each subscription opens a client of its own when it subscribes. A stream or list subscription reads on it, because a blocking read (XREADGROUP with BLOCK, BLMOVE, BRPOP) holds its connection until it returns, and a publish queued behind it would wait out the whole block interval. A Pub/Sub subscription receives its messages on it. A stream subscription creates its consumer group before it returns.

When a subscription is dropped, its client sends CLIENT UNBLOCK on fred's side connection, which ends a read still blocked on the server, and then QUIT. A blocked pop left running would take the next entry for a subscription nobody reads.

Every connection reconnects after the server drops it, backing off from 100 ms to 10 s, and a command issued meanwhile waits for it.

Shutdown marks the connection closed, waits until every read client has closed, and sends QUIT to the pool. ClosedRedisBroker reports how many pool connections it closed. A publisher, a seeker or a delivery that outlives the connection checks the mark before it sends, and answers RedisError::ShutDown at once.