rivven-client 0.0.22

High-performance async client for Rivven event streaming platform
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
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
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
//! Production-grade resilient client with connection pooling, retries, and circuit breaker
//!
//! # Features
//!
//! - **Connection pooling**: Efficiently reuse connections across requests
//! - **Automatic retries**: Exponential backoff with jitter for transient failures
//! - **Circuit breaker**: Prevent cascading failures to unhealthy servers
//! - **Health checking**: Background health monitoring and connection validation
//! - **Timeouts**: Request and connection timeouts with configurable values
//!
//! # Example
//!
//! ```rust,ignore
//! use rivven_client::{ResilientClient, ResilientClientConfig};
//!
//! let config = ResilientClientConfig::builder()
//!     .bootstrap_servers(vec!["localhost:9092".to_string()])
//!     .pool_size(10)
//!     .retry_max_attempts(3)
//!     .circuit_breaker_threshold(5)
//!     .build();
//!
//! let client = ResilientClient::new(config).await?;
//!
//! // Auto-retry on transient failures
//! let offset = client.publish("my-topic", b"hello").await?;
//! ```

use crate::{Client, Error, MessageData, Result};
use bytes::Bytes;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{Mutex, RwLock, Semaphore};
use tokio::time::{sleep, timeout};
use tracing::{debug, info, warn};

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for the resilient client
#[derive(Debug, Clone)]
pub struct ResilientClientConfig {
    /// Bootstrap servers (host:port)
    pub bootstrap_servers: Vec<String>,
    /// Connection pool size per server
    pub pool_size: usize,
    /// Maximum retry attempts
    pub retry_max_attempts: u32,
    /// Initial retry delay
    pub retry_initial_delay: Duration,
    /// Maximum retry delay
    pub retry_max_delay: Duration,
    /// Retry backoff multiplier
    pub retry_multiplier: f64,
    /// Circuit breaker failure threshold
    pub circuit_breaker_threshold: u32,
    /// Circuit breaker recovery timeout
    pub circuit_breaker_timeout: Duration,
    /// Circuit breaker half-open success threshold
    pub circuit_breaker_success_threshold: u32,
    /// Connection timeout
    pub connection_timeout: Duration,
    /// Request timeout
    pub request_timeout: Duration,
    /// Health check interval
    pub health_check_interval: Duration,
    /// Enable automatic health checking
    pub health_check_enabled: bool,
    /// Maximum connection lifetime before recycling (default: 300s)
    pub max_connection_lifetime: Duration,
    /// Idle timeout — connections unused longer than this are discarded (default: 60s)
    pub idle_timeout: Duration,
    /// Optional authentication credentials
    ///
    /// When set, every new connection created by the pool will automatically
    /// authenticate using SCRAM-SHA-256 before being returned to the caller.
    pub auth: Option<ResilientAuthConfig>,
    /// Optional TLS configuration.
    ///
    /// When set, all connections use TLS encryption via `Client::connect_tls()`.
    /// Both client and mTLS are supported via the `TlsConfig` type.
    #[cfg(feature = "tls")]
    pub tls: Option<ResilientTlsConfig>,
}

/// TLS configuration for resilient client
#[cfg(feature = "tls")]
#[derive(Debug, Clone)]
pub struct ResilientTlsConfig {
    /// TLS configuration (certificates, CA, etc.)
    pub tls_config: rivven_core::tls::TlsConfig,
    /// Server name for SNI verification
    pub server_name: String,
}

/// Authentication configuration for resilient client
#[derive(Debug, Clone)]
pub struct ResilientAuthConfig {
    /// Username
    pub username: String,
    /// Password
    pub password: String,
}

impl Default for ResilientClientConfig {
    fn default() -> Self {
        Self {
            bootstrap_servers: vec!["localhost:9092".to_string()],
            pool_size: 5,
            retry_max_attempts: 3,
            retry_initial_delay: Duration::from_millis(100),
            retry_max_delay: Duration::from_secs(10),
            retry_multiplier: 2.0,
            circuit_breaker_threshold: 5,
            circuit_breaker_timeout: Duration::from_secs(30),
            circuit_breaker_success_threshold: 2,
            connection_timeout: Duration::from_secs(10),
            request_timeout: Duration::from_secs(30),
            health_check_interval: Duration::from_secs(30),
            health_check_enabled: true,
            max_connection_lifetime: Duration::from_secs(300),
            idle_timeout: Duration::from_secs(60),
            auth: None,
            #[cfg(feature = "tls")]
            tls: None,
        }
    }
}

impl ResilientClientConfig {
    /// Create a new builder
    pub fn builder() -> ResilientClientConfigBuilder {
        ResilientClientConfigBuilder::default()
    }
}

/// Builder for ResilientClientConfig
#[derive(Default)]
pub struct ResilientClientConfigBuilder {
    config: ResilientClientConfig,
}

impl ResilientClientConfigBuilder {
    /// Set bootstrap servers
    pub fn bootstrap_servers(mut self, servers: Vec<String>) -> Self {
        self.config.bootstrap_servers = servers;
        self
    }

    /// Set pool size per server
    pub fn pool_size(mut self, size: usize) -> Self {
        self.config.pool_size = size;
        self
    }

    /// Set maximum retry attempts
    pub fn retry_max_attempts(mut self, attempts: u32) -> Self {
        self.config.retry_max_attempts = attempts;
        self
    }

