kincir 0.2.0

A Rust message streaming library inspired by Watermill
Documentation
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
//! Acknowledgment-aware router implementation for the Kincir messaging system.
//!
//! This module provides router functionality that integrates with acknowledgment-capable
//! subscribers, enabling reliable message processing with automatic acknowledgment handling.

use crate::ack::{AckHandle, AckSubscriber};
use crate::router::HandlerFunc;
use crate::{Message, Publisher};
use std::error::Error;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tokio::time::timeout;

/// Configuration for acknowledgment behavior in the router
#[derive(Debug, Clone)]
pub struct RouterAckConfig {
    /// Automatic acknowledgment strategy
    pub strategy: AckStrategy,
    /// Timeout for message processing before considering it failed
    pub processing_timeout: Option<Duration>,
    /// Maximum number of retries for failed messages
    pub max_retries: u32,
    /// Whether to requeue messages on processing failure
    pub requeue_on_failure: bool,
    /// Batch size for acknowledgment operations
    pub batch_size: Option<usize>,
}

impl Default for RouterAckConfig {
    fn default() -> Self {
        Self {
            strategy: AckStrategy::AutoAckOnSuccess,
            processing_timeout: Some(Duration::from_secs(30)),
            max_retries: 3,
            requeue_on_failure: true,
            batch_size: None,
        }
    }
}

/// Strategy for automatic acknowledgment handling
#[derive(Debug, Clone, PartialEq)]
pub enum AckStrategy {
    /// Automatically acknowledge on successful processing, nack on failure
    AutoAckOnSuccess,
    /// Manually handle acknowledgments in the handler function
    Manual,
    /// Always acknowledge regardless of processing result
    AlwaysAck,
    /// Never acknowledge (for testing or special cases)
    NeverAck,
}

/// Statistics for acknowledgment operations
#[derive(Debug, Clone, Default)]
pub struct RouterAckStats {
    /// Total messages processed
    pub messages_processed: u64,
    /// Messages successfully acknowledged
    pub messages_acked: u64,
    /// Messages negatively acknowledged
    pub messages_nacked: u64,
    /// Messages that timed out during processing
    pub messages_timed_out: u64,
    /// Messages that exceeded max retries
    pub messages_max_retries_exceeded: u64,
    /// Average processing time in milliseconds
    pub avg_processing_time_ms: f64,
    /// Total processing time for average calculation
    total_processing_time_ms: u64,
}

impl RouterAckStats {
    /// Update statistics with a new processing time
    pub fn update_processing_time(&mut self, duration: Duration) {
        self.total_processing_time_ms += duration.as_millis() as u64;
        self.avg_processing_time_ms = if self.messages_processed > 0 {
            self.total_processing_time_ms as f64 / self.messages_processed as f64
        } else {
            0.0
        };
    }

    /// Get acknowledgment rate as a percentage
    pub fn ack_rate(&self) -> f64 {
        if self.messages_processed > 0 {
            (self.messages_acked as f64 / self.messages_processed as f64) * 100.0
        } else {
            0.0
        }
    }

    /// Get negative acknowledgment rate as a percentage
    pub fn nack_rate(&self) -> f64 {
        if self.messages_processed > 0 {
            (self.messages_nacked as f64 / self.messages_processed as f64) * 100.0
        } else {
            0.0
        }
    }
}

/// Acknowledgment-aware router for reliable message processing
#[cfg(feature = "logging")]
pub struct AckRouter<S, H>
where
    S: AckSubscriber + Send + Sync,
    H: AckHandle + Send + Sync,
{
    /// Logger for debugging and monitoring
    logger: Arc<dyn crate::logging::Logger>,
    /// Topic to consume messages from
    consume_topic: String,
    /// Topic to publish processed messages to
    publish_topic: String,
    /// Acknowledgment-capable subscriber
    subscriber: Arc<Mutex<S>>,
    /// Publisher for processed messages
    publisher: Arc<dyn Publisher<Error = Box<dyn Error + Send + Sync>>>,
    /// Message processing handler
    handler: HandlerFunc,
    /// Acknowledgment configuration
    config: RouterAckConfig,
    /// Statistics tracking
    stats: Arc<Mutex<RouterAckStats>>,
    /// Phantom data for handle type
    _phantom: PhantomData<H>,
}

