celers-core 0.2.0

Core traits and types for CeleRS distributed task queue
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
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap
)]
//! Rate Limiting for Task Execution
//!
//! This module provides rate limiting capabilities for controlling task execution rates.
//! It supports multiple algorithms:
//!
//! - **Token Bucket**: Classic algorithm that allows bursts up to bucket capacity
//! - **Sliding Window**: Tracks actual execution counts within a time window
//!
//! # Example
//!
//! ```rust
//! use celers_core::rate_limit::{RateLimiter, TokenBucket, RateLimitConfig};
//! use std::time::Duration;
//!
//! // Create a rate limiter allowing 10 tasks per second with burst capacity of 20
//! let config = RateLimitConfig::new(10.0).with_burst(20);
//! let mut limiter = TokenBucket::new(config);
//!
//! // Check if we can execute a task
//! if limiter.try_acquire() {
//!     println!("Task can execute");
//! } else {
//!     println!("Rate limited, wait {:?}", limiter.time_until_available());
//! }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Configuration for rate limiting
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Maximum tasks per second
    pub rate: f64,
    /// Burst capacity (max tokens in bucket)
    /// If None, defaults to rate
    pub burst: Option<u32>,
    /// Whether to use sliding window algorithm instead of token bucket
    pub sliding_window: bool,
    /// Window size for sliding window algorithm (in seconds)
    pub window_size: u64,
}

impl RateLimitConfig {
    /// Create a new rate limit configuration
    ///
    /// # Arguments
    ///
    /// * `rate` - Maximum tasks per second
    #[must_use]
    pub fn new(rate: f64) -> Self {
        Self {
            rate,
            burst: None,
            sliding_window: false,
            window_size: 1,
        }
    }

    /// Set the burst capacity (max tokens in bucket)
    #[must_use]
    pub fn with_burst(mut self, burst: u32) -> Self {
        self.burst = Some(burst);
        self
    }

    /// Use sliding window algorithm instead of token bucket
    #[must_use]
    pub fn with_sliding_window(mut self, window_size: u64) -> Self {
        self.sliding_window = true;
        self.window_size = window_size;
        self
    }

    /// Get the effective burst capacity
    #[must_use]
    #[inline]
    pub fn effective_burst(&self) -> u32 {
        self.burst.unwrap_or(self.rate.ceil() as u32)
    }
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            rate: 100.0, // 100 tasks per second default
            burst: None,
            sliding_window: false,
            window_size: 1,
        }
    }
}

/// Trait for rate limiter implementations
pub trait RateLimiter: Send + Sync {
    /// Try to acquire a permit to execute a task
    ///
    /// Returns `true` if the task can execute immediately, `false` if rate limited
    fn try_acquire(&mut self) -> bool;

    /// Acquire a permit, blocking until available
    ///
    /// Returns the time waited
    fn acquire(&mut self) -> Duration;

    /// Get the time until a permit will be available
    fn time_until_available(&self) -> Duration;

    /// Get the current number of available permits
    fn available_permits(&self) -> u32;

    /// Reset the rate limiter to its initial state
    fn reset(&mut self);

    /// Update the rate limit configuration
    fn set_rate(&mut self, rate: f64);

    /// Get current configuration
    fn config(&self) -> &RateLimitConfig;
}

/// Token bucket rate limiter
///
/// The token bucket algorithm works by:
/// - Adding tokens at a fixed rate (rate per second)
/// - Consuming one token per task execution
/// - Allowing bursts up to the bucket capacity
///
/// This is the default and recommended rate limiter for most use cases.
#[derive(Debug)]
pub struct TokenBucket {
    config: RateLimitConfig,
    /// Current number of tokens in the bucket
    tokens: f64,
    /// Last time tokens were refilled
    last_refill: Instant,
}

impl TokenBucket {
    /// Create a new token bucket rate limiter
    #[must_use]
    pub fn new(config: RateLimitConfig) -> Self {
        let tokens = f64::from(config.effective_burst());
        Self {
            config,
            tokens,
            last_refill: Instant::now(),
        }
    }

    /// Create a token bucket with default configuration
    #[must_use]
    pub fn with_rate(rate: f64) -> Self {
        Self::new(RateLimitConfig::new(rate))
    }

    /// Refill tokens based on elapsed time
    #[inline]
    fn refill(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.last_refill);
        let new_tokens = elapsed.as_secs_f64() * self.config.rate;
        let max_tokens = f64::from(self.config.effective_burst());
        self.tokens = (self.tokens + new_tokens).min(max_tokens);
        self.last_refill = now;
    }
}

impl RateLimiter for TokenBucket {
    fn try_acquire(&mut self) -> bool {
        self.refill();
        if self.tokens >= 1.0 {
            self.tokens -= 1.0;
            true
        } else {
            false
        }
    }