    /// Set initial retry delay
    pub fn retry_initial_delay(mut self, delay: Duration) -> Self {
        self.config.retry_initial_delay = delay;
        self
    }

    /// Set maximum retry delay
    pub fn retry_max_delay(mut self, delay: Duration) -> Self {
        self.config.retry_max_delay = delay;
        self
    }

    /// Set retry backoff multiplier
    pub fn retry_multiplier(mut self, multiplier: f64) -> Self {
        self.config.retry_multiplier = multiplier;
        self
    }

    /// Set circuit breaker failure threshold
    pub fn circuit_breaker_threshold(mut self, threshold: u32) -> Self {
        self.config.circuit_breaker_threshold = threshold;
        self
    }

    /// Set circuit breaker recovery timeout
    pub fn circuit_breaker_timeout(mut self, timeout: Duration) -> Self {
        self.config.circuit_breaker_timeout = timeout;
        self
    }

    /// Set connection timeout
    pub fn connection_timeout(mut self, timeout: Duration) -> Self {
        self.config.connection_timeout = timeout;
        self
    }

    /// Set request timeout
    pub fn request_timeout(mut self, timeout: Duration) -> Self {
        self.config.request_timeout = timeout;
        self
    }

    /// Enable or disable health checking
    pub fn health_check_enabled(mut self, enabled: bool) -> Self {
        self.config.health_check_enabled = enabled;
        self
    }

    /// Set health check interval
    pub fn health_check_interval(mut self, interval: Duration) -> Self {
        self.config.health_check_interval = interval;
        self
    }

    /// Set maximum connection lifetime before recycling
    pub fn max_connection_lifetime(mut self, lifetime: Duration) -> Self {
        self.config.max_connection_lifetime = lifetime;
        self
    }

    /// Set authentication credentials
    ///
    /// When set, every new connection created by the pool will authenticate
    /// using SCRAM-SHA-256 before being returned to the caller.
    pub fn auth(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
        self.config.auth = Some(ResilientAuthConfig {
            username: username.into(),
            password: password.into(),
        });
        self
    }

    /// Set TLS configuration for encrypted connections.
    ///
    /// When set, all connections created by the pool will use TLS.
    #[cfg(feature = "tls")]
    pub fn tls(
        mut self,
        tls_config: rivven_core::tls::TlsConfig,
        server_name: impl Into<String>,
    ) -> Self {
        self.config.tls = Some(ResilientTlsConfig {
            tls_config,
            server_name: server_name.into(),
        });
        self
    }

    /// Build the configuration
    pub fn build(self) -> ResilientClientConfig {
        self.config
    }
}

// ============================================================================
// Circuit Breaker
// ============================================================================

/// Circuit breaker states
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    Closed,
    Open,
    HalfOpen,
}

/// Circuit breaker for a single server
struct CircuitBreaker {
    state: AtomicU32,
    failure_count: AtomicU32,
    success_count: AtomicU32,
    last_failure: RwLock<Option<Instant>>,
    config: Arc<ResilientClientConfig>,
}

impl CircuitBreaker {
    fn new(config: Arc<ResilientClientConfig>) -> Self {
        Self {
            state: AtomicU32::new(0), // Closed
            failure_count: AtomicU32::new(0),
            success_count: AtomicU32::new(0),
            last_failure: RwLock::new(None),
            config,
        }
    }

    fn get_state(&self) -> CircuitState {
        match self.state.load(Ordering::SeqCst) {
            0 => CircuitState::Closed,
            1 => CircuitState::Open,
            _ => CircuitState::HalfOpen,
        }
    }

    async fn allow_request(&self) -> bool {
        match self.get_state() {
            CircuitState::Closed => true,
            CircuitState::Open => {
                let last_failure = self.last_failure.read().await;
                if let Some(t) = *last_failure {
                    if t.elapsed() > self.config.circuit_breaker_timeout {
                        // Use compare_exchange so only one thread
                        // transitions Open → HalfOpen atomically. Without this,
                        // multiple threads race through the probe window.
                        if self
                            .state
                            .compare_exchange(1, 2, Ordering::SeqCst, Ordering::SeqCst)
                            .is_ok()
                        {
                            self.success_count.store(0, Ordering::SeqCst);
                            return true;
                        }
                        // Another thread already transitioned — allow if now HalfOpen
                        return self.get_state() == CircuitState::HalfOpen;
                    }
                }
                false
            }
            CircuitState::HalfOpen => true,
        }
    }

    async fn record_success(&self) {
        self.failure_count.store(0, Ordering::SeqCst);

        if self.get_state() == CircuitState::HalfOpen {
            let count = self.success_count.fetch_add(1, Ordering::SeqCst) + 1;
            if count >= self.config.circuit_breaker_success_threshold {
                self.state.store(0, Ordering::SeqCst); // Closed
                debug!("Circuit breaker closed after {} successes", count);
            }
        }
    }

    async fn record_failure(&self) {
        let count = self.failure_count.fetch_add(1, Ordering::SeqCst) + 1;
        *self.last_failure.write().await = Some(Instant::now());

        if count >= self.config.circuit_breaker_threshold {
            self.state.store(1, Ordering::SeqCst); // Open
            warn!("Circuit breaker opened after {} failures", count);
        }
    }
}

// ============================================================================
// Connection Pool
// ============================================================================

