Skip to main content

spawn_consumer

Function spawn_consumer 

Source
pub async fn spawn_consumer<S>(
    config: &StreamConfig,
    state: S,
    handler: EventHandler<S>,
) -> Result<JoinHandle<()>>
where S: Clone + Send + Sync + 'static,
Expand description

Spawn the Jetstream consumer as a pair of background tasks.

Returns a join handle for the reader task. The consumer architecture:

  1. Reader task — connects to the Jetstream WebSocket, deserializes incoming messages into JetstreamEvents, and sends them into a bounded mpsc channel. Reconnects with exponential backoff on error.
  2. 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.