    fn acquire(&mut self) -> Duration {
        let start = Instant::now();
        while !self.try_acquire() {
            let wait_time = self.time_until_available();
            if wait_time > Duration::ZERO {
                std::thread::sleep(wait_time);
            }
        }
        start.elapsed()
    }

    fn time_until_available(&self) -> Duration {
        if self.tokens >= 1.0 {
            Duration::ZERO
        } else {
            let tokens_needed = 1.0 - self.tokens;
            let seconds = tokens_needed / self.config.rate;
            Duration::from_secs_f64(seconds)
        }
    }

    fn available_permits(&self) -> u32 {
        self.tokens.floor() as u32
    }

    fn reset(&mut self) {
        self.tokens = f64::from(self.config.effective_burst());
        self.last_refill = Instant::now();
    }

    fn set_rate(&mut self, rate: f64) {
        self.config.rate = rate;
    }

    fn config(&self) -> &RateLimitConfig {
        &self.config
    }
}

/// Sliding window rate limiter
///
/// Tracks actual execution timestamps and counts executions within a sliding window.
/// More accurate than token bucket but uses more memory.
#[derive(Debug)]
pub struct SlidingWindow {
    config: RateLimitConfig,
    /// Timestamps of recent executions
    timestamps: Vec<Instant>,
}

impl SlidingWindow {
    /// Create a new sliding window rate limiter
    #[must_use]
    pub fn new(config: RateLimitConfig) -> Self {
        Self {
            config,
            timestamps: Vec::new(),
        }
    }

    /// Create a sliding window limiter with default configuration
    #[must_use]
    pub fn with_rate(rate: f64, window_size: u64) -> Self {
        let config = RateLimitConfig::new(rate).with_sliding_window(window_size);
        Self::new(config)
    }

    /// Clean up old timestamps outside the window
    #[inline]
    fn cleanup(&mut self) {
        let window = Duration::from_secs(self.config.window_size);
        let cutoff = Instant::now()
            .checked_sub(window)
            .expect("window duration should be valid for subtraction");
        self.timestamps.retain(|&t| t > cutoff);
    }

    /// Get the maximum allowed executions in the window
    #[inline]
    fn max_executions(&self) -> usize {
        (self.config.rate * self.config.window_size as f64).ceil() as usize
    }
}

impl RateLimiter for SlidingWindow {
    fn try_acquire(&mut self) -> bool {
        self.cleanup();
        if self.timestamps.len() < self.max_executions() {
            self.timestamps.push(Instant::now());
            true
        } else {
            false
        }
    }

    fn acquire(&mut self) -> Duration {
        let start = Instant::now();
        while !self.try_acquire() {
            let wait_time = self.time_until_available();
            if wait_time > Duration::ZERO {
                std::thread::sleep(wait_time);
            }
        }
        start.elapsed()
    }

    fn time_until_available(&self) -> Duration {
        if self.timestamps.len() < self.max_executions() {
            Duration::ZERO
        } else if let Some(&oldest) = self.timestamps.first() {
            let window = Duration::from_secs(self.config.window_size);
            let expires = oldest + window;
            let now = Instant::now();
            if expires > now {
                expires - now
            } else {
                Duration::ZERO
            }
        } else {
            Duration::ZERO
        }
    }

    fn available_permits(&self) -> u32 {
        let max = self.max_executions();
        let current = self.timestamps.len();
        (max.saturating_sub(current)) as u32
    }

    fn reset(&mut self) {
        self.timestamps.clear();
    }

    fn set_rate(&mut self, rate: f64) {
        self.config.rate = rate;
    }

    fn config(&self) -> &RateLimitConfig {
        &self.config
    }
}

/// Per-task rate limiter manager
///
/// Manages rate limiters for multiple task types, allowing different
/// rate limits per task name.
#[derive(Debug)]
pub struct TaskRateLimiter {
    /// Per-task rate limiters (`task_name` -> limiter)
    limiters: HashMap<String, TokenBucket>,
    /// Default rate limit for tasks without specific configuration
    default_config: Option<RateLimitConfig>,
}

impl TaskRateLimiter {
    /// Create a new task rate limiter manager
    #[must_use]
    pub fn new() -> Self {
        Self {
            limiters: HashMap::new(),
            default_config: None,
        }
    }

    /// Create with a default rate limit for all tasks
    #[must_use]
    pub fn with_default(config: RateLimitConfig) -> Self {
        Self {
            limiters: HashMap::new(),
            default_config: Some(config),
        }
    }

    /// Set rate limit for a specific task type
    pub fn set_task_rate(&mut self, task_name: impl Into<String>, config: RateLimitConfig) {
        let name = task_name.into();
        self.limiters.insert(name, TokenBucket::new(config));
    }

    /// Remove rate limit for a specific task type
    pub fn remove_task_rate(&mut self, task_name: &str) {
        self.limiters.remove(task_name);
    }

