Lifecycle
KafkaBroker::new records configuration, connect
probes the cluster, startup opens a consumer per subscription, and shutdown flushes.
The diagram as text
- Broker: KafkaBroker::new (servers); ConnectedKafkaBroker; ClosedKafkaBroker unflushed_records()
- On Kafka: Configuration only, no I/O; Metadata probe within connect_timeout; Each subscription: a consumer, joined and assigned its partitions; The first publish opens the producer; Flush in-flight records within flush_timeout
- KafkaBroker::new (servers) → ConnectedKafkaBroker: connect(self)
- ConnectedKafkaBroker → ClosedKafkaBroker unflushed_records(): shutdown(self)
- Configuration only, no I/O → Metadata probe within connect_timeout
- Metadata probe within connect_timeout → Each subscription: a consumer, joined and assigned its partitions
- Each subscription: a consumer, joined and assigned its partitions → The first publish opens the producer
- The first publish opens the producer → Flush in-flight records within flush_timeout
- A publisher that outlives the connection answers KafkaError::Closed, never a silent success
new does no I/O, so the service's builder stays synchronous. The runtime connects the broker when
the app starts. Connect fetches the cluster's metadata through a short-lived client built from the
producer settings, within connect_timeout
(30 seconds). An unreachable cluster or a producer property librdkafka refuses fails the start.
Each subscription's descriptor resolves once, against the connected broker. A group subscription
opens its consumer and waits until the group has assigned it partitions and the start of each is
known, up to assignment_timeout (one
minute). Until the runtime starts reading, the consumer keeps answering the group, so a rebalance
does not wait for it. A subscription that names no group, under a broker with no
default_group, fails the start.
A service that only consumes runs no producer. The first publish opens the shared one.
Shutdown marks the connection closed, then flushes what the producer still holds within
flush_timeout (30 seconds).
ClosedKafkaBroker reports the records the flush left
behind. A consumer closes when its subscription is dropped: it leaves the group, and unless its
subscription is transactional it commits its stored position on the way.