/// Acknowledgment-aware router without logging
#[cfg(not(feature = "logging"))]
pub struct AckRouter<S, H>
where
    S: AckSubscriber + Send + Sync,
    H: AckHandle + Send + Sync,
{
    /// Topic to consume messages from
    consume_topic: String,
    /// Topic to publish processed messages to
    publish_topic: String,
    /// Acknowledgment-capable subscriber
    subscriber: Arc<Mutex<S>>,
    /// Publisher for processed messages
    publisher: Arc<dyn Publisher<Error = Box<dyn Error + Send + Sync>>>,
    /// Message processing handler
    handler: HandlerFunc,
    /// Acknowledgment configuration
    config: RouterAckConfig,
    /// Statistics tracking
    stats: Arc<Mutex<RouterAckStats>>,
    /// Phantom data for handle type
    _phantom: PhantomData<H>,
}

#[cfg(feature = "logging")]
impl<S, H> AckRouter<S, H>
where
    S: AckSubscriber<AckHandle = H> + Send + Sync + 'static,
    H: AckHandle + Send + Sync + 'static,
    S::Error: Into<Box<dyn Error + Send + Sync>>,
{
    /// Create a new acknowledgment-aware router with logging
    pub fn new(
        logger: Arc<dyn crate::logging::Logger>,
        consume_topic: String,
        publish_topic: String,
        subscriber: Arc<Mutex<S>>,
        publisher: Arc<dyn Publisher<Error = Box<dyn Error + Send + Sync>>>,
        handler: HandlerFunc,
        config: RouterAckConfig,
    ) -> Self {
        Self {
            logger,
            consume_topic,
            publish_topic,
            subscriber,
            publisher,
            handler,
            config,
            stats: Arc::new(Mutex::new(RouterAckStats::default())),
            _phantom: PhantomData,
        }
    }

    /// Create a new acknowledgment-aware router with default configuration
    pub fn with_default_config(
        logger: Arc<dyn crate::logging::Logger>,
        consume_topic: String,
        publish_topic: String,
        subscriber: Arc<Mutex<S>>,
        publisher: Arc<dyn Publisher<Error = Box<dyn Error + Send + Sync>>>,
        handler: HandlerFunc,
    ) -> Self {
        Self::new(
            logger,
            consume_topic,
            publish_topic,
            subscriber,
            publisher,
            handler,
            RouterAckConfig::default(),
        )
    }
}

#[cfg(not(feature = "logging"))]
impl<S, H> AckRouter<S, H>
where
    S: AckSubscriber<AckHandle = H> + Send + Sync + 'static,
    H: AckHandle + Send + Sync + 'static,
    S::Error: Into<Box<dyn Error + Send + Sync>>,
{
    /// Create a new acknowledgment-aware router without logging
    pub fn new(
        consume_topic: String,
        publish_topic: String,
        subscriber: Arc<Mutex<S>>,
        publisher: Arc<dyn Publisher<Error = Box<dyn Error + Send + Sync>>>,
        handler: HandlerFunc,
        config: RouterAckConfig,
    ) -> Self {
        Self {
            consume_topic,
            publish_topic,
            subscriber,
            publisher,
            handler,
            config,
            stats: Arc::new(Mutex::new(RouterAckStats::default())),
            _phantom: PhantomData,
        }
    }

    /// Create a new acknowledgment-aware router with default configuration
    pub fn with_default_config(
        consume_topic: String,
        publish_topic: String,
        subscriber: Arc<Mutex<S>>,
        publisher: Arc<dyn Publisher<Error = Box<dyn Error + Send + Sync>>>,
        handler: HandlerFunc,
    ) -> Self {
        Self::new(
            consume_topic,
            publish_topic,
            subscriber,
            publisher,
            handler,
            RouterAckConfig::default(),
        )
    }
}

