Skip to main content

pyra_streams/
config.rs

1use std::time::Duration;
2
3/// Configuration for a Redis Stream consumer.
4#[derive(Debug, Clone)]
5pub struct StreamConfig {
6    /// Redis stream key to consume from (e.g. "notifications:deposits").
7    pub stream_key: String,
8    /// Redis stream key for dead-letter messages (e.g. "notifications:deposits:dead_letter").
9    pub dead_letter_key: String,
10    /// Consumer group name (e.g. "notification-service").
11    pub consumer_group: String,
12    /// Consumer name within the group (e.g. "worker-1").
13    pub consumer_name: String,
14    /// Number of messages to read per XREADGROUP call.
15    pub batch_size: usize,
16    /// XREADGROUP block timeout in milliseconds.
17    pub block_ms: usize,
18    /// Maximum delivery attempts before moving to dead-letter.
19    pub max_retries: i64,
20    /// Minimum idle time (ms) before a pending message can be reclaimed via XCLAIM.
21    pub min_idle_ms: u64,
22    /// How often to run the pending message reclaim loop.
23    pub reclaim_interval: Duration,
24    /// Starting message ID for consumer group creation ("$" = new only, "0" = replay all).
25    pub group_start_id: String,
26    /// Field name to extract from stream entries (default: "data").
27    /// Some streams use different field names (e.g. "wallet").
28    pub data_field: String,
29}
30
31impl StreamConfig {
32    /// Create a new config with required fields and sensible defaults.
33    ///
34    /// Defaults: batch_size=10, block_ms=5000, max_retries=5, min_idle_ms=60000,
35    /// reclaim_interval=30s, group_start_id="$".
36    pub fn new(
37        stream_key: impl Into<String>,
38        dead_letter_key: impl Into<String>,
39        consumer_group: impl Into<String>,
40        consumer_name: impl Into<String>,
41    ) -> Self {
42        Self {
43            stream_key: stream_key.into(),
44            dead_letter_key: dead_letter_key.into(),
45            consumer_group: consumer_group.into(),
46            consumer_name: consumer_name.into(),
47            batch_size: 10,
48            block_ms: 5000,
49            max_retries: 5,
50            min_idle_ms: 60_000,
51            reclaim_interval: Duration::from_secs(30),
52            group_start_id: "$".to_string(),
53            data_field: "data".to_string(),
54        }
55    }
56
57    /// Set minimum idle time for pending message reclaim (milliseconds).
58    pub fn with_min_idle_ms(mut self, ms: u64) -> Self {
59        self.min_idle_ms = ms;
60        self
61    }
62
63    /// Set maximum retry count before dead-lettering.
64    pub fn with_max_retries(mut self, n: i64) -> Self {
65        self.max_retries = n;
66        self
67    }
68
69    /// Set the group start ID ("$" for new messages only, "0" for replay).
70    pub fn with_group_start_id(mut self, id: impl Into<String>) -> Self {
71        self.group_start_id = id.into();
72        self
73    }
74
75    /// Set batch size for XREADGROUP.
76    pub fn with_batch_size(mut self, n: usize) -> Self {
77        self.batch_size = n;
78        self
79    }
80
81    /// Set block timeout for XREADGROUP (milliseconds).
82    pub fn with_block_ms(mut self, ms: usize) -> Self {
83        self.block_ms = ms;
84        self
85    }
86
87    /// Set reclaim interval.
88    pub fn with_reclaim_interval(mut self, interval: Duration) -> Self {
89        self.reclaim_interval = interval;
90        self
91    }
92
93    /// Set the field name to extract from stream entries.
94    ///
95    /// Defaults to "data". Use this when the producer uses a different
96    /// field name (e.g. "wallet").
97    pub fn with_data_field(mut self, field: impl Into<String>) -> Self {
98        self.data_field = field.into();
99        self
100    }
101}