    /// Try to acquire a permit for a specific task
    ///
    /// Returns `true` if the task can execute, `false` if rate limited
    pub fn try_acquire(&mut self, task_name: &str) -> bool {
        if let Some(limiter) = self.limiters.get_mut(task_name) {
            limiter.try_acquire()
        } else if let Some(ref config) = self.default_config {
            // Create a limiter for this task using default config
            let mut limiter = TokenBucket::new(config.clone());
            let result = limiter.try_acquire();
            self.limiters.insert(task_name.to_string(), limiter);
            result
        } else {
            // No rate limit configured
            true
        }
    }

    /// Get time until a task can be executed
    #[must_use]
    pub fn time_until_available(&self, task_name: &str) -> Duration {
        if let Some(limiter) = self.limiters.get(task_name) {
            limiter.time_until_available()
        } else {
            Duration::ZERO
        }
    }

    /// Check if a task type has a rate limit configured
    #[inline]
    #[must_use]
    pub fn has_rate_limit(&self, task_name: &str) -> bool {
        self.limiters.contains_key(task_name) || self.default_config.is_some()
    }

    /// Get the rate limit configuration for a task
    #[inline]
    pub fn get_rate_limit(&self, task_name: &str) -> Option<&RateLimitConfig> {
        self.limiters
            .get(task_name)
            .map(RateLimiter::config)
            .or(self.default_config.as_ref())
    }

    /// Reset all rate limiters
    pub fn reset_all(&mut self) {
        for limiter in self.limiters.values_mut() {
            limiter.reset();
        }
    }
}

impl Default for TaskRateLimiter {
    fn default() -> Self {
        Self::new()
    }
}

/// Thread-safe per-worker rate limiter
///
/// Wraps a rate limiter for safe concurrent access from multiple worker threads.
#[derive(Debug, Clone)]
pub struct WorkerRateLimiter {
    inner: Arc<RwLock<TaskRateLimiter>>,
}

impl WorkerRateLimiter {
    /// Create a new worker rate limiter
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::new(TaskRateLimiter::new())),
        }
    }

    /// Create with a default rate limit
    #[must_use]
    pub fn with_default(config: RateLimitConfig) -> Self {
        Self {
            inner: Arc::new(RwLock::new(TaskRateLimiter::with_default(config))),
        }
    }

    /// Set rate limit for a specific task type
    pub fn set_task_rate(&self, task_name: impl Into<String>, config: RateLimitConfig) {
        if let Ok(mut guard) = self.inner.write() {
            guard.set_task_rate(task_name, config);
        }
    }

    /// Remove rate limit for a specific task type
    pub fn remove_task_rate(&self, task_name: &str) {
        if let Ok(mut guard) = self.inner.write() {
            guard.remove_task_rate(task_name);
        }
    }

    /// Try to acquire a permit for a specific task
    #[must_use]
    pub fn try_acquire(&self, task_name: &str) -> bool {
        if let Ok(mut guard) = self.inner.write() {
            guard.try_acquire(task_name)
        } else {
            // If lock is poisoned, allow execution
            true
        }
    }

    /// Get time until a task can be executed
    #[must_use]
    pub fn time_until_available(&self, task_name: &str) -> Duration {
        if let Ok(guard) = self.inner.read() {
            guard.time_until_available(task_name)
        } else {
            Duration::ZERO
        }
    }

    /// Check if a task type has a rate limit configured
    #[inline]
    #[must_use]
    pub fn has_rate_limit(&self, task_name: &str) -> bool {
        if let Ok(guard) = self.inner.read() {
            guard.has_rate_limit(task_name)
        } else {
            false
        }
    }

    /// Reset all rate limiters
    pub fn reset_all(&self) {
        if let Ok(mut guard) = self.inner.write() {
            guard.reset_all();
        }
    }
}

impl Default for WorkerRateLimiter {
    fn default() -> Self {
        Self::new()
    }
}

/// Create a rate limiter from configuration
#[must_use]
pub fn create_rate_limiter(config: RateLimitConfig) -> Box<dyn RateLimiter> {
    if config.sliding_window {
        Box::new(SlidingWindow::new(config))
    } else {
        Box::new(TokenBucket::new(config))
    }
}

