atrg_stream/lib.rs
1#![deny(unsafe_code)]
2#![warn(missing_docs)]
3//! Jetstream consumer wiring for at-rust-go.
4//!
5//! Provides a bounded, backpressure-aware Jetstream consumer that
6//! spawns as a background task and delivers events to a user-supplied handler.
7//!
8//! This crate is deliberately independent of `atrg-core` to avoid cyclic
9//! dependencies. It defines its own [`StreamConfig`] that `atrg-core` maps
10//! its `JetstreamConfig` into before calling [`spawn_consumer`].
11
12pub mod backoff;
13pub mod consumer;
14pub mod event;
15pub mod metrics;
16pub mod zstd_dict;
17
18pub use consumer::spawn_consumer;
19pub use event::JetstreamEvent;
20pub use metrics::JetstreamMetrics;
21
22use std::sync::Arc;
23
24use futures::future::BoxFuture;
25
26/// Configuration for the Jetstream consumer.
27///
28/// This mirrors the fields from `atrg-core`'s `JetstreamConfig` but lives
29/// in this crate so that `atrg-stream` has zero dependency on `atrg-core`.
30#[derive(Debug, Clone)]
31pub struct StreamConfig {
32 /// Jetstream relay host, e.g. `"jetstream1.us-east.bsky.network"`.
33 pub host: String,
34 /// NSID collections to subscribe to, e.g. `["app.bsky.feed.post"]`.
35 pub collections: Vec<String>,
36 /// Optional path or URL to a ZSTD dictionary for decompression.
37 pub zstd_dict: Option<String>,
38 /// Bounded back-pressure channel size (default: 1024).
39 pub channel_capacity: usize,
40 /// Event lag threshold before shedding/warning (default: 10_000).
41 pub max_lag_events: usize,
42}
43
44/// Type alias for event handler functions.
45///
46/// The handler receives a [`JetstreamEvent`] and a clone of whatever state
47/// object the caller supplied to [`spawn_consumer`]. The state type must be
48/// `Clone + Send + Sync + 'static`.
49pub type EventHandler<S> =
50 Arc<dyn Fn(JetstreamEvent, S) -> BoxFuture<'static, anyhow::Result<()>> + Send + Sync>;