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}
27
28impl StreamConfig {
29 /// Create a new config with required fields and sensible defaults.
30 ///
31 /// Defaults: batch_size=10, block_ms=5000, max_retries=5, min_idle_ms=60000,
32 /// reclaim_interval=30s, group_start_id="$".
33 pub fn new(
34 stream_key: impl Into<String>,
35 dead_letter_key: impl Into<String>,
36 consumer_group: impl Into<String>,
37 consumer_name: impl Into<String>,
38 ) -> Self {
39 Self {
40 stream_key: stream_key.into(),
41 dead_letter_key: dead_letter_key.into(),
42 consumer_group: consumer_group.into(),
43 consumer_name: consumer_name.into(),
44 batch_size: 10,
45 block_ms: 5000,
46 max_retries: 5,
47 min_idle_ms: 60_000,
48 reclaim_interval: Duration::from_secs(30),
49 group_start_id: "$".to_string(),
50 }
51 }
52
53 /// Set minimum idle time for pending message reclaim (milliseconds).
54 pub fn with_min_idle_ms(mut self, ms: u64) -> Self {
55 self.min_idle_ms = ms;
56 self
57 }
58
59 /// Set maximum retry count before dead-lettering.
60 pub fn with_max_retries(mut self, n: i64) -> Self {
61 self.max_retries = n;
62 self
63 }
64
65 /// Set the group start ID ("$" for new messages only, "0" for replay).
66 pub fn with_group_start_id(mut self, id: impl Into<String>) -> Self {
67 self.group_start_id = id.into();
68 self
69 }
70
71 /// Set batch size for XREADGROUP.
72 pub fn with_batch_size(mut self, n: usize) -> Self {
73 self.batch_size = n;
74 self
75 }
76
77 /// Set block timeout for XREADGROUP (milliseconds).
78 pub fn with_block_ms(mut self, ms: usize) -> Self {
79 self.block_ms = ms;
80 self
81 }
82
83 /// Set reclaim interval.
84 pub fn with_reclaim_interval(mut self, interval: Duration) -> Self {
85 self.reclaim_interval = interval;
86 self
87 }
88}