/// Pooled connection wrapper.
///
/// Holds a semaphore permit from the owning `ConnectionPool`. When this struct
/// is dropped (e.g. on request timeout), the permit is automatically released,
/// preventing permanent pool slot exhaustion.
struct PooledConnection {
    client: Client,
    created_at: Instant,
    last_used: Instant,
    /// Semaphore permit — released on drop to return the pool slot.
    _permit: tokio::sync::OwnedSemaphorePermit,
}

/// Connection pool for a single server
struct ConnectionPool {
    addr: String,
    connections: Mutex<Vec<PooledConnection>>,
    semaphore: Arc<Semaphore>,
    config: Arc<ResilientClientConfig>,
    circuit_breaker: CircuitBreaker,
}

impl ConnectionPool {
    fn new(addr: String, config: Arc<ResilientClientConfig>) -> Self {
        Self {
            addr,
            connections: Mutex::new(Vec::new()),
            semaphore: Arc::new(Semaphore::new(config.pool_size)),
            circuit_breaker: CircuitBreaker::new(config.clone()),
            config,
        }
    }

    async fn get(&self) -> Result<PooledConnection> {
        // Check circuit breaker
        if !self.circuit_breaker.allow_request().await {
            return Err(Error::CircuitBreakerOpen(self.addr.clone()));
        }

        // Acquire semaphore permit — owned so it can be stored in PooledConnection
        let permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|_| Error::ConnectionError("Pool exhausted".to_string()))?;

        // Try to get existing connection — also check idle staleness
        // to avoid reusing connections the server may have already closed.
        {
            let mut connections = self.connections.lock().await;
            while let Some(conn) = connections.pop() {
                // Discard connections idle for too long (server may have closed them)
                if conn.last_used.elapsed() >= self.config.idle_timeout {
                    continue; // Drop stale connection, try next one in pool
                }
                // Discard connections that have exceeded their maximum lifetime
                if conn.created_at.elapsed() >= self.config.max_connection_lifetime {
                    continue;
                }
                let mut conn = conn;
                conn.last_used = Instant::now();
                conn._permit = permit;
                return Ok(conn);
            }
        }

        // Create new connection with timeout (TLS or plaintext)
        #[cfg(feature = "tls")]
        let mut client = if let Some(ref tls) = self.config.tls {
            timeout(
                self.config.connection_timeout,
                Client::connect_tls_with_timeout(
                    &self.addr,
                    &tls.tls_config,
                    &tls.server_name,
                    self.config.connection_timeout,
                ),
            )
            .await
            .map_err(|_| {
                Error::ConnectionError(format!("TLS connection timeout to {}", self.addr))
            })?
            .map_err(|e| {
                Error::ConnectionError(format!("Failed TLS connect to {}: {}", self.addr, e))
            })?
        } else {
            timeout(self.config.connection_timeout, Client::connect(&self.addr))
                .await
                .map_err(|_| {
                    Error::ConnectionError(format!("Connection timeout to {}", self.addr))
                })?
                .map_err(|e| {
                    Error::ConnectionError(format!("Failed to connect to {}: {}", self.addr, e))
                })?
        };

        #[cfg(not(feature = "tls"))]
        let mut client = timeout(self.config.connection_timeout, Client::connect(&self.addr))
            .await
            .map_err(|_| Error::ConnectionError(format!("Connection timeout to {}", self.addr)))?
            .map_err(|e| {
                Error::ConnectionError(format!("Failed to connect to {}: {}", self.addr, e))
            })?;

        // Auto-authenticate new connections when credentials are configured.
        // Uses SCRAM-SHA-256 for secure challenge-response authentication.
        // Use AuthenticationFailed (not ConnectionError) so that
        // the retry logic does not treat credential errors as transient.
        if let Some(auth) = &self.config.auth {
            client
                .authenticate_scram(&auth.username, &auth.password)
                .await
                .map_err(|e| {
                    Error::AuthenticationFailed(format!(
                        "Authentication failed for {}: {}",
                        self.addr, e
                    ))
                })?;
        }

