atrg-stream
Bounded, backpressure-aware Jetstream consumer for AT Protocol event streaming.
Part of at-rust-go (atrg) — a batteries-included AT Protocol backend framework for Rust.
What this crate provides
spawn_consumer— spawns a backgroundtokiotask that connects to an AT Protocol Jetstream relay and delivers events through a bounded channelJetstreamEvent— re-exported event type from the Jetstream protocolJetstreamMetrics— runtime metrics: events received, events dropped, reconnects, current backoffStreamConfig— standalone configuration (no dependency onatrg-core) for relay host, collections, channel capacity, lag thresholds, and optional ZSTD dictionaryEventHandler<S>— type alias for async event handler closures that receive an event and a clone of your app state- Built-in backpressure — events flow through a
tokio::sync::mpscchannel; when the handler falls behind, the consumer pauses WebSocket reads rather than buffering unbounded - Lag detection & shedding — warns via
tracingwhen the channel reachesmax_lag_eventsand drops the oldest events to keep memory bounded - ZSTD dictionary support — auto-fetches and caches dictionaries from a URL, or loads from a local path
- Automatic reconnection with exponential backoff
Usage
[]
= "0.1"
= { = "1", = ["full"] }
= "1"
use ;
use Arc;
async
Design notes
This crate is deliberately independent of atrg-core to avoid cyclic dependencies. It defines its own StreamConfig that atrg-core maps from its JetstreamConfig before calling spawn_consumer. Events are delivered in arrival order per consumer connection.
When used via the atrg-core AtrgApp builder, all of this wiring is automatic — just add [jetstream] to your atrg.toml and register a handler with .on_event().
License
LGPL-3.0-only — see LICENSE.