/// Distributed rate limiting coordination
///
/// This module provides distributed rate limiting across multiple workers,
/// allowing rate limits to be enforced cluster-wide rather than per-worker.
///
/// # Features
///
/// - **Cluster-wide rate limiting**: Coordinate rate limits across all workers
/// - **Redis backend**: Use Redis for distributed state storage
/// - **Token bucket algorithm**: Distributed token bucket with atomic operations
/// - **Sliding window algorithm**: Distributed sliding window using sorted sets
/// - **Fallback support**: Graceful degradation to local rate limiting on failure
/// - **TTL support**: Automatic cleanup of stale data
///
/// # Example
///
/// ```rust,ignore
/// use celers_core::rate_limit::{DistributedRateLimiter, RateLimitConfig};
///
/// // Create a distributed rate limiter backed by Redis
/// let config = RateLimitConfig::new(100.0).with_burst(200);
/// let limiter = DistributedRateLimiter::redis(
///     "redis://localhost:6379",
///     "my_task",
///     config,
/// ).await?;
///
/// // Try to acquire a permit across all workers
/// if limiter.try_acquire().await? {
///     println!("Task can execute");
/// } else {
///     println!("Rate limited cluster-wide");
/// }
/// ```
use async_trait::async_trait;

/// Trait for distributed rate limiter backends
///
/// Implementations should provide atomic operations for rate limiting
/// across multiple processes/workers.
#[async_trait]
pub trait DistributedRateLimiter: Send + Sync {
    /// Try to acquire a permit atomically
    ///
    /// Returns `Ok(true)` if acquired, `Ok(false)` if rate limited,
    /// or an error if the backend is unavailable.
    async fn try_acquire(&self) -> crate::Result<bool>;

    /// Get the time until a permit will be available
    ///
    /// Returns `Ok(Duration)` with the wait time, or an error if unavailable.
    async fn time_until_available(&self) -> crate::Result<Duration>;

    /// Get the current number of available permits
    ///
    /// Returns `Ok(count)` or an error if unavailable.
    async fn available_permits(&self) -> crate::Result<u32>;

    /// Reset the distributed rate limiter
    ///
    /// Clears all state in the distributed backend.
    async fn reset(&self) -> crate::Result<()>;

    /// Update the rate limit configuration
    ///
    /// Note: This updates the local configuration. Distributed backends
    /// may need additional coordination to sync configuration changes.
    async fn set_rate(&self, rate: f64) -> crate::Result<()>;

    /// Get current configuration
    fn config(&self) -> &RateLimitConfig;

    /// Get the backend name (for diagnostics)
    fn backend_name(&self) -> &str;
}

/// Distributed rate limiter state
///
/// Stores rate limiting state in a distributed backend (e.g., Redis)
/// for coordination across multiple workers.
#[derive(Debug, Clone)]
pub struct DistributedRateLimiterState {
    /// Redis key for storing rate limit state
    pub key: String,
    /// Rate limit configuration
    pub config: RateLimitConfig,
    /// Local fallback limiter (used if distributed backend is unavailable)
    pub fallback: Arc<RwLock<TokenBucket>>,
}

impl DistributedRateLimiterState {
    /// Create a new distributed rate limiter state
    ///
    /// # Arguments
    ///
    /// * `key` - Redis key for storing rate limit state
    /// * `config` - Rate limit configuration
    #[must_use]
    pub fn new(key: String, config: RateLimitConfig) -> Self {
        let fallback = Arc::new(RwLock::new(TokenBucket::new(config.clone())));
        Self {
            key,
            config,
            fallback,
        }
    }

    /// Get the Redis key for token count
    ///
    /// Used by backend implementations to store token count.
    #[inline]
    #[must_use]
    pub fn token_key(&self) -> String {
        format!("{}:tokens", self.key)
    }

    /// Get the Redis key for last refill timestamp
    ///
    /// Used by backend implementations to store last refill time.
    #[inline]
    #[must_use]
    pub fn refill_key(&self) -> String {
        format!("{}:refill", self.key)
    }

    /// Get the Redis key for sliding window
    ///
    /// Used by backend implementations to store sliding window data.
    #[inline]
    #[must_use]
    pub fn window_key(&self) -> String {
        format!("{}:window", self.key)
    }

    /// Try to acquire using local fallback
    fn try_acquire_fallback(&self) -> bool {
        if let Ok(mut guard) = self.fallback.write() {
            guard.try_acquire()
        } else {
            // If lock is poisoned, allow execution
            true
        }
    }
}