        Ok(PooledConnection {
            client,
            created_at: Instant::now(),
            last_used: Instant::now(),
            _permit: permit,
        })
    }

    async fn put(&self, conn: PooledConnection) {
        // Use configured max_connection_lifetime instead of hardcoded 300s
        if conn.created_at.elapsed() < self.config.max_connection_lifetime {
            let mut connections = self.connections.lock().await;
            if connections.len() < self.config.pool_size {
                connections.push(conn);
            }
        }
    }

    /// Get a connection for health checking, bypassing the circuit breaker.
    ///
    /// When the circuit breaker is open, normal `get()` calls are blocked,
    /// which would prevent health checks from ever running to re-close the
    /// breaker. This method allows health probes to bypass that check.
    ///
    /// Uses the same TLS + SCRAM-SHA-256 logic as `get()` to avoid
    /// security regressions (CLIENT-C1/C2).
    async fn get_for_health_check(&self) -> Result<PooledConnection> {
        // Skip circuit breaker — the whole point is to probe a possibly-down server
        let permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|_| Error::ConnectionError("Pool exhausted".to_string()))?;

        // Try existing connection first
        {
            let mut connections = self.connections.lock().await;
            while let Some(conn) = connections.pop() {
                if conn.last_used.elapsed() < self.config.idle_timeout
                    && conn.created_at.elapsed() < self.config.max_connection_lifetime
                {
                    let mut conn = conn;
                    conn.last_used = Instant::now();
                    conn._permit = permit;
                    return Ok(conn);
                }
            }
        }

        // Create new connection for health check — use TLS when configured
        // (mirrors the get() logic to avoid sending credentials over plaintext)
        #[cfg(feature = "tls")]
        let mut client = if let Some(ref tls) = self.config.tls {
            timeout(
                self.config.connection_timeout,
                Client::connect_tls_with_timeout(
                    &self.addr,
                    &tls.tls_config,
                    &tls.server_name,
                    self.config.connection_timeout,
                ),
            )
            .await
            .map_err(|_| {
                Error::ConnectionError(format!(
                    "TLS health-check connection timeout to {}",
                    self.addr
                ))
            })?
            .map_err(|e| {
                Error::ConnectionError(format!(
                    "Failed TLS health-check connect to {}: {}",
                    self.addr, e
                ))
            })?
        } else {
            timeout(self.config.connection_timeout, Client::connect(&self.addr))
                .await
                .map_err(|_| Error::Timeout)?
                .map_err(|e| Error::ConnectionError(e.to_string()))?
        };

        #[cfg(not(feature = "tls"))]
        let mut client = timeout(self.config.connection_timeout, Client::connect(&self.addr))
            .await
            .map_err(|_| Error::Timeout)?
            .map_err(|e| Error::ConnectionError(e.to_string()))?;

        // Authenticate using SCRAM-SHA-256 (not SASL/PLAIN) — same as get()
        if let Some(ref auth) = self.config.auth {
            client
                .authenticate_scram(&auth.username, &auth.password)
                .await
                .map_err(|e| {
                    Error::AuthenticationFailed(format!(
                        "Health-check authentication failed for {}: {}",
                        self.addr, e
                    ))
                })?;
        }

        Ok(PooledConnection {
            client,
            created_at: Instant::now(),
            last_used: Instant::now(),
            _permit: permit,
        })
    }

    async fn record_success(&self) {
        self.circuit_breaker.record_success().await;
    }

    async fn record_failure(&self) {
        self.circuit_breaker.record_failure().await;
    }

    fn circuit_state(&self) -> CircuitState {
        self.circuit_breaker.get_state()
    }
}

// ============================================================================
// Resilient Client
// ============================================================================

/// Production-grade resilient client with connection pooling and fault tolerance
pub struct ResilientClient {
    pools: HashMap<String, Arc<ConnectionPool>>,
    config: Arc<ResilientClientConfig>,
    current_server: AtomicU64,
    total_requests: AtomicU64,
    total_failures: AtomicU64,
    _health_check_handle: Option<tokio::task::JoinHandle<()>>,
}

impl ResilientClient {
    /// Create a new resilient client
    pub async fn new(config: ResilientClientConfig) -> Result<Self> {
        if config.bootstrap_servers.is_empty() {
            return Err(Error::ConnectionError(
                "No bootstrap servers configured".to_string(),
            ));
        }

        let config = Arc::new(config);
        let mut pools = HashMap::new();

        for server in &config.bootstrap_servers {
            let pool = Arc::new(ConnectionPool::new(server.clone(), config.clone()));
            pools.insert(server.clone(), pool);
        }

        info!(
            "Resilient client initialized with {} servers, pool size {}",
            config.bootstrap_servers.len(),
            config.pool_size
        );

        let mut client = Self {
            pools,
            config: config.clone(),
            current_server: AtomicU64::new(0),
            total_requests: AtomicU64::new(0),
            total_failures: AtomicU64::new(0),
            _health_check_handle: None,
        };

        // Start health check background task
        if config.health_check_enabled {
            let pools_clone: HashMap<String, Arc<ConnectionPool>> = client
                .pools
                .iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect();
            let interval = config.health_check_interval;

            let handle = tokio::spawn(async move {
                loop {
                    sleep(interval).await;
                    for (addr, pool) in &pools_clone {
                        // Use get_for_health_check() which bypasses the circuit
                        // breaker — otherwise, when the circuit is open, health
                        // checks are blocked and the breaker never recovers.
                        if let Ok(mut conn) = pool.get_for_health_check().await {
                            match conn.client.ping().await {
                                Ok(()) => {
                                    pool.record_success().await;
                                    debug!("Health check passed for {}", addr);
                                }
                                Err(e) => {
                                    pool.record_failure().await;
                                    warn!("Health check failed for {}: {}", addr, e);
                                    // Discard connections that fail health
                                    // checks — do not return them to the pool.
                                    continue;
                                }
                            }
                            pool.put(conn).await;
                        }
                    }
                }
            });

            client._health_check_handle = Some(handle);
        }

        Ok(client)
    }

