oxirs-stream 0.2.4

Real-time streaming support with Kafka/NATS/MQTT/OPC-UA I/O, RDF Patch, and SPARQL Update delta
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
//! Automatic reconnection logic with exponential backoff
//!
//! Provides resilient connection management with configurable retry strategies,
//! connection failure callbacks, and comprehensive error handling.

use crate::connection_pool::{ConnectionFactory, PooledConnection};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{broadcast, RwLock};
use tokio::time::sleep;
use tracing::{error, info, warn};

/// Reconnection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReconnectConfig {
    /// Initial retry delay
    pub initial_delay: Duration,
    /// Maximum retry delay
    pub max_delay: Duration,
    /// Exponential backoff multiplier
    pub multiplier: f64,
    /// Maximum retry attempts (0 for unlimited)
    pub max_attempts: u32,
    /// Jitter factor (0.0 to 1.0)
    pub jitter_factor: f64,
    /// Connection timeout
    pub connection_timeout: Duration,
    /// Enable connection failure callbacks
    pub enable_callbacks: bool,
}

impl Default for ReconnectConfig {
    fn default() -> Self {
        Self {
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(60),
            multiplier: 2.0,
            max_attempts: 10,
            jitter_factor: 0.1,
            connection_timeout: Duration::from_secs(30),
            enable_callbacks: true,
        }
    }
}

/// Reconnection strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ReconnectStrategy {
    /// Exponential backoff with jitter
    ExponentialBackoff,
    /// Fixed delay between attempts
    FixedDelay(Duration),
    /// Linear backoff
    LinearBackoff(Duration),
    /// Custom strategy with callback
    Custom,
}

/// Reconnection event
#[derive(Debug, Clone)]
pub enum ReconnectEvent {
    /// Reconnection attempt started
    AttemptStarted {
        connection_id: String,
        attempt: u32,
        delay: Duration,
    },
    /// Reconnection attempt succeeded
    AttemptSucceeded {
        connection_id: String,
        attempt: u32,
        total_time: Duration,
    },
    /// Reconnection attempt failed
    AttemptFailed {
        connection_id: String,
        attempt: u32,
        error: String,
        next_delay: Option<Duration>,
    },
    /// All reconnection attempts exhausted
    ReconnectionExhausted {
        connection_id: String,
        total_attempts: u32,
        total_time: Duration,
    },
}

/// Reconnection statistics
#[derive(Debug, Clone, Default)]
pub struct ReconnectStatistics {
    pub total_attempts: u64,
    pub successful_reconnects: u64,
    pub failed_reconnects: u64,
    pub current_streak: u32,
    pub longest_streak: u32,
    pub total_reconnect_time: Duration,
    pub avg_reconnect_time: Duration,
    pub last_reconnect_attempt: Option<Instant>,
    pub last_successful_reconnect: Option<Instant>,
}

/// Callback for connection failures
pub type ConnectionFailureCallback =
    Arc<dyn Fn(String, String, u32) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