/// Distributed token bucket implementation
///
/// Uses atomic operations in a distributed backend (e.g., Redis Lua scripts)
/// to implement token bucket algorithm across multiple workers.
///
/// # Redis Implementation
///
/// The token bucket is implemented using Redis with two keys:
/// - `{key}:tokens` - Current token count (float)
/// - `{key}:refill` - Last refill timestamp (integer, milliseconds since epoch)
///
/// A Lua script performs atomic token refill and acquisition:
/// 1. Calculate elapsed time since last refill
/// 2. Add tokens based on elapsed time and rate
/// 3. Cap tokens at burst capacity
/// 4. Attempt to consume 1 token
/// 5. Update last refill timestamp
///
/// # Example Lua Script
///
/// ```lua
/// local tokens_key = KEYS[1]
/// local refill_key = KEYS[2]
/// local rate = tonumber(ARGV[1])
/// local burst = tonumber(ARGV[2])
/// local now = tonumber(ARGV[3])
///
/// local last_refill = redis.call('GET', refill_key)
/// local tokens = redis.call('GET', tokens_key)
///
/// if not tokens then
///     tokens = burst
/// else
///     tokens = tonumber(tokens)
/// end
///
/// if last_refill then
///     local elapsed = (now - tonumber(last_refill)) / 1000.0
///     tokens = math.min(tokens + elapsed * rate, burst)
/// end
///
/// if tokens >= 1.0 then
///     tokens = tokens - 1.0
///     redis.call('SET', tokens_key, tostring(tokens))
///     redis.call('SET', refill_key, tostring(now))
///     return 1
/// else
///     redis.call('SET', tokens_key, tostring(tokens))
///     redis.call('SET', refill_key, tostring(now))
///     return 0
/// end
/// ```
#[derive(Debug, Clone)]
pub struct DistributedTokenBucketSpec {
    state: DistributedRateLimiterState,
}

impl DistributedTokenBucketSpec {
    /// Create a new distributed token bucket specification
    ///
    /// This creates the specification for a distributed token bucket.
    /// Actual implementation requires a backend (e.g., Redis client).
    #[must_use]
    pub fn new(key: String, config: RateLimitConfig) -> Self {
        Self {
            state: DistributedRateLimiterState::new(key, config),
        }
    }

    /// Get the Lua script for atomic token acquisition
    ///
    /// This script should be loaded into Redis using SCRIPT LOAD
    /// and executed with EVALSHA for better performance.
    #[must_use]
    pub fn lua_acquire_script() -> &'static str {
        r"
        local tokens_key = KEYS[1]
        local refill_key = KEYS[2]
        local rate = tonumber(ARGV[1])
        local burst = tonumber(ARGV[2])
        local now = tonumber(ARGV[3])
        local ttl = tonumber(ARGV[4])

        local last_refill = redis.call('GET', refill_key)
        local tokens = redis.call('GET', tokens_key)

        if not tokens then
            tokens = burst
        else
            tokens = tonumber(tokens)
        end

        if last_refill then
            local elapsed = (now - tonumber(last_refill)) / 1000.0
            tokens = math.min(tokens + elapsed * rate, burst)
        end

        if tokens >= 1.0 then
            tokens = tokens - 1.0
            redis.call('SET', tokens_key, tostring(tokens), 'EX', ttl)
            redis.call('SET', refill_key, tostring(now), 'EX', ttl)
            return {1, tokens}
        else
            redis.call('SET', tokens_key, tostring(tokens), 'EX', ttl)
            redis.call('SET', refill_key, tostring(now), 'EX', ttl)
            return {0, tokens}
        end
        "
    }

    /// Get the Lua script for querying available permits
    #[must_use]
    pub fn lua_available_script() -> &'static str {
        r"
        local tokens_key = KEYS[1]
        local refill_key = KEYS[2]
        local rate = tonumber(ARGV[1])
        local burst = tonumber(ARGV[2])
        local now = tonumber(ARGV[3])

        local last_refill = redis.call('GET', refill_key)
        local tokens = redis.call('GET', tokens_key)

        if not tokens then
            return burst
        else
            tokens = tonumber(tokens)
        end

        if last_refill then
            local elapsed = (now - tonumber(last_refill)) / 1000.0
            tokens = math.min(tokens + elapsed * rate, burst)
        end

        return math.floor(tokens)
        "
    }

    /// Get the state for implementing the distributed backend
    #[inline]
    #[must_use]
    pub fn state(&self) -> &DistributedRateLimiterState {
        &self.state
    }

    /// Try to acquire using local fallback
    #[must_use]
    pub fn try_acquire_fallback(&self) -> bool {
        self.state.try_acquire_fallback()
    }
}

/// Distributed sliding window implementation
///
/// Uses sorted sets in a distributed backend (e.g., Redis ZSET)
/// to implement sliding window algorithm across multiple workers.
///
/// # Redis Implementation
///
/// The sliding window is implemented using Redis sorted set:
/// - `{key}:window` - Sorted set of timestamps (score = timestamp, member = UUID)
///
/// Operations:
/// 1. **Acquire**: Add current timestamp to sorted set if count < limit
/// 2. **Cleanup**: Remove timestamps outside the window using ZREMRANGEBYSCORE
/// 3. **Count**: Count timestamps within window using ZCOUNT
///
/// # Example Lua Script
///
/// ```lua
/// local window_key = KEYS[1]
/// local now = tonumber(ARGV[1])
/// local window_size = tonumber(ARGV[2])
/// local max_count = tonumber(ARGV[3])
/// local uuid = ARGV[4]
///
/// local cutoff = now - window_size * 1000
/// redis.call('ZREMRANGEBYSCORE', window_key, '-inf', cutoff)
///
/// local count = redis.call('ZCARD', window_key)
/// if count < max_count then
///     redis.call('ZADD', window_key, now, uuid)
///     redis.call('EXPIRE', window_key, window_size * 2)
///     return 1
/// else
///     return 0
/// end
/// ```
#[derive(Debug, Clone)]
pub struct DistributedSlidingWindowSpec {
    state: DistributedRateLimiterState,
}