    /// Execute an operation with automatic retries and server failover
    async fn execute_with_retry<F, T, Fut>(&self, operation: F) -> Result<T>
    where
        F: Fn(PooledConnection) -> Fut + Clone,
        Fut: std::future::Future<Output = (PooledConnection, Result<T>)>,
    {
        self.total_requests.fetch_add(1, Ordering::Relaxed);
        let servers: Vec<_> = self.config.bootstrap_servers.clone();
        let num_servers = servers.len();
        let mut last_error: Option<Error> = None;

        // Use a manual attempt counter so that circuit-breaker
        // skips do not consume retry budget. Guard against infinite loops
        // with a max-iterations bound (attempts × servers).
        let mut attempt: u32 = 0;
        let max_iterations = self.config.retry_max_attempts as usize * num_servers.max(1) * 2;
        let mut iteration: usize = 0;

        while attempt < self.config.retry_max_attempts && iteration < max_iterations {
            iteration += 1;

            // Round-robin server selection with failover
            let server_idx =
                (self.current_server.fetch_add(1, Ordering::Relaxed) as usize) % num_servers;
            let server = &servers[server_idx];

            let pool = match self.pools.get(server) {
                Some(p) => p,
                None => continue,
            };

            // Skip servers with open circuit breaker — does NOT
            // consume a retry attempt.
            if pool.circuit_state() == CircuitState::Open {
                debug!("Skipping {} (circuit breaker open)", server);
                continue;
            }

            // This is a real request attempt — count it.
            attempt += 1;

            // Get connection from pool
            let conn = match pool.get().await {
                Ok(c) => c,
                Err(e) => {
                    warn!("Failed to get connection from {}: {}", server, e);
                    pool.record_failure().await;
                    last_error = Some(e);
                    continue;
                }
            };

            // Execute operation with timeout
            let result = timeout(self.config.request_timeout, (operation.clone())(conn)).await;

            match result {
                Ok((conn, Ok(value))) => {
                    pool.record_success().await;
                    pool.put(conn).await;
                    return Ok(value);
                }
                Ok((conn, Err(e))) => {
                    self.total_failures.fetch_add(1, Ordering::Relaxed);
                    pool.record_failure().await;

                    // Determine if error is retryable
                    if is_retryable_error(&e) && attempt < self.config.retry_max_attempts {
                        let delay = calculate_backoff(
                            attempt - 1,
                            self.config.retry_initial_delay,
                            self.config.retry_max_delay,
                            self.config.retry_multiplier,
                        );
                        warn!(
                            "Retryable error on attempt {}: {}. Retrying in {:?}",
                            attempt, e, delay
                        );
                        // Do not return connection to pool after
                        // retryable I/O errors (ConnectionError, IoError) —
                        // the stream is likely corrupted / desynchronized.
                        // Dropping `conn` releases the semaphore permit and
                        // closes the underlying TCP connection.
                        last_error = Some(e);
                        drop(conn);
                        sleep(delay).await;
                        continue;
                    }

                    return Err(e);
                }
                Err(_) => {
                    self.total_failures.fetch_add(1, Ordering::Relaxed);
                    pool.record_failure().await;
                    warn!("Request timeout to {}", server);
                    last_error = Some(Error::TimeoutWithMessage(format!(
                        "Request timeout to {}",
                        server
                    )));

                    if attempt < self.config.retry_max_attempts {
                        let delay = calculate_backoff(
                            attempt - 1,
                            self.config.retry_initial_delay,
                            self.config.retry_max_delay,
                            self.config.retry_multiplier,
                        );
                        sleep(delay).await;
                    }
                }
            }
        }

        // Return the original error to preserve its kind
        // (e.g. Timeout, IoError) instead of wrapping everything in
        // ConnectionError which loses the discriminant for callers.
        match last_error {
            Some(e) => Err(e),
            None => Err(Error::AllServersUnavailable),
        }
    }

    /// Publish a message to a topic with automatic retries
    ///
    /// **WARNING: At-least-once semantics.** If the server commits the write
    /// but the response is lost (timeout/network partition), the retry produces
    /// a duplicate. For exactly-once, use `Client::publish_idempotent` or
    /// `IdempotentPublish` requests instead.
    pub async fn publish(&self, topic: impl Into<String>, value: impl Into<Bytes>) -> Result<u64> {
        let topic = topic.into();
        let value = value.into();

        self.execute_with_retry(move |mut conn| {
            let topic = topic.clone();
            let value = value.clone();
            async move {
                let result = conn.client.publish(&topic, value).await;
                (conn, result)
            }
        })
        .await
    }

    /// Publish a message with a key
    ///
    /// **WARNING: At-least-once semantics.** See [`Self::publish`] for details.
    pub async fn publish_with_key(
        &self,
        topic: impl Into<String>,
        key: Option<impl Into<Bytes>>,
        value: impl Into<Bytes>,
    ) -> Result<u64> {
        let topic = topic.into();
        let key: Option<Bytes> = key.map(|k| k.into());
        let value = value.into();

        self.execute_with_retry(move |mut conn| {
            let topic = topic.clone();
            let key = key.clone();
            let value = value.clone();
            async move {
                let result = conn.client.publish_with_key(&topic, key, value).await;
                (conn, result)
            }
        })
        .await
    }

    /// Consume messages with automatic retries
    ///
    /// Uses read_uncommitted isolation level (default).
    /// For transactional consumers, use [`Self::consume_with_isolation`] or [`Self::consume_read_committed`].
    pub async fn consume(
        &self,
        topic: impl Into<String>,
        partition: u32,
        offset: u64,
        max_messages: u32,
    ) -> Result<Vec<MessageData>> {
        self.consume_with_isolation(topic, partition, offset, max_messages, None)
            .await
    }