impl<S, H> AckRouter<S, H>
where
    S: AckSubscriber<AckHandle = H> + Send + Sync + 'static,
    H: AckHandle + Send + Sync + 'static,
    S::Error: Into<Box<dyn Error + Send + Sync>>,
{
    /// Get current acknowledgment statistics
    pub async fn stats(&self) -> RouterAckStats {
        let stats = self.stats.lock().await;
        stats.clone()
    }

    /// Reset acknowledgment statistics
    pub async fn reset_stats(&self) {
        let mut stats = self.stats.lock().await;
        *stats = RouterAckStats::default();
    }

    /// Update acknowledgment configuration
    pub fn with_config(mut self, config: RouterAckConfig) -> Self {
        self.config = config;
        self
    }

    /// Run the acknowledgment-aware router
    pub async fn run(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
        #[cfg(feature = "logging")]
        self.logger.info("Starting acknowledgment-aware router...").await;

        // Subscribe to the consume topic
        {
            let subscriber = self.subscriber.lock().await;
            subscriber.subscribe(&self.consume_topic).await
                .map_err(|e| e.into())?;
        }

        #[cfg(feature = "logging")]
        self.logger.info(&format!("Subscribed to topic: {}", self.consume_topic)).await;

        // Main processing loop
        loop {
            match self.process_single_message().await {
                Ok(_) => {
                    // Continue processing
                }
                Err(e) => {
                    #[cfg(feature = "logging")]
                    self.logger.error(&format!("Error in message processing loop: {}", e)).await;
                    
                    // Continue processing despite errors
                    tokio::time::sleep(Duration::from_millis(100)).await;
                }
            }
        }
    }

    /// Process a single message with acknowledgment handling (exposed for testing)
    pub async fn process_single_message(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
        let start_time = Instant::now();

        // Receive message with acknowledgment handle
        let (message, ack_handle) = {
            let mut subscriber = self.subscriber.lock().await;
            subscriber.receive_with_ack().await.map_err(|e| e.into())?
        };

        #[cfg(feature = "logging")]
        self.logger.info(&format!(
            "Received message: id={}, topic={}, delivery_count={}",
            ack_handle.message_id(),
            ack_handle.topic(),
            ack_handle.delivery_count()
        )).await;

        // Update statistics
        {
            let mut stats = self.stats.lock().await;
            stats.messages_processed += 1;
        }

        // Process message with timeout if configured
        let processing_result = if let Some(timeout_duration) = self.config.processing_timeout {
            match timeout(timeout_duration, (self.handler)(message.clone())).await {
                Ok(result) => result,
                Err(_) => {
                    #[cfg(feature = "logging")]
                    self.logger.error(&format!(
                        "Message processing timed out after {:?} for message: {}",
                        timeout_duration,
                        ack_handle.message_id()
                    )).await;

                    // Update timeout statistics
                    {
                        let mut stats = self.stats.lock().await;
                        stats.messages_timed_out += 1;
                    }

                    return self.handle_processing_failure(ack_handle, "Processing timeout").await;
                }
            }
        } else {
            (self.handler)(message.clone()).await
        };

        // Update processing time statistics
        let processing_time = start_time.elapsed();
        {
            let mut stats = self.stats.lock().await;
            stats.update_processing_time(processing_time);
        }

        // Handle processing result based on strategy
        match processing_result {
            Ok(processed_messages) => {
                self.handle_processing_success(message, ack_handle, processed_messages).await
            }
            Err(e) => {
                #[cfg(feature = "logging")]
                self.logger.error(&format!(
                    "Message processing failed for message {}: {}",
                    ack_handle.message_id(),
                    e
                )).await;

                self.handle_processing_failure(ack_handle, &e.to_string()).await
            }
        }
    }

    /// Handle successful message processing
    async fn handle_processing_success(
        &self,
        _original_message: Message,
        ack_handle: H,
        processed_messages: Vec<Message>,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        // Publish processed messages if any
        if !processed_messages.is_empty() {
            let message_count = processed_messages.len();
            self.publisher
                .publish(&self.publish_topic, processed_messages)
                .await?;

            #[cfg(feature = "logging")]
            self.logger.info(&format!(
                "Published {} processed messages to topic: {}",
                message_count,
                self.publish_topic
            )).await;
        }

        // Handle acknowledgment based on strategy
        match self.config.strategy {
            AckStrategy::AutoAckOnSuccess | AckStrategy::AlwaysAck => {
                let message_id = ack_handle.message_id().to_string();
                let subscriber = self.subscriber.lock().await;
                subscriber.ack(ack_handle).await.map_err(|e| e.into())?;

                #[cfg(feature = "logging")]
                self.logger.info(&format!(
                    "Message acknowledged: {}",
                    message_id
                )).await;

                // Update statistics
                {
                    let mut stats = self.stats.lock().await;
                    stats.messages_acked += 1;
                }
            }
            AckStrategy::Manual => {
                #[cfg(feature = "logging")]
                self.logger.info(&format!(
                    "Manual acknowledgment mode - handler should handle ack for message: {}",
                    ack_handle.message_id()
                )).await;
            }
            AckStrategy::NeverAck => {
                #[cfg(feature = "logging")]
                self.logger.info(&format!(
                    "Never acknowledge mode - message not acknowledged: {}",
                    ack_handle.message_id()
                )).await;
            }
        }

        Ok(())
    }

    /// Handle failed message processing
    async fn handle_processing_failure(
        &self,
        ack_handle: H,
        error_message: &str,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        let delivery_count = ack_handle.delivery_count();
        let message_id = ack_handle.message_id().to_string();
        let should_retry = delivery_count <= self.config.max_retries;

        if should_retry && self.config.requeue_on_failure {
            // Negative acknowledge with requeue
            let subscriber = self.subscriber.lock().await;
            subscriber.nack(ack_handle, true).await.map_err(|e| e.into())?;

            #[cfg(feature = "logging")]
            self.logger.info(&format!(
                "Message negatively acknowledged with requeue (attempt {}): {} - {}",
                delivery_count,
                message_id,
                error_message
            )).await;
        } else {
            // Max retries exceeded or no requeue - discard message
            let subscriber = self.subscriber.lock().await;
            subscriber.nack(ack_handle, false).await.map_err(|e| e.into())?;

            #[cfg(feature = "logging")]
            if delivery_count > self.config.max_retries {
                self.logger.error(&format!(
                    "Max retries exceeded - message discarded: {} (attempts: {})",
                    message_id,
                    delivery_count
                )).await;

                // Update max retries statistics
                {
                    let mut stats = self.stats.lock().await;
                    stats.messages_max_retries_exceeded += 1;
                }
            } else {
                self.logger.info(&format!(
                    "Message negatively acknowledged without requeue: {} - {}",
                    message_id,
                    error_message
                )).await;
            }
        }

        // Update statistics
        {
            let mut stats = self.stats.lock().await;
            stats.messages_nacked += 1;
        }

        Ok(())
    }

    /// Run the router with batch processing (if configured)
    pub async fn run_with_batching(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
        if let Some(batch_size) = self.config.batch_size {
            self.run_batched(batch_size).await
        } else {
            self.run().await
        }
    }

    /// Run the router with batch acknowledgment processing
    async fn run_batched(&self, batch_size: usize) -> Result<(), Box<dyn Error + Send + Sync>> {
        #[cfg(feature = "logging")]
        self.logger.info(&format!(
            "Starting acknowledgment-aware router with batch size: {}",
            batch_size
        )).await;

        // Subscribe to the consume topic
        {
            let subscriber = self.subscriber.lock().await;
            subscriber.subscribe(&self.consume_topic).await
                .map_err(|e| e.into())?;
        }

        let mut batch_messages = Vec::new();
        let mut batch_handles = Vec::new();

        loop {
            // Collect batch of messages
            for _ in 0..batch_size {
                match self.receive_single_message().await {
                    Ok((message, handle)) => {
                        batch_messages.push(message);
                        batch_handles.push(handle);
                    }
                    Err(e) => {
                        #[cfg(feature = "logging")]
                        self.logger.error(&format!("Error receiving message: {}", e)).await;
                        break;
                    }
                }
            }

            if !batch_messages.is_empty() {
                self.process_message_batch(batch_messages, batch_handles).await?;
                batch_messages = Vec::new();
                batch_handles = Vec::new();
            }
        }
    }

    /// Receive a single message (helper for batching)
    async fn receive_single_message(&self) -> Result<(Message, H), Box<dyn Error + Send + Sync>> {
        let mut subscriber = self.subscriber.lock().await;
        subscriber.receive_with_ack().await.map_err(|e| e.into())
    }

    /// Process a batch of messages
    async fn process_message_batch(
        &self,
        messages: Vec<Message>,
        handles: Vec<H>,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        let mut success_handles = Vec::new();
        let mut failed_handles = Vec::new();
        let mut all_processed_messages = Vec::new();

        // Process each message in the batch
        for (message, handle) in messages.into_iter().zip(handles.into_iter()) {
            match (self.handler)(message).await {
                Ok(mut processed_messages) => {
                    all_processed_messages.append(&mut processed_messages);
                    success_handles.push(handle);
                }
                Err(_) => {
                    failed_handles.push(handle);
                }
            }
        }

        // Publish all processed messages
        if !all_processed_messages.is_empty() {
            self.publisher
                .publish(&self.publish_topic, all_processed_messages)
                .await?;
        }

        // Batch acknowledge successful messages
        if !success_handles.is_empty() {
            let subscriber = self.subscriber.lock().await;
            subscriber.ack_batch(success_handles).await.map_err(|e| e.into())?;
        }

        // Batch handle failed messages
        if !failed_handles.is_empty() {
            let subscriber = self.subscriber.lock().await;
            subscriber.nack_batch(failed_handles, self.config.requeue_on_failure)
                .await
                .map_err(|e| e.into())?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ack_config_default() {
        let config = RouterAckConfig::default();
        assert_eq!(config.strategy, AckStrategy::AutoAckOnSuccess);
        assert_eq!(config.processing_timeout, Some(Duration::from_secs(30)));
        assert_eq!(config.max_retries, 3);
        assert!(config.requeue_on_failure);
        assert_eq!(config.batch_size, None);
    }

    #[test]
    fn test_ack_stats_calculations() {
        let mut stats = RouterAckStats::default();
        
        // Test initial state
        assert_eq!(stats.ack_rate(), 0.0);
        assert_eq!(stats.nack_rate(), 0.0);
        assert_eq!(stats.avg_processing_time_ms, 0.0);

        // Update with some data
        stats.messages_processed = 10;
        stats.messages_acked = 8;
        stats.messages_nacked = 2;
        stats.update_processing_time(Duration::from_millis(100));
        stats.update_processing_time(Duration::from_millis(200));

        assert_eq!(stats.ack_rate(), 80.0);
        assert_eq!(stats.nack_rate(), 20.0);
        assert!(stats.avg_processing_time_ms > 0.0);
    }

    #[test]
    fn test_ack_strategy_variants() {
        let strategies = vec![
            AckStrategy::AutoAckOnSuccess,
            AckStrategy::Manual,
            AckStrategy::AlwaysAck,
            AckStrategy::NeverAck,
        ];

        for strategy in strategies {
            let config = RouterAckConfig {
                strategy: strategy.clone(),
                ..Default::default()
            };
            assert_eq!(config.strategy, strategy);
        }
    }
}