impl DistributedSlidingWindowSpec {
    /// Create a new distributed sliding window specification
    #[must_use]
    pub fn new(key: String, config: RateLimitConfig) -> Self {
        Self {
            state: DistributedRateLimiterState::new(key, config),
        }
    }

    /// Get the Lua script for atomic window acquisition
    #[must_use]
    pub fn lua_acquire_script() -> &'static str {
        r"
        local window_key = KEYS[1]
        local now = tonumber(ARGV[1])
        local window_size = tonumber(ARGV[2])
        local max_count = tonumber(ARGV[3])
        local uuid = ARGV[4]

        local cutoff = now - window_size * 1000
        redis.call('ZREMRANGEBYSCORE', window_key, '-inf', cutoff)

        local count = redis.call('ZCARD', window_key)
        if count < max_count then
            redis.call('ZADD', window_key, now, uuid)
            redis.call('EXPIRE', window_key, window_size * 2)
            return {1, max_count - count - 1}
        else
            return {0, 0}
        end
        "
    }

    /// Get the Lua script for querying available permits
    #[must_use]
    pub fn lua_available_script() -> &'static str {
        r"
        local window_key = KEYS[1]
        local now = tonumber(ARGV[1])
        local window_size = tonumber(ARGV[2])
        local max_count = tonumber(ARGV[3])

        local cutoff = now - window_size * 1000
        redis.call('ZREMRANGEBYSCORE', window_key, '-inf', cutoff)

        local count = redis.call('ZCARD', window_key)
        return math.max(0, max_count - count)
        "
    }

    /// Get the Lua script for querying time until available
    #[must_use]
    pub fn lua_time_until_script() -> &'static str {
        r"
        local window_key = KEYS[1]
        local now = tonumber(ARGV[1])
        local window_size = tonumber(ARGV[2])
        local max_count = tonumber(ARGV[3])

        local cutoff = now - window_size * 1000
        redis.call('ZREMRANGEBYSCORE', window_key, '-inf', cutoff)

        local count = redis.call('ZCARD', window_key)
        if count < max_count then
            return 0
        else
            local oldest = redis.call('ZRANGE', window_key, 0, 0, 'WITHSCORES')
            if #oldest >= 2 then
                local oldest_timestamp = tonumber(oldest[2])
                local expires = oldest_timestamp + window_size * 1000
                return math.max(0, expires - now)
            else
                return 0
            end
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss, clippy::cast_precision_loss)]
        "
    }

    /// Get the maximum number of executions allowed in the window
    #[must_use]
    #[inline]
    pub fn max_executions(&self) -> usize {
        (self.state.config.rate * self.state.config.window_size as f64).ceil() as usize
    }

    /// Get the state for implementing the distributed backend
    #[inline]
    #[must_use]
    pub fn state(&self) -> &DistributedRateLimiterState {
        &self.state
    }

    /// Try to acquire using local fallback
    #[must_use]
    pub fn try_acquire_fallback(&self) -> bool {
        self.state.try_acquire_fallback()
    }
}

/// Distributed rate limiter coordinator
///
/// Manages distributed rate limiters for multiple task types,
/// allowing cluster-wide rate limit enforcement.
///
/// # Features
///
/// - **Per-task rate limits**: Different rate limits for each task type
/// - **Cluster-wide coordination**: Rate limits enforced across all workers
/// - **Automatic fallback**: Gracefully degrade to local rate limiting on backend failure
/// - **Configuration management**: Dynamic rate limit updates
///
/// # Example
///
/// ```rust,ignore
/// use celers_core::rate_limit::{DistributedRateLimiterCoordinator, RateLimitConfig};
///
/// let coordinator = DistributedRateLimiterCoordinator::new("myapp");
///
/// // Set cluster-wide rate limit for a task
/// coordinator.set_task_rate(
///     "send_email",
///     RateLimitConfig::new(100.0).with_burst(200),
/// );
///
/// // Try to acquire across the cluster
/// if coordinator.try_acquire("send_email").await? {
///     send_email().await?;
/// }
/// ```
#[derive(Debug, Clone)]
pub struct DistributedRateLimiterCoordinator {
    /// Application namespace for Redis keys
    namespace: String,
    /// Per-task token bucket specs
    token_buckets: Arc<RwLock<HashMap<String, DistributedTokenBucketSpec>>>,
    /// Per-task sliding window specs
    sliding_windows: Arc<RwLock<HashMap<String, DistributedSlidingWindowSpec>>>,
    /// Default configuration for tasks without specific limits
    default_config: Option<RateLimitConfig>,
}