    /// Consume messages with specified isolation level and automatic retries
    ///
    /// # Arguments
    /// * `topic` - Topic name
    /// * `partition` - Partition number
    /// * `offset` - Starting offset
    /// * `max_messages` - Maximum messages to return
    /// * `isolation_level` - Transaction isolation level:
    ///   - `None` or `Some(0)` = read_uncommitted (default)
    ///   - `Some(1)` = read_committed (filters aborted transactions)
    pub async fn consume_with_isolation(
        &self,
        topic: impl Into<String>,
        partition: u32,
        offset: u64,
        max_messages: u32,
        isolation_level: Option<u8>,
    ) -> Result<Vec<MessageData>> {
        let topic = topic.into();

        self.execute_with_retry(move |mut conn| {
            let topic = topic.clone();
            async move {
                let result = conn
                    .client
                    .consume_with_isolation(
                        &topic,
                        partition,
                        offset,
                        max_messages,
                        isolation_level,
                    )
                    .await;
                (conn, result)
            }
        })
        .await
    }

    /// Consume messages with read_committed isolation level and automatic retries
    ///
    /// Only returns committed transactional messages; aborted transactions are filtered out.
    pub async fn consume_read_committed(
        &self,
        topic: impl Into<String>,
        partition: u32,
        offset: u64,
        max_messages: u32,
    ) -> Result<Vec<MessageData>> {
        self.consume_with_isolation(topic, partition, offset, max_messages, Some(1))
            .await
    }

    /// Create a topic with automatic retries
    pub async fn create_topic(
        &self,
        name: impl Into<String>,
        partitions: Option<u32>,
    ) -> Result<u32> {
        let name = name.into();

        self.execute_with_retry(move |mut conn| {
            let name = name.clone();
            async move {
                let result = conn.client.create_topic(&name, partitions).await;
                (conn, result)
            }
        })
        .await
    }

    /// List all topics
    pub async fn list_topics(&self) -> Result<Vec<String>> {
        self.execute_with_retry(|mut conn| async move {
            let result = conn.client.list_topics().await;
            (conn, result)
        })
        .await
    }

    /// Delete a topic
    pub async fn delete_topic(&self, name: impl Into<String>) -> Result<()> {
        let name = name.into();

        self.execute_with_retry(move |mut conn| {
            let name = name.clone();
            async move {
                let result = conn.client.delete_topic(&name).await;
                (conn, result)
            }
        })
        .await
    }

    /// Commit consumer offset
    pub async fn commit_offset(
        &self,
        consumer_group: impl Into<String>,
        topic: impl Into<String>,
        partition: u32,
        offset: u64,
    ) -> Result<()> {
        let consumer_group = consumer_group.into();
        let topic = topic.into();

        self.execute_with_retry(move |mut conn| {
            let consumer_group = consumer_group.clone();
            let topic = topic.clone();
            async move {
                let result = conn
                    .client
                    .commit_offset(&consumer_group, &topic, partition, offset)
                    .await;
                (conn, result)
            }
        })
        .await
    }

    /// Get consumer offset
    pub async fn get_offset(
        &self,
        consumer_group: impl Into<String>,
        topic: impl Into<String>,
        partition: u32,
    ) -> Result<Option<u64>> {
        let consumer_group = consumer_group.into();
        let topic = topic.into();

        self.execute_with_retry(move |mut conn| {
            let consumer_group = consumer_group.clone();
            let topic = topic.clone();
            async move {
                let result = conn
                    .client
                    .get_offset(&consumer_group, &topic, partition)
                    .await;
                (conn, result)
            }
        })
        .await
    }

    /// Get offset bounds (earliest, latest)
    pub async fn get_offset_bounds(
        &self,
        topic: impl Into<String>,
        partition: u32,
    ) -> Result<(u64, u64)> {
        let topic = topic.into();

        self.execute_with_retry(move |mut conn| {
            let topic = topic.clone();
            async move {
                let result = conn.client.get_offset_bounds(&topic, partition).await;
                (conn, result)
            }
        })
        .await
    }

    /// Get topic metadata
    pub async fn get_metadata(&self, topic: impl Into<String>) -> Result<(String, u32)> {
        let topic = topic.into();

        self.execute_with_retry(move |mut conn| {
            let topic = topic.clone();
            async move {
                let result = conn.client.get_metadata(&topic).await;
                (conn, result)
            }
        })
        .await
    }

    /// Ping (health check)
    pub async fn ping(&self) -> Result<()> {
        self.execute_with_retry(|mut conn| async move {
            let result = conn.client.ping().await;
            (conn, result)
        })
        .await
    }

    /// Get client statistics
    pub fn stats(&self) -> ClientStats {
        let pools: Vec<_> = self
            .pools
            .iter()
            .map(|(addr, pool)| ServerStats {
                address: addr.clone(),
                circuit_state: pool.circuit_state(),
            })
            .collect();

        ClientStats {
            total_requests: self.total_requests.load(Ordering::Relaxed),
            total_failures: self.total_failures.load(Ordering::Relaxed),
            servers: pools,
        }
    }
}

impl Drop for ResilientClient {
    fn drop(&mut self) {
        if let Some(handle) = self._health_check_handle.take() {
            handle.abort();
        }
    }
}

/// Client statistics
#[derive(Debug, Clone)]
pub struct ClientStats {
    pub total_requests: u64,
    pub total_failures: u64,
    pub servers: Vec<ServerStats>,
}

/// Per-server statistics
#[derive(Debug, Clone)]
pub struct ServerStats {
    pub address: String,
    pub circuit_state: CircuitState,
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Determine if an error is retryable
///
/// Authentication failures are NOT retryable — they indicate
/// permanent credential problems, not transient network issues. Retrying
/// them wastes time and may trigger account lockout policies.
fn is_retryable_error(error: &Error) -> bool {
    error.is_retriable()
}

/// Calculate exponential backoff with jitter
fn calculate_backoff(
    attempt: u32,
    initial_delay: Duration,
    max_delay: Duration,
    multiplier: f64,
) -> Duration {
    let base_delay = initial_delay.as_millis() as f64 * multiplier.powi(attempt as i32);
    let capped_delay = base_delay.min(max_delay.as_millis() as f64);

    // Add jitter (±25%)
    let jitter = (rand_simple() * 0.5 - 0.25) * capped_delay;
    let final_delay = (capped_delay + jitter).max(0.0);

    Duration::from_millis(final_delay as u64)
}

/// Random number generator (0.0 - 1.0).
///
/// Uses `rand::random::<f64>()` instead of `subsec_nanos()` which
/// is deterministic within the same millisecond, causing thundering herd
/// when multiple clients have synchronized clocks.
fn rand_simple() -> f64 {
    rand::random::<f64>()
}

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

    #[test]
    fn test_config_builder() {
        let config = ResilientClientConfig::builder()
            .bootstrap_servers(vec!["server1:9092".to_string(), "server2:9092".to_string()])
            .pool_size(10)
            .retry_max_attempts(5)
            .circuit_breaker_threshold(10)
            .connection_timeout(Duration::from_secs(5))
            .build();

        assert_eq!(config.bootstrap_servers.len(), 2);
        assert_eq!(config.pool_size, 10);
        assert_eq!(config.retry_max_attempts, 5);
        assert_eq!(config.circuit_breaker_threshold, 10);
        assert_eq!(config.connection_timeout, Duration::from_secs(5));
    }

    #[test]
    fn test_calculate_backoff() {
        let initial = Duration::from_millis(100);
        let max = Duration::from_secs(10);

        // First attempt
        let delay = calculate_backoff(0, initial, max, 2.0);
        assert!(delay.as_millis() >= 75 && delay.as_millis() <= 125);

        // Second attempt (should be ~200ms)
        let delay = calculate_backoff(1, initial, max, 2.0);
        assert!(delay.as_millis() >= 150 && delay.as_millis() <= 250);

        // Many attempts (should cap at max)
        let delay = calculate_backoff(20, initial, max, 2.0);
        assert!(delay <= max + Duration::from_millis(2500)); // max + jitter
    }

    #[test]
    fn test_is_retryable_error() {
        // Transient errors — should be retried
        assert!(is_retryable_error(&Error::ConnectionError("test".into())));
        assert!(is_retryable_error(&Error::IoError(
            std::io::ErrorKind::ConnectionReset,
            "test".into()
        )));
        assert!(is_retryable_error(&Error::CircuitBreakerOpen(
            "test".into()
        )));
        assert!(is_retryable_error(&Error::Timeout));
        assert!(is_retryable_error(&Error::PoolExhausted(
            "pool full".into()
        )));
        // Generic server errors without a permanent prefix are transient
        assert!(is_retryable_error(&Error::ServerError(
            "NOT_LEADER: try another broker".into()
        )));
        // InvalidResponse is transient (network corruption, partial read)
        assert!(is_retryable_error(&Error::InvalidResponse));

        // Permanent errors — must NOT be retried
        assert!(!is_retryable_error(&Error::AuthenticationFailed(
            "bad password".into()
        )));
        assert!(!is_retryable_error(&Error::ServerError(
            "PRODUCER_FENCED: epoch mismatch".into()
        )));
        assert!(!is_retryable_error(&Error::ServerError(
            "INVALID_TOPIC: bad name".into()
        )));
        assert!(!is_retryable_error(&Error::ConfigError(
            "bad config".into()
        )));
    }

    #[test]
    fn test_circuit_state() {
        let config = Arc::new(ResilientClientConfig::default());
        let cb = CircuitBreaker::new(config);

        assert_eq!(cb.get_state(), CircuitState::Closed);
    }

    // ========================================================================
    // Circuit Breaker State Machine Tests
    // ========================================================================

    #[tokio::test]
    async fn test_circuit_breaker_starts_closed() {
        let config = Arc::new(ResilientClientConfig::default());
        let cb = CircuitBreaker::new(config);

        assert_eq!(cb.get_state(), CircuitState::Closed);
        assert!(cb.allow_request().await);
    }

