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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
use crate::common::model::config::RedisConfig;
use crate::errors::Result;
use crate::errors::error::QueueError;
use crate::queue::{
AckAction, HEADER_ATTEMPT, HEADER_NACK_REASON, Message, MqBackend, NackDisposition, NackPolicy,
decide_nack, parse_attempt,
};
use async_trait::async_trait;
use deadpool_redis::redis;
use deadpool_redis::redis::{AsyncCommands, FromRedisValue};
use log::{debug, error, info, warn};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;
use tokio::sync::mpsc;
struct StreamRouter {
routes: HashMap<String, mpsc::Sender<Message>>,
}
pub struct RedisQueue {
pool: deadpool_redis::Pool,
group_id: String,
consumer_name: String,
minid_time: u64,
namespace: String,
shards: usize,
batch_size: usize,
claim_min_idle: u64,
claim_count: usize,
claim_interval: u64,
listener_count: usize,
router: Arc<RwLock<StreamRouter>>,
ack_tx: mpsc::Sender<(String, AckAction)>,
}
impl RedisQueue {
pub fn new(
redis_config: &RedisConfig,
minid_time: u64,
namespace: &str,
batch_size: usize,
nack_policy: NackPolicy,
) -> Result<Self> {
let pool = crate::utils::connector::create_redis_pool(
&redis_config.redis_host,
redis_config.redis_port,
redis_config.redis_db,
&redis_config.redis_username,
&redis_config.redis_password,
redis_config.pool_size,
redis_config.tls.unwrap_or(false),
)
.ok_or(QueueError::ConnectionFailed)?;
let consumer_name = uuid::Uuid::new_v4().to_string();
// Use a default group ID or maybe pass it in?
// The previous implementation didn't use group ID for lists, but streams need it.
// I'll use a default one or maybe "crawler_group".
let group_id = format!("{}:crawler_group", namespace);
let shards = redis_config.shards.unwrap_or(8);
let claim_min_idle = redis_config.claim_min_idle.unwrap_or(60000); // Default 60s
let claim_count = redis_config.claim_count.unwrap_or(100); // Default 100
let claim_interval = redis_config.claim_interval.unwrap_or(60000); // Default 60s
let listener_count = redis_config.listener_count.unwrap_or(8);
// Create shared ACK channel (Buffer size 10000 to handle high throughput)
let (ack_tx, ack_rx) = mpsc::channel::<(String, AckAction)>(10000);
let router = Arc::new(RwLock::new(StreamRouter {
routes: HashMap::new(),
}));
let queue = Self {
pool: pool.clone(),
group_id: group_id.clone(),
consumer_name: consumer_name.clone(),
minid_time,
namespace: namespace.to_string(),
shards,
batch_size,
claim_min_idle,
claim_count,
claim_interval,
listener_count,
ack_tx,
router,
};
// Spawn shared components
queue.spawn_ack_processor(pool.clone(), group_id.clone(), ack_rx, nack_policy);
queue.spawn_shared_claimer();
queue.spawn_shared_lag_monitor();
queue.spawn_sharded_listeners();
Ok(queue)
}
fn extract_shard_id(key: &str) -> Option<usize> {
let key = if key.starts_with('{') && key.ends_with('}') {
&key[1..key.len() - 1]
} else {
key
};
if let Some(last_colon) = key.rfind(':') {
key[last_colon + 1..].parse::<usize>().ok()
} else {
None
}
}
fn spawn_sharded_listeners(&self) {
let listener_count = self.listener_count;
for i in 0..listener_count {
let pool = self.pool.clone();
let group_id = self.group_id.clone();
// Use distinct consumer name for observability, though they handle disjoint key sets
let consumer_name = format!("{}-{}", self.consumer_name, i);
let router = self.router.clone();
let batch_size = self.batch_size;
let ack_tx = self.ack_tx.clone();
let listener_index = i;
tokio::spawn(async move {
info!(
"Starting Sharded Redis Listener {}/{} (Group: {}, Consumer: {})",
listener_index + 1,
listener_count,
group_id,
consumer_name
);
let mut conn: Option<deadpool_redis::Connection> = None;
loop {
// 1. Snapshot and Filter Routes
let routes = {
let r = router.read().await;
r.routes.clone()
};
let keys: Vec<String> = routes
.keys()
.filter(|k| {
// Try to extract shard ID from the key (format: ...:shard_id)
if let Some(shard_id) = Self::extract_shard_id(k) {
return shard_id % listener_count == listener_index;
}
// Fallback to hashing for non-sharded keys or parsing failures
let mut hasher = crc32fast::Hasher::new();
hasher.update(k.as_bytes());
(hasher.finalize() as usize) % listener_count == listener_index
})
.cloned()
.collect();
if keys.is_empty() {
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
let ids: Vec<&str> = vec![">"; keys.len()];
// 2. Get Connection
if conn.is_none() {
match pool.get().await {
Ok(c) => conn = Some(c),
Err(e) => {
error!(
"Listener {} failed to get connection: {}. Retrying...",
listener_index, e
);
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
}
}
let active_conn = match conn.as_mut() {
Some(c) => c,
None => {
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
};
// 3. XREADGROUP
let opts = redis::streams::StreamReadOptions::default()
.group(&group_id, &consumer_name)
.block(2000)
.count(batch_size);
let result: redis::RedisResult<redis::streams::StreamReadReply> =
active_conn.xread_options(&keys, &ids, &opts).await;
match result {
Ok(reply) => {
for stream_key in reply.keys {
let s_key = stream_key.key;
if let Some(sender) = routes.get(&s_key) {
for stream_id in stream_key.ids {
let id = stream_id.id.clone();
if let Some(val) = stream_id.map.get("payload")
&& let Ok(data) = Vec::<u8>::from_redis_value(val)
{
let mut headers = HashMap::new();
for (k, v) in &stream_id.map {
if k.starts_with("h:")
&& let Ok(s) = String::from_redis_value(v)
{
headers.insert(k[2..].to_string(), s);
}
}
let msg = Message {
payload: std::sync::Arc::new(data),
id: format!("{}@{}", s_key, id),
headers: std::sync::Arc::new(headers),
ack_tx: ack_tx.clone(),
};
crate::common::metrics::inc_throughput(
"queue", "consumer", "consume", "success", 1,
);
if sender.send(msg).await.is_err() {
warn!("Subscriber for {} dropped", s_key);
}
}
}
}
}
}
Err(e) => {
if let Some(code) = e.code()
&& code == "NOGROUP"
{
// NOGROUP usually handled by subscribe ensuring group creation.
}
error!("Error in listener {}: {}. Retrying...", listener_index, e);
conn = None;
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
}
});
}
}
fn spawn_ack_processor(
&self,
pool: deadpool_redis::Pool,
group_id: String,
mut ack_rx: mpsc::Receiver<(String, AckAction)>,
nack_policy: NackPolicy,
) {
tokio::spawn(async move {
let mut batches: HashMap<String, Vec<String>> = HashMap::new();
let mut pending_count: usize = 0;
let mut interval = tokio::time::interval(Duration::from_millis(100));
loop {
tokio::select! {
Some((id, action)) = ack_rx.recv() => {
let (stream_key, real_id) = match id.split_once('@') {
Some((k, i)) => (k.to_string(), i.to_string()),
None => {
error!("Invalid ID format in ACK: {}", id);
continue;
}
};
match action {
AckAction::Ack => {
batches.entry(stream_key).or_default().push(real_id);
pending_count += 1;
}
AckAction::Nack(reason, payload, headers) => {
let attempt = parse_attempt(&headers);
let disposition = decide_nack(nack_policy, attempt);
let action_label = match disposition {
NackDisposition::Retry { .. } => "retry",
NackDisposition::Dlq => "dlq",
};
crate::common::metrics::inc_throughput("queue", "ack_processor", "nack_policy", action_label, 1);
if let Ok(mut ack_conn) = pool.get().await {
if let NackDisposition::Retry { next_attempt } = disposition {
if nack_policy.backoff_ms > 0 {
tokio::time::sleep(Duration::from_millis(nack_policy.backoff_ms)).await;
}
let mut header_map = (*headers).clone();
header_map.insert(HEADER_ATTEMPT.to_string(), next_attempt.to_string());
header_map.insert(HEADER_NACK_REASON.to_string(), reason.clone());
let mut cmd = redis::cmd("XADD");
cmd.arg(&stream_key).arg("*").arg("payload").arg(payload.as_slice());
for (k, v) in &header_map {
cmd.arg(format!("h:{}", k)).arg(v);
}
let add_res: redis::RedisResult<String> = cmd.query_async(&mut ack_conn).await;
if let Err(e) = add_res {
error!("Failed to requeue message to {}: {}", stream_key, e);
}
let _: redis::RedisResult<()> = redis::cmd("XACK")
.arg(&stream_key)
.arg(&group_id)
.arg(&real_id)
.query_async(&mut ack_conn)
.await;
crate::common::metrics::inc_throughput("queue", "ack_processor", "retry", "success", 1);
} else {
let dlq_key = format!("{}:dlq", stream_key);
let script = redis::Script::new(r"
redis.call('XADD', KEYS[3], '*', 'payload', ARGV[1], 'reason', ARGV[2], 'original_id', ARGV[3], 'attempt', ARGV[4])
return redis.call('XACK', KEYS[1], KEYS[2], ARGV[3])
");
let _: () = script
.key(&stream_key)
.key(&group_id)
.key(&dlq_key)
.arg(payload.as_slice())
.arg(&reason)
.arg(&real_id)
.arg(attempt.to_string())
.invoke_async(&mut ack_conn)
.await
.map_err(|e| error!("Failed to atomic NACK: {}", e))
.unwrap_or(());
crate::common::metrics::inc_throughput("queue", "ack_processor", "dlq", "success", 1);
}
}
}
}
if pending_count >= 50
&& Self::flush_acks(&pool, &group_id, &mut batches).await {
pending_count = 0;
}
}
_ = interval.tick() => {
if !batches.is_empty()
&& Self::flush_acks(&pool, &group_id, &mut batches).await {
pending_count = 0;
}
}
else => break,
}
}
});
}
async fn flush_acks(
pool: &deadpool_redis::Pool,
group_id: &str,
batches: &mut HashMap<String, Vec<String>>,
) -> bool {
if batches.is_empty() {
return true;
}
match pool.get().await {
Ok(mut ack_conn) => {
let mut pipeline = redis::pipe();
let mut count_map = HashMap::new();
for (s_key, ids) in batches.iter() {
if ids.is_empty() {
continue;
}
pipeline.xack(s_key, group_id, ids).ignore();
count_map.insert(s_key.clone(), ids.len());
}
let result: redis::RedisResult<()> = pipeline.query_async(&mut ack_conn).await;
match result {
Ok(_) => {
for (_k, v) in count_map {
crate::common::metrics::inc_throughput(
"queue",
"ack_processor",
"ack",
"success",
v as u64,
);
}
batches.clear();
true
}
Err(e) => {
error!("Failed to flush ACKs: {}", e);
// Do not clear batches, retry next time
false
}
}
}
Err(e) => {
error!(
"Failed to get Redis connection for ACK flush: {}. Retrying next tick.",
e
);
// Do NOT clear batches, retry next time
false
}
}
}
fn spawn_shared_lag_monitor(&self) {
let pool = self.pool.clone();
let router = self.router.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(15));
loop {
interval.tick().await;
let topic_keys: Vec<String> = {
let r = router.read().await;
r.routes.keys().cloned().collect()
};
if topic_keys.is_empty() {
continue;
}
if let Ok(mut conn) = pool.get().await {
let mut totals: HashMap<String, u64> = HashMap::new();
for t_key in &topic_keys {
let len: u64 = redis::cmd("XLEN")
.arg(t_key)
.query_async(&mut conn)
.await
.unwrap_or(0);
let label = Self::topic_label_from_key(t_key);
*totals.entry(label).or_insert(0) += len;
}
for (topic_label, total_len) in totals {
crate::common::metrics::set_backlog(
"queue",
&topic_label,
total_len as f64,
);
if total_len > 1000 {
warn!("High queue depth for {}: {}", topic_label, total_len);
} else if total_len > 0 {
debug!("Queue depth for {}: {}", topic_label, total_len);
}
}
}
}
});
}
fn spawn_shared_claimer(&self) {
let pool = self.pool.clone();
let router = self.router.clone();
let group_id = self.group_id.clone();
let consumer_name = format!("{}-claimer", self.consumer_name);
let ack_tx = self.ack_tx.clone();
let claim_min_idle = self.claim_min_idle;
let claim_count = self.claim_count;
let claim_interval = self.claim_interval;
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_millis(claim_interval));
loop {
interval.tick().await;
let routes: Vec<(String, mpsc::Sender<Message>)> = {
let r = router.read().await;
r.routes
.iter()
.map(|(k, s)| (k.clone(), s.clone()))
.collect()
};
if routes.is_empty() {
continue;
}
let mut conn = match pool.get().await {
Ok(c) => c,
Err(e) => {
error!("Claimer connection failed: {}", e);
continue;
}
};
for (topic_claim, sender) in routes {
// Loop to drain all stuck messages if there are more than claim_count
loop {
let result: redis::RedisResult<(
String,
Vec<(String, Vec<Vec<u8>>)>,
Vec<String>,
)> = redis::cmd("XAUTOCLAIM")
.arg(topic_claim.as_str())
.arg(&group_id)
.arg(&consumer_name)
.arg(claim_min_idle)
.arg("0-0")
.arg("COUNT")
.arg(claim_count)
.query_async(&mut conn)
.await;
match result {
Ok((cursor, messages, _deleted)) => {
let is_empty = messages.is_empty();
if !is_empty {
info!(
"Claimed {} stuck messages from topic {}",
messages.len(),
topic_claim
);
crate::common::metrics::inc_throughput(
"queue",
"claimer",
"claim",
"success",
messages.len() as u64,
);
for (id, fields) in messages {
if let Some(msg) = Self::parse_message(
&topic_claim,
id,
fields,
ack_tx.clone(),
) {
if let Err(e) = sender.send(msg).await {
warn!("Failed to send claimed message: {}", e);
}
}
}
}
if cursor == "0-0" || is_empty {
break;
}
}
Err(e) => {
warn!("XAUTOCLAIM check failed for {}: {}", topic_claim, e);
break;
}
}
}
}
}
});
}
fn topic_label_from_key(key: &str) -> String {
let trimmed = if key.starts_with('{') && key.ends_with('}') {
&key[1..key.len() - 1]
} else {
key
};
let parts: Vec<&str> = trimmed.split(':').collect();
if parts.len() >= 2 {
parts[1].to_string()
} else {
trimmed.to_string()
}
}
fn get_topic_key(&self, topic: &str, key: Option<&str>) -> String {
if self.shards > 1 {
let shard = if let Some(k) = key {
let mut hasher = crc32fast::Hasher::new();
hasher.update(k.as_bytes());
(hasher.finalize() as usize) % self.shards
} else {
(rand::random::<u64>() as usize) % self.shards
};
format!("{{{}:{}:{}}}", self.namespace, topic, shard)
} else {
format!("{{{}:{}}}", self.namespace, topic)
}
}
fn get_minid_threshold(&self) -> Option<String> {
if self.minid_time > 0 {
let retention_ms = (self.minid_time as u128) * 60 * 60 * 1000;
let now_ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
let threshold = now_ms.saturating_sub(retention_ms);
Some(threshold.to_string())
} else {
None
}
}
async fn get_connection(&self) -> Result<deadpool_redis::Connection> {
self.pool
.get()
.await
.map_err(|_| QueueError::ConnectionFailed.into())
}
fn parse_message(
topic_claim: &str,
id: String,
fields: Vec<Vec<u8>>,
ack_tx: mpsc::Sender<(String, AckAction)>,
) -> Option<Message> {
let mut found_payload = None;
let mut headers = HashMap::new();
let mut iter = fields.into_iter();
while let Some(key) = iter.next() {
if let Some(val) = iter.next() {
if key == b"payload" {
found_payload = Some(val);
} else if key.starts_with(b"h:") {
// Optimized header parsing to avoid intermediate allocations
// "h:key" -> key
if key.len() > 2 {
let mut k_vec = key;
k_vec.drain(0..2);
if let Ok(k_str) = String::from_utf8(k_vec) {
if let Ok(v_str) = String::from_utf8(val) {
headers.insert(k_str, v_str);
}
}
}
}
}
}
found_payload.map(|payload| Message {
payload: std::sync::Arc::new(payload),
id: format!("{}@{}", topic_claim, id),
headers: std::sync::Arc::new(headers),
ack_tx,
})
}
}
#[async_trait]
impl MqBackend for RedisQueue {
async fn publish_with_headers(
&self,
topic: &str,
key: Option<&str>,
payload: &[u8],
headers: &HashMap<String, String>,
) -> Result<()> {
let mut conn = self.get_connection().await?;
let topic_key = self.get_topic_key(topic, key);
let mut cmd = redis::cmd("XADD");
cmd.arg(topic_key);
if let Some(threshold) = self.get_minid_threshold() {
cmd.arg("MINID").arg("~").arg(threshold);
}
cmd.arg("*").arg("payload").arg(payload);
for (k, v) in headers {
cmd.arg(format!("h:{}", k)).arg(v);
}
let _: String = cmd
.query_async(&mut conn)
.await
.map_err(|e| QueueError::PushFailed(Box::new(e)))?;
crate::common::metrics::inc_throughput("queue", "producer", "publish", "success", 1);
Ok(())
}
async fn publish_batch(&self, topic: &str, items: &[(Option<String>, Vec<u8>)]) -> Result<()> {
if items.is_empty() {
return Ok(());
}
let mut conn = self.get_connection().await?;
let mut batches: HashMap<String, Vec<&Vec<u8>>> = HashMap::new();
for (key, payload) in items {
let topic_key = self.get_topic_key(topic, key.as_deref());
batches.entry(topic_key).or_default().push(payload);
}
let minid_threshold = self.get_minid_threshold().unwrap_or_default();
let script = redis::Script::new(
r"
local key = KEYS[1]
local minid = KEYS[2]
for i, payload in ipairs(ARGV) do
if minid ~= '' then
redis.call('XADD', key, 'MINID', '~', minid, '*', 'payload', payload)
else
redis.call('XADD', key, '*', 'payload', payload)
end
end
",
);
for attempt in 0..2 {
let mut pipe = redis::pipe();
for (topic_key, payloads) in &batches {
let mut invocation = script.key(topic_key);
invocation.key(&minid_threshold);
for p in payloads {
invocation.arg(p.as_slice());
}
pipe.invoke_script(&invocation);
}
let result: redis::RedisResult<()> = pipe.query_async(&mut conn).await;
match result {
Ok(_) => {
crate::common::metrics::inc_throughput(
"queue",
"producer",
"publish_batch",
"success",
items.len() as u64,
);
return Ok(());
}
Err(e) => {
let is_noscript = e.kind() == redis::ErrorKind::NoScriptError
|| e.to_string().contains("NOSCRIPT");
if is_noscript && attempt == 0 {
let _ = script
.key("")
.key("")
.arg("")
.invoke_async::<()>(&mut conn)
.await;
continue;
}
return Err(QueueError::PushFailed(Box::new(e)).into());
}
}
}
Ok(())
}
async fn publish_batch_with_headers(
&self,
topic: &str,
items: &[(Option<String>, Vec<u8>, HashMap<String, String>)],
) -> Result<()> {
if items.is_empty() {
return Ok(());
}
let mut conn = self.get_connection().await?;
let mut batches: HashMap<String, Vec<(&Vec<u8>, &HashMap<String, String>)>> =
HashMap::new();
for (key, payload, headers) in items {
let topic_key = self.get_topic_key(topic, key.as_deref());
batches
.entry(topic_key)
.or_default()
.push((payload, headers));
}
let minid_threshold = self.get_minid_threshold().unwrap_or_default();
let script = redis::Script::new(
r"
local key = KEYS[1]
local minid = KEYS[2]
for i = 1, #ARGV, 2 do
local payload = ARGV[i]
local headers_str = ARGV[i+1]
local args = {'XADD', key}
if minid ~= '' then
table.insert(args, 'MINID')
table.insert(args, '~')
table.insert(args, minid)
end
table.insert(args, '*')
table.insert(args, 'payload')
table.insert(args, payload)
if headers_str ~= '' then
local headers = cjson.decode(headers_str)
for k, v in pairs(headers) do
table.insert(args, 'h:' .. k)
table.insert(args, v)
end
end
redis.call(unpack(args))
end
",
);
for attempt in 0..2 {
let mut pipe = redis::pipe();
for (topic_key, item_list) in &batches {
let mut invocation = script.key(topic_key);
invocation.key(&minid_threshold);
for (p, h) in item_list {
invocation.arg(p.as_slice());
if h.is_empty() {
invocation.arg("");
} else {
let h_json = serde_json::to_string(h).unwrap_or_default();
invocation.arg(h_json);
}
}
pipe.invoke_script(&invocation);
}
let result: redis::RedisResult<()> = pipe.query_async(&mut conn).await;
match result {
Ok(_) => {
crate::common::metrics::inc_throughput(
"queue",
"producer",
"publish_batch",
"success",
items.len() as u64,
);
return Ok(());
}
Err(e) => {
let is_noscript = e.kind() == redis::ErrorKind::NoScriptError
|| e.to_string().contains("NOSCRIPT");
if is_noscript && attempt == 0 {
let _ = script
.key("")
.key("")
.arg("")
.arg("")
.invoke_async::<()>(&mut conn)
.await;
continue;
}
return Err(QueueError::PushFailed(Box::new(e)).into());
}
}
}
Ok(())
}
async fn subscribe(&self, topic: &str, sender: mpsc::Sender<Message>) -> Result<()> {
let topic = topic.to_string();
let topic_keys: Vec<String> = if self.shards > 1 {
(0..self.shards)
.map(|i| format!("{{{}:{}:{}}}", self.namespace, topic, i))
.collect()
} else {
vec![format!("{{{}:{}}}", self.namespace, topic)]
};
let group_id = self.group_id.clone();
// let consumer_name = self.consumer_name.clone(); // Not used here anymore
// Ensure Groups Exist & Register Routes
{
let mut conn = self
.pool
.get()
.await
.map_err(|_e| QueueError::ConnectionFailed)?;
let mut router = self.router.write().await;
for t_key in &topic_keys {
// Register route
router.routes.insert(t_key.clone(), sender.clone());
// Ensure group exists
match conn
.xgroup_create_mkstream::<&str, &str, &str, ()>(t_key, &group_id, "$")
.await
{
Ok(_) => info!("Created consumer group {} for topic {}", group_id, t_key),
Err(e) => {
if e.code() == Some("BUSYGROUP") {
// Group already exists, which is fine.
} else {
error!("Failed to create consumer group for {}: {}", t_key, e);
}
}
}
}
}
Ok(())
}
async fn clean_storage(&self) -> Result<()> {
if self.minid_time == 0 {
return Ok(());
}
let mut conn = self
.pool
.get()
.await
.map_err(|_| QueueError::ConnectionFailed)?;
let retention_period = self.minid_time as u128 * 60 * 60 * 1000;
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::ZERO)
.as_millis();
let min_id = now.saturating_sub(retention_period);
info!(
"Starting Redis storage cleanup for namespace {}, min_id: {}",
self.namespace, min_id
);
let pattern = format!("{}:*", self.namespace);
let mut keys: Vec<String> = Vec::new();
{
let mut cursor = 0;
loop {
let (next_cursor, batch): (u64, Vec<String>) = redis::cmd("SCAN")
.cursor_arg(cursor)
.arg("MATCH")
.arg(&pattern)
.arg("COUNT")
.arg(100)
.query_async(&mut conn)
.await
.map_err(|e| QueueError::OperationFailed(Box::new(e)))?;
keys.extend(batch);
cursor = next_cursor;
if cursor == 0 {
break;
}
}
}
// Batch check types
if keys.is_empty() {
return Ok(());
}
let mut pipe = redis::pipe();
for key in &keys {
pipe.cmd("TYPE").arg(key);
}
let types: Vec<String> = pipe
.query_async(&mut conn)
.await
.map_err(|e| QueueError::OperationFailed(Box::new(e)))?;
let mut trim_pipe = redis::pipe();
let mut has_trim = false;
for (key, key_type) in keys.iter().zip(types.iter()) {
if key_type == "stream" {
trim_pipe
.cmd("XTRIM")
.arg(key)
.arg("MINID")
.arg("~")
.arg(min_id.to_string())
.ignore();
has_trim = true;
}
}
if has_trim {
let _: () = trim_pipe
.query_async(&mut conn)
.await
.map_err(|e| QueueError::OperationFailed(Box::new(e)))?;
}
Ok(())
}
async fn send_to_dlq(&self, topic: &str, id: &str, payload: &[u8], reason: &str) -> Result<()> {
let mut conn = self
.pool
.get()
.await
.map_err(|_| QueueError::ConnectionFailed)?;
let topic_key = format!("{}:{}", self.namespace, topic);
let dlq_key = format!("{}:dlq", topic_key);
let _: () = redis::cmd("XADD")
.arg(&dlq_key)
.arg("*")
.arg("payload")
.arg(payload)
.arg("reason")
.arg(reason)
.arg("original_id")
.arg(id)
.query_async(&mut conn)
.await
.map_err(|e| QueueError::PushFailed(Box::new(e)))?;
crate::common::metrics::inc_throughput("queue", "producer", "send_dlq", "success", 1);
Ok(())
}
async fn read_dlq(
&self,
topic: &str,
count: usize,
) -> Result<Vec<(String, Vec<u8>, String, String)>> {
let mut conn = self
.pool
.get()
.await
.map_err(|_| QueueError::ConnectionFailed)?;
let mut dlq_keys = Vec::new();
dlq_keys.push(format!("{}:{}:dlq", self.namespace, topic));
if self.shards > 1 {
for shard in 0..self.shards {
dlq_keys.push(format!("{{{}:{}:{}}}:dlq", self.namespace, topic, shard));
}
} else {
dlq_keys.push(format!("{{{}:{}}}:dlq", self.namespace, topic));
}
let mut output = Vec::new();
for dlq_key in dlq_keys {
let result: redis::RedisResult<Vec<(String, HashMap<String, Vec<u8>>)>> =
redis::cmd("XREVRANGE")
.arg(&dlq_key)
.arg("+")
.arg("-")
.arg("COUNT")
.arg(count)
.query_async(&mut conn)
.await;
match result {
Ok(messages) => {
for (id, map) in messages {
let payload = map.get("payload").cloned().unwrap_or_default();
let reason =
String::from_utf8(map.get("reason").cloned().unwrap_or_default())
.unwrap_or_else(|_| "Invalid UTF-8".to_string());
let original_id =
String::from_utf8(map.get("original_id").cloned().unwrap_or_default())
.unwrap_or_default();
output.push((id, payload, reason, original_id));
}
}
Err(e) => {
if let Some(_code) = e.code() {
warn!("Error reading DLQ {}: {}", dlq_key, e);
}
}
}
}
Ok(output)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_shard_id() {
assert_eq!(RedisQueue::extract_shard_id("mocra:task:0"), Some(0));
assert_eq!(RedisQueue::extract_shard_id("mocra:task:7"), Some(7));
assert_eq!(RedisQueue::extract_shard_id("mocra:task:123"), Some(123));
assert_eq!(RedisQueue::extract_shard_id("mocra:task"), None);
assert_eq!(RedisQueue::extract_shard_id("mocra:task:a"), None);
assert_eq!(RedisQueue::extract_shard_id("simple"), None);
}
}