impl DistributedRateLimiterCoordinator {
    /// Create a new distributed rate limiter coordinator
    ///
    /// # Arguments
    ///
    /// * `namespace` - Application namespace for Redis keys (e.g., "myapp")
    pub fn new(namespace: impl Into<String>) -> Self {
        Self {
            namespace: namespace.into(),
            token_buckets: Arc::new(RwLock::new(HashMap::new())),
            sliding_windows: Arc::new(RwLock::new(HashMap::new())),
            default_config: None,
        }
    }

    /// Create with a default rate limit for all tasks
    pub fn with_default(namespace: impl Into<String>, config: RateLimitConfig) -> Self {
        Self {
            namespace: namespace.into(),
            token_buckets: Arc::new(RwLock::new(HashMap::new())),
            sliding_windows: Arc::new(RwLock::new(HashMap::new())),
            default_config: Some(config),
        }
    }

    /// Set distributed rate limit for a specific task type
    ///
    /// Creates a distributed rate limiter spec that can be used by
    /// backend implementations (e.g., Redis).
    pub fn set_task_rate(&self, task_name: impl Into<String>, config: RateLimitConfig) {
        let name = task_name.into();
        let key = format!("{}:ratelimit:{}", self.namespace, name);

        if config.sliding_window {
            if let Ok(mut guard) = self.sliding_windows.write() {
                guard.insert(name.clone(), DistributedSlidingWindowSpec::new(key, config));
            }
        } else if let Ok(mut guard) = self.token_buckets.write() {
            guard.insert(name.clone(), DistributedTokenBucketSpec::new(key, config));
        }
    }

    /// Remove rate limit for a specific task type
    pub fn remove_task_rate(&self, task_name: &str) {
        if let Ok(mut guard) = self.token_buckets.write() {
            guard.remove(task_name);
        }
        if let Ok(mut guard) = self.sliding_windows.write() {
            guard.remove(task_name);
        }
    }

    /// Get the token bucket spec for a task (if using token bucket)
    #[inline]
    #[must_use]
    pub fn get_token_bucket_spec(&self, task_name: &str) -> Option<DistributedTokenBucketSpec> {
        if let Ok(guard) = self.token_buckets.read() {
            guard.get(task_name).cloned()
        } else {
            None
        }
    }

    /// Get the sliding window spec for a task (if using sliding window)
    #[inline]
    #[must_use]
    pub fn get_sliding_window_spec(&self, task_name: &str) -> Option<DistributedSlidingWindowSpec> {
        if let Ok(guard) = self.sliding_windows.read() {
            guard.get(task_name).cloned()
        } else {
            None
        }
    }

    /// Check if a task has a distributed rate limit configured
    #[inline]
    #[must_use]
    pub fn has_rate_limit(&self, task_name: &str) -> bool {
        let has_bucket = if let Ok(guard) = self.token_buckets.read() {
            guard.contains_key(task_name)
        } else {
            false
        };

        let has_window = if let Ok(guard) = self.sliding_windows.read() {
            guard.contains_key(task_name)
        } else {
            false
        };

        has_bucket || has_window || self.default_config.is_some()
    }

    /// Try to acquire using local fallback for a task
    ///
    /// This method is useful when the distributed backend is unavailable.
    #[must_use]
    pub fn try_acquire_fallback(&self, task_name: &str) -> bool {
        // Try token bucket first
        if let Some(spec) = self.get_token_bucket_spec(task_name) {
            return spec.try_acquire_fallback();
        }

        // Try sliding window
        if let Some(spec) = self.get_sliding_window_spec(task_name) {
            return spec.try_acquire_fallback();
        }

        // Use default config if available
        if let Some(ref config) = self.default_config {
            let key = format!("{}:ratelimit:{}", self.namespace, task_name);
            let spec = DistributedTokenBucketSpec::new(key, config.clone());
            return spec.try_acquire_fallback();
        }

        // No rate limit configured
        true
    }

