pub async fn spawn_consumer<S>(
config: &StreamConfig,
state: S,
handler: EventHandler<S>,
) -> Result<JoinHandle<()>>Expand description
Spawn the Jetstream consumer as a pair of background tasks.
Returns a join handle for the reader task. The consumer architecture:
- Reader task — connects to the Jetstream WebSocket, deserializes
incoming messages into
JetstreamEvents, and sends them into a boundedmpscchannel. Reconnects with exponential backoff on error. - Dispatcher task — reads events from the channel and invokes the user-supplied handler for each one.
Backpressure: when the channel is full, the reader drops events and
increments the events_dropped metric counter.
The state parameter is an arbitrary Clone + Send + 'static value
that is forwarded to the handler on every event. In a typical atrg app
this is AppState, but the consumer itself does not depend on
atrg-core to avoid a cyclic dependency.