/// Automatic reconnection manager
pub struct ReconnectManager<T: PooledConnection> {
    config: ReconnectConfig,
    strategy: ReconnectStrategy,
    statistics: Arc<RwLock<ReconnectStatistics>>,
    event_sender: broadcast::Sender<ReconnectEvent>,
    failure_callbacks: Arc<RwLock<Vec<ConnectionFailureCallback>>>,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: PooledConnection + Clone> ReconnectManager<T> {
    /// Create a new reconnection manager
    pub fn new(config: ReconnectConfig, strategy: ReconnectStrategy) -> Self {
        let (event_sender, _) = broadcast::channel(1000);

        Self {
            config,
            strategy,
            statistics: Arc::new(RwLock::new(ReconnectStatistics::default())),
            event_sender,
            failure_callbacks: Arc::new(RwLock::new(Vec::new())),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Attempt to reconnect with configured strategy
    pub async fn reconnect(
        &self,
        connection_id: String,
        factory: Arc<dyn ConnectionFactory<T>>,
    ) -> Result<T> {
        let start_time = Instant::now();
        let mut attempt = 0;
        let mut current_delay = self.config.initial_delay;

        loop {
            attempt += 1;

            // Check max attempts
            if self.config.max_attempts > 0 && attempt > self.config.max_attempts {
                let total_time = start_time.elapsed();

                let _ = self
                    .event_sender
                    .send(ReconnectEvent::ReconnectionExhausted {
                        connection_id: connection_id.clone(),
                        total_attempts: attempt - 1,
                        total_time,
                    });

                // Update statistics
                let mut stats = self.statistics.write().await;
                stats.failed_reconnects += 1;
                stats.current_streak = 0;

                // Call failure callbacks
                if self.config.enable_callbacks {
                    self.invoke_failure_callbacks(
                        connection_id.clone(),
                        "Maximum retry attempts exhausted".to_string(),
                        attempt - 1,
                    )
                    .await;
                }

                return Err(anyhow!(
                    "Failed to reconnect after {} attempts",
                    self.config.max_attempts
                ));
            }

            // Calculate delay with jitter
            let jittered_delay = self.apply_jitter(current_delay);

            if attempt > 1 {
                info!(
                    "Reconnection attempt {} for {} in {:?}",
                    attempt, connection_id, jittered_delay
                );

                let _ = self.event_sender.send(ReconnectEvent::AttemptStarted {
                    connection_id: connection_id.clone(),
                    attempt,
                    delay: jittered_delay,
                });

                sleep(jittered_delay).await;
            }

            // Update statistics
            {
                let mut stats = self.statistics.write().await;
                stats.total_attempts += 1;
                stats.last_reconnect_attempt = Some(Instant::now());
            }

            // Attempt connection with timeout
            match tokio::time::timeout(self.config.connection_timeout, factory.create_connection())
                .await
            {
                Ok(Ok(connection)) => {
                    let total_time = start_time.elapsed();

                    info!(
                        "Successfully reconnected {} after {} attempts in {:?}",
                        connection_id, attempt, total_time
                    );

                    let _ = self.event_sender.send(ReconnectEvent::AttemptSucceeded {
                        connection_id: connection_id.clone(),
                        attempt,
                        total_time,
                    });

                    // Update statistics
                    let mut stats = self.statistics.write().await;
                    stats.successful_reconnects += 1;
                    stats.current_streak += 1;
                    stats.longest_streak = stats.longest_streak.max(stats.current_streak);
                    stats.total_reconnect_time += total_time;
                    stats.last_successful_reconnect = Some(Instant::now());

                    if stats.successful_reconnects > 0 {
                        stats.avg_reconnect_time =
                            stats.total_reconnect_time / stats.successful_reconnects as u32;
                    }

                    return Ok(connection);
                }
                Ok(Err(e)) => {
                    warn!(
                        "Reconnection attempt {} for {} failed: {}",
                        attempt, connection_id, e
                    );

                    // Calculate next delay
                    current_delay = self.calculate_next_delay(current_delay, attempt);
                    let next_delay = if attempt < self.config.max_attempts {
                        Some(current_delay)
                    } else {
                        None
                    };

                    let _ = self.event_sender.send(ReconnectEvent::AttemptFailed {
                        connection_id: connection_id.clone(),
                        attempt,
                        error: e.to_string(),
                        next_delay,
                    });

                    // Call failure callbacks for each attempt if enabled
                    if self.config.enable_callbacks && attempt % 3 == 0 {
                        self.invoke_failure_callbacks(
                            connection_id.clone(),
                            e.to_string(),
                            attempt,
                        )
                        .await;
                    }
                }
                Err(_) => {
                    error!(
                        "Reconnection attempt {} for {} timed out",
                        attempt, connection_id
                    );

                    current_delay = self.calculate_next_delay(current_delay, attempt);

                    let _ = self.event_sender.send(ReconnectEvent::AttemptFailed {
                        connection_id: connection_id.clone(),
                        attempt,
                        error: "Connection timeout".to_string(),
                        next_delay: Some(current_delay),
                    });
                }
            }
        }
    }

    /// Calculate next delay based on strategy
    fn calculate_next_delay(&self, current_delay: Duration, attempt: u32) -> Duration {
        match &self.strategy {
            ReconnectStrategy::ExponentialBackoff => {
                let next_delay = current_delay.mul_f64(self.config.multiplier);
                next_delay.min(self.config.max_delay)
            }
            ReconnectStrategy::FixedDelay(delay) => *delay,
            ReconnectStrategy::LinearBackoff(increment) => {
                let next_delay = self.config.initial_delay + (*increment * attempt);
                next_delay.min(self.config.max_delay)
            }
            ReconnectStrategy::Custom => {
                // For custom strategy, use exponential backoff as fallback
                let next_delay = current_delay.mul_f64(self.config.multiplier);
                next_delay.min(self.config.max_delay)
            }
        }
    }

    /// Apply jitter to delay
    fn apply_jitter(&self, delay: Duration) -> Duration {
        if self.config.jitter_factor <= 0.0 {
            return delay;
        }

        let jitter_range = delay.as_millis() as f64 * self.config.jitter_factor;
        let jitter = (fastrand::f64() - 0.5) * 2.0 * jitter_range;
        let jittered_millis = (delay.as_millis() as f64 + jitter).max(0.0) as u64;

        Duration::from_millis(jittered_millis)
    }

    /// Register a connection failure callback
    pub async fn register_failure_callback<F>(&self, callback: F)
    where
        F: Fn(String, String, u32) -> Pin<Box<dyn Future<Output = ()> + Send>>
            + Send
            + Sync
            + 'static,
    {
        let mut callbacks = self.failure_callbacks.write().await;
        callbacks.push(Arc::new(callback));
    }

    /// Invoke all registered failure callbacks
    async fn invoke_failure_callbacks(&self, connection_id: String, error: String, attempt: u32) {
        let callbacks = self.failure_callbacks.read().await;

        for callback in callbacks.iter() {
            let fut = callback(connection_id.clone(), error.clone(), attempt);
            tokio::spawn(async move {
                fut.await;
            });
        }
    }

    /// Get reconnection statistics
    pub async fn get_statistics(&self) -> ReconnectStatistics {
        self.statistics.read().await.clone()
    }

    /// Reset reconnection statistics
    pub async fn reset_statistics(&self) {
        *self.statistics.write().await = ReconnectStatistics::default();
    }

    /// Subscribe to reconnection events
    pub fn subscribe(&self) -> broadcast::Receiver<ReconnectEvent> {
        self.event_sender.subscribe()
    }
}

/// Helper for creating reconnection-aware connections
pub struct ResilientConnection<T: PooledConnection> {
    connection: Option<T>,
    connection_id: String,
    factory: Arc<dyn ConnectionFactory<T>>,
    reconnect_manager: Arc<ReconnectManager<T>>,
    last_error: Option<String>,
}

impl<T: PooledConnection + Clone> ResilientConnection<T> {
    /// Create a new resilient connection
    pub async fn new(
        connection_id: String,
        factory: Arc<dyn ConnectionFactory<T>>,
        reconnect_manager: Arc<ReconnectManager<T>>,
    ) -> Result<Self> {
        let connection = factory.create_connection().await?;

        Ok(Self {
            connection: Some(connection),
            connection_id,
            factory,
            reconnect_manager,
            last_error: None,
        })
    }

    /// Get the underlying connection, reconnecting if necessary
    pub async fn get_connection(&mut self) -> Result<&mut T> {
        // Check if we have a healthy connection
        let needs_reconnection = match self.connection {
            Some(ref mut conn) => !conn.is_healthy().await,
            None => true,
        };

        if !needs_reconnection {
            // Return the healthy connection
            return self
                .connection
                .as_mut()
                .ok_or_else(|| anyhow!("Connection unexpectedly None"));
        }

        // Connection is unhealthy or missing, attempt reconnection
        info!(
            "Connection {} is unhealthy, attempting reconnection",
            self.connection_id
        );

        match self
            .reconnect_manager
            .reconnect(self.connection_id.clone(), self.factory.clone())
            .await
        {
            Ok(new_conn) => {
                self.connection = Some(new_conn);
                self.last_error = None;
                self.connection
                    .as_mut()
                    .ok_or_else(|| anyhow!("Connection unexpectedly None"))
            }
            Err(e) => {
                self.last_error = Some(e.to_string());
                Err(e)
            }
        }
    }

    /// Check if connection is currently healthy
    pub async fn is_healthy(&self) -> bool {
        if let Some(ref conn) = self.connection {
            conn.is_healthy().await
        } else {
            false
        }
    }

    /// Get the last error if any
    pub fn last_error(&self) -> Option<&str> {
        self.last_error.as_deref()
    }

    /// Manually trigger reconnection
    pub async fn reconnect(&mut self) -> Result<()> {
        let new_conn = self
            .reconnect_manager
            .reconnect(self.connection_id.clone(), self.factory.clone())
            .await?;

        // Close old connection if exists
        if let Some(mut old_conn) = self.connection.take() {
            let _ = old_conn.close().await;
        }

        self.connection = Some(new_conn);
        self.last_error = None;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};

    #[derive(Clone)]
    struct TestConnection {
        id: u32,
        healthy: Arc<AtomicBool>,
        created_at: Instant,
    }

    #[async_trait::async_trait]
    impl PooledConnection for TestConnection {
        async fn is_healthy(&self) -> bool {
            self.healthy.load(Ordering::Relaxed)
        }

        async fn close(&mut self) -> Result<()> {
            Ok(())
        }

        fn clone_connection(&self) -> Box<dyn PooledConnection> {
            Box::new(TestConnection {
                id: self.id,
                healthy: Arc::new(AtomicBool::new(self.healthy.load(Ordering::Relaxed))),
                created_at: self.created_at,
            })
        }

        fn created_at(&self) -> Instant {
            self.created_at
        }

        fn last_activity(&self) -> Instant {
            Instant::now()
        }

        fn update_activity(&mut self) {}
    }

    struct TestConnectionFactory {
        counter: Arc<AtomicU32>,
        should_fail: Arc<AtomicBool>,
        fail_count: Arc<AtomicU32>,
    }

    #[async_trait::async_trait]
    impl ConnectionFactory<TestConnection> for TestConnectionFactory {
        async fn create_connection(&self) -> Result<TestConnection> {
            let current_fails = self.fail_count.load(Ordering::Relaxed);

            if self.should_fail.load(Ordering::Relaxed) && current_fails > 0 {
                self.fail_count.fetch_sub(1, Ordering::Relaxed);
                return Err(anyhow!("Simulated connection failure"));
            }

            let id = self.counter.fetch_add(1, Ordering::Relaxed);
            Ok(TestConnection {
                id,
                healthy: Arc::new(AtomicBool::new(true)),
                created_at: Instant::now(),
            })
        }
    }

    #[tokio::test]
    async fn test_exponential_backoff() {
        let config = ReconnectConfig {
            initial_delay: Duration::from_millis(10),
            max_delay: Duration::from_millis(100),
            multiplier: 2.0,
            max_attempts: 5,
            jitter_factor: 0.0,
            ..Default::default()
        };

        let manager =
            ReconnectManager::<TestConnection>::new(config, ReconnectStrategy::ExponentialBackoff);

        let factory = Arc::new(TestConnectionFactory {
            counter: Arc::new(AtomicU32::new(0)),
            should_fail: Arc::new(AtomicBool::new(true)),
            fail_count: Arc::new(AtomicU32::new(3)), // Fail first 3 attempts
        });

        let start = Instant::now();
        let result = manager.reconnect("test-conn".to_string(), factory).await;
        let elapsed = start.elapsed();

        assert!(result.is_ok());

        // Should have delays: 0ms, 10ms, 20ms, 40ms (total ~70ms)
        // Allow for more timing variance during parallel test execution
        assert!(elapsed >= Duration::from_millis(50));
        assert!(elapsed < Duration::from_millis(300));

        let stats = manager.get_statistics().await;
        assert_eq!(stats.total_attempts, 4);
        assert_eq!(stats.successful_reconnects, 1);
    }

    #[tokio::test]
    async fn test_max_attempts() {
        let config = ReconnectConfig {
            initial_delay: Duration::from_millis(1),
            max_attempts: 3,
            ..Default::default()
        };

        let manager =
            ReconnectManager::<TestConnection>::new(config, ReconnectStrategy::ExponentialBackoff);

        let factory = Arc::new(TestConnectionFactory {
            counter: Arc::new(AtomicU32::new(0)),
            should_fail: Arc::new(AtomicBool::new(true)),
            fail_count: Arc::new(AtomicU32::new(100)), // Always fail
        });

        let result = manager.reconnect("test-conn".to_string(), factory).await;
        assert!(result.is_err());

        let stats = manager.get_statistics().await;
        assert_eq!(stats.total_attempts, 3);
        assert_eq!(stats.failed_reconnects, 1);
    }

    #[tokio::test]
    async fn test_failure_callbacks() {
        let config = ReconnectConfig {
            initial_delay: Duration::from_millis(1),
            max_attempts: 3,
            enable_callbacks: true,
            ..Default::default()
        };

        let manager = ReconnectManager::<TestConnection>::new(
            config,
            ReconnectStrategy::FixedDelay(Duration::from_millis(1)),
        );

        let callback_called = Arc::new(AtomicBool::new(false));
        let callback_called_clone = callback_called.clone();

        manager
            .register_failure_callback(move |_id, _error, _attempt| {
                let called = callback_called_clone.clone();
                Box::pin(async move {
                    called.store(true, Ordering::Relaxed);
                })
            })
            .await;

        let factory = Arc::new(TestConnectionFactory {
            counter: Arc::new(AtomicU32::new(0)),
            should_fail: Arc::new(AtomicBool::new(true)),
            fail_count: Arc::new(AtomicU32::new(100)),
        });

        let _ = manager.reconnect("test-conn".to_string(), factory).await;

        // Give callback time to execute
        tokio::time::sleep(Duration::from_millis(10)).await;

        assert!(callback_called.load(Ordering::Relaxed));
    }

    #[tokio::test]
    async fn test_resilient_connection() {
        let config = ReconnectConfig::default();
        let manager = Arc::new(ReconnectManager::<TestConnection>::new(
            config,
            ReconnectStrategy::ExponentialBackoff,
        ));

        let _healthy_flag = Arc::new(AtomicBool::new(true));
        let factory = Arc::new(TestConnectionFactory {
            counter: Arc::new(AtomicU32::new(0)),
            should_fail: Arc::new(AtomicBool::new(false)),
            fail_count: Arc::new(AtomicU32::new(0)),
        });

        let mut resilient = ResilientConnection::new("test-conn".to_string(), factory, manager)
            .await
            .unwrap();

        // Should work normally
        assert!(resilient.is_healthy().await);
        let conn = resilient.get_connection().await.unwrap();
        assert!(conn.is_healthy().await);
    }
}