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