    /// Get the Redis key for a task's rate limiter
    #[must_use]
    pub fn redis_key(&self, task_name: &str) -> String {
        format!("{}:ratelimit:{}", self.namespace, task_name)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_token_bucket_basic() {
        let config = RateLimitConfig::new(10.0).with_burst(5);
        let mut limiter = TokenBucket::new(config);

        // Should be able to acquire up to burst capacity immediately
        for _ in 0..5 {
            assert!(limiter.try_acquire());
        }

        // Next acquisition should fail
        assert!(!limiter.try_acquire());
    }

    #[test]
    fn test_token_bucket_refill() {
        let config = RateLimitConfig::new(100.0).with_burst(10);
        let mut limiter = TokenBucket::new(config);

        // Exhaust all tokens
        for _ in 0..10 {
            assert!(limiter.try_acquire());
        }
        assert!(!limiter.try_acquire());

        // Wait for refill (10ms should give us ~1 token at 100/sec)
        thread::sleep(Duration::from_millis(15));

        // Should have at least 1 token now
        assert!(limiter.try_acquire());
    }

    #[test]
    fn test_sliding_window_basic() {
        let config = RateLimitConfig::new(5.0).with_sliding_window(1);
        let mut limiter = SlidingWindow::new(config);

        // Should be able to execute 5 tasks in 1 second window
        for _ in 0..5 {
            assert!(limiter.try_acquire());
        }

        // Next acquisition should fail
        assert!(!limiter.try_acquire());
    }

    #[test]
    fn test_task_rate_limiter() {
        let mut manager = TaskRateLimiter::new();

        // Set rate limit for task_a
        manager.set_task_rate("task_a", RateLimitConfig::new(10.0).with_burst(2));

        // task_a should be rate limited
        assert!(manager.try_acquire("task_a"));
        assert!(manager.try_acquire("task_a"));
        assert!(!manager.try_acquire("task_a"));

        // task_b has no rate limit, should always pass
        assert!(manager.try_acquire("task_b"));
        assert!(manager.try_acquire("task_b"));
        assert!(manager.try_acquire("task_b"));
    }

    #[test]
    fn test_task_rate_limiter_default() {
        let mut manager = TaskRateLimiter::with_default(RateLimitConfig::new(10.0).with_burst(2));

        // All tasks should use default rate limit
        assert!(manager.try_acquire("task_a"));
        assert!(manager.try_acquire("task_a"));
        assert!(!manager.try_acquire("task_a"));

        assert!(manager.try_acquire("task_b"));
        assert!(manager.try_acquire("task_b"));
        assert!(!manager.try_acquire("task_b"));
    }

    #[test]
    fn test_worker_rate_limiter_thread_safe() {
        let limiter = WorkerRateLimiter::new();
        // Use a very low rate (0.1 tokens/sec = 1 token per 10 seconds) to prevent
        // token regeneration during test execution, which would cause flaky results
        limiter.set_task_rate("task_a", RateLimitConfig::new(0.1).with_burst(10));

        let limiter_clone = limiter.clone();

        // Spawn multiple threads to test thread safety
        let handles: Vec<_> = (0..4)
            .map(|_| {
                let l = limiter_clone.clone();
                thread::spawn(move || {
                    let mut count = 0;
                    for _ in 0..5 {
                        if l.try_acquire("task_a") {
                            count += 1;
                        }
                    }
                    count
                })
            })
            .collect();

        let total: usize = handles.into_iter().map(|h| h.join().unwrap()).sum();

        // Total acquisitions should not exceed burst capacity
        assert!(total <= 10);
    }

    #[test]
    fn test_rate_limit_config_serialization() {
        let config = RateLimitConfig::new(50.0)
            .with_burst(100)
            .with_sliding_window(10);

        let json = serde_json::to_string(&config).unwrap();
        let parsed: RateLimitConfig = serde_json::from_str(&json).unwrap();

        assert!((parsed.rate - 50.0).abs() < f64::EPSILON);
        assert_eq!(parsed.burst, Some(100));
        assert!(parsed.sliding_window);
        assert_eq!(parsed.window_size, 10);
    }

    #[test]
    fn test_time_until_available() {
        let config = RateLimitConfig::new(10.0).with_burst(1);
        let mut limiter = TokenBucket::new(config);

        // Exhaust the token
        assert!(limiter.try_acquire());

        // Time until available should be around 100ms (1 token at 10/sec)
        let wait_time = limiter.time_until_available();
        assert!(wait_time > Duration::ZERO);
        assert!(wait_time <= Duration::from_millis(150));
    }

    #[test]
    fn test_reset() {
        let config = RateLimitConfig::new(10.0).with_burst(5);
        let mut limiter = TokenBucket::new(config);

        // Exhaust all tokens
        for _ in 0..5 {
            limiter.try_acquire();
        }
        assert!(!limiter.try_acquire());

        // Reset should restore tokens
        limiter.reset();
        assert!(limiter.try_acquire());
    }

    #[test]
    fn test_set_rate() {
        let config = RateLimitConfig::new(10.0).with_burst(10);
        let mut limiter = TokenBucket::new(config);

        // Update rate
        limiter.set_rate(100.0);
        assert!((limiter.config().rate - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_create_rate_limiter() {
        // Token bucket
        let config = RateLimitConfig::new(10.0);
        let mut limiter = create_rate_limiter(config);
        assert!(limiter.try_acquire());

        // Sliding window
        let config = RateLimitConfig::new(10.0).with_sliding_window(1);
        let mut limiter = create_rate_limiter(config);
        assert!(limiter.try_acquire());
    }
}