    #[tokio::test]
    async fn test_circuit_breaker_opens_after_threshold_failures() {
        let config = Arc::new(
            ResilientClientConfig::builder()
                .circuit_breaker_threshold(3)
                .build(),
        );
        let cb = CircuitBreaker::new(config);

        // Should be closed initially
        assert_eq!(cb.get_state(), CircuitState::Closed);

        // Record failures up to threshold - 1
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Closed);
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Closed);

        // Threshold reached - should open
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Open);
        assert!(!cb.allow_request().await);
    }

    #[tokio::test]
    async fn test_circuit_breaker_success_resets_failure_count() {
        let config = Arc::new(
            ResilientClientConfig::builder()
                .circuit_breaker_threshold(3)
                .build(),
        );
        let cb = CircuitBreaker::new(config);

        // Record some failures
        cb.record_failure().await;
        cb.record_failure().await;
        assert_eq!(cb.failure_count.load(Ordering::SeqCst), 2);

        // Success should reset
        cb.record_success().await;
        assert_eq!(cb.failure_count.load(Ordering::SeqCst), 0);
        assert_eq!(cb.get_state(), CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_half_open_after_timeout() {
        let config = Arc::new(
            ResilientClientConfig::builder()
                .circuit_breaker_threshold(1)
                .circuit_breaker_timeout(Duration::from_millis(50))
                .build(),
        );
        let cb = CircuitBreaker::new(config);

        // Open the circuit
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Open);
        assert!(!cb.allow_request().await);

        // Wait for timeout
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Should transition to half-open and allow request
        assert!(cb.allow_request().await);
        assert_eq!(cb.get_state(), CircuitState::HalfOpen);
    }

    #[tokio::test]
    async fn test_circuit_breaker_closes_after_success_threshold() {
        let config = Arc::new(
            ResilientClientConfig::builder()
                .circuit_breaker_threshold(1)
                .circuit_breaker_timeout(Duration::from_millis(10))
                .build(),
        );
        // Note: default success threshold is 2
        let cb = CircuitBreaker::new(config);

        // Open the circuit
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Open);

        // Wait for timeout and transition to half-open
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert!(cb.allow_request().await);
        assert_eq!(cb.get_state(), CircuitState::HalfOpen);

        // First success - still half-open
        cb.record_success().await;
        assert_eq!(cb.get_state(), CircuitState::HalfOpen);

        // Second success - should close
        cb.record_success().await;
        assert_eq!(cb.get_state(), CircuitState::Closed);
    }

    #[tokio::test]
    async fn test_circuit_breaker_failure_in_half_open_reopens() {
        let config = Arc::new(
            ResilientClientConfig::builder()
                .circuit_breaker_threshold(1)
                .circuit_breaker_timeout(Duration::from_millis(10))
                .build(),
        );
        let cb = CircuitBreaker::new(config);

        // Open the circuit
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Open);

        // Wait for timeout and transition to half-open
        tokio::time::sleep(Duration::from_millis(20)).await;
        assert!(cb.allow_request().await);
        assert_eq!(cb.get_state(), CircuitState::HalfOpen);

        // Failure in half-open should reopen
        cb.record_failure().await;
        assert_eq!(cb.get_state(), CircuitState::Open);
    }

    // ========================================================================
    // Connection Pool Tests
    // ========================================================================

    #[test]
    fn test_pool_config_defaults() {
        let config = ResilientClientConfig::default();
        assert_eq!(config.pool_size, 5);
        assert_eq!(config.retry_max_attempts, 3);
        assert_eq!(config.circuit_breaker_threshold, 5);
        assert_eq!(config.circuit_breaker_success_threshold, 2);
    }

    #[tokio::test]
    async fn test_pool_semaphore_limits_concurrent_connections() {
        let config = Arc::new(ResilientClientConfig::builder().pool_size(2).build());
        let pool = ConnectionPool::new("localhost:9999".to_string(), config);

        // Verify semaphore has correct permits
        // Note: can't directly test without a server, but verify pool was created
        assert_eq!(pool.addr, "localhost:9999");
    }

    // ========================================================================
    // Retry Logic Tests
    // ========================================================================

    #[test]
    fn test_backoff_respects_max_delay() {
        let initial = Duration::from_millis(100);
        let max = Duration::from_secs(1);

        // Even with high attempt count, should not exceed max + jitter
        for attempt in 10..20 {
            let delay = calculate_backoff(attempt, initial, max, 2.0);
            // Max jitter is 25% of max = 250ms
            assert!(delay <= max + Duration::from_millis(250));
        }
    }

    #[test]
    fn test_backoff_exponential_growth() {
        let initial = Duration::from_millis(100);
        let max = Duration::from_secs(100);

        // Get base delays (center of jitter range)
        let delay0 = calculate_backoff(0, initial, max, 2.0);
        let delay1 = calculate_backoff(1, initial, max, 2.0);
        let delay2 = calculate_backoff(2, initial, max, 2.0);

        // Each should be roughly 2x the previous (accounting for jitter)
        // delay0 ≈ 100ms, delay1 ≈ 200ms, delay2 ≈ 400ms
        assert!(delay1 > delay0 / 2); // Very loose check due to jitter
        assert!(delay2 > delay1 / 2);
    }

    // ========================================================================
    // Client Statistics Tests
    // ========================================================================

    #[test]
    fn test_client_stats_structure() {
        let stats = ClientStats {
            total_requests: 100,
            total_failures: 5,
            servers: vec![
                ServerStats {
                    address: "server1:9092".to_string(),
                    circuit_state: CircuitState::Closed,
                },
                ServerStats {
                    address: "server2:9092".to_string(),
                    circuit_state: CircuitState::Open,
                },
            ],
        };

        assert_eq!(stats.total_requests, 100);
        assert_eq!(stats.total_failures, 5);
        assert_eq!(stats.servers.len(), 2);
        assert_eq!(stats.servers[0].circuit_state, CircuitState::Closed);
        assert_eq!(stats.servers[1].circuit_state, CircuitState::Open);
    }
}