mocra-core 0.4.0

The mocra crawler framework runtime: errors, cache, utilities, domain models, downloader, data-plane queue, coordination, scheduler and engine.
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
#![allow(unused)]
use crate::errors::Result;
use crate::errors::error::RateLimitError;
use crate::utils::redis_lock::{AdvancedDistributedLock, DistributedLockManager};
use dashmap::DashMap;
use deadpool_redis::{
    Pool,
    redis::{AsyncCommands, cmd},
};
use log::error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;

/// Rate limiter configuration.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RateLimitConfig {
    /// Maximum requests per second.
    pub max_requests_per_second: f32,
    /// Window size in milliseconds (default: 1 second).
    pub window_size_millis: u64,
    /// Baseline max requests per second (used for restoration).
    #[serde(default)]
    pub base_max_requests_per_second: Option<f32>,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            max_requests_per_second: 2.0,
            window_size_millis: 1000,
            base_max_requests_per_second: None,
        }
    }
}

impl RateLimitConfig {
    pub fn new(max_requests_per_second: f32) -> Self {
        Self {
            max_requests_per_second,
            window_size_millis: 1000,
            base_max_requests_per_second: Some(max_requests_per_second),
        }
    }

    pub fn with_window_size(mut self, window_size: Duration) -> Self {
        self.window_size_millis = window_size.as_millis() as u64;
        self
    }
}

/// Redis-based distributed smooth rate limiter.
///
/// Uses a smooth throttling strategy to control request rate per identifier.
/// Features:
/// 1. Distributed limiting with shared state across multiple instances.
/// 2. Smooth pacing via computed interval per request.
/// 3. Uses `AdvancedDistributedLock` to protect Redis read/write operations.
/// 4. Supports per-key rate limits.
/// 5. Supports dynamic key-level config updates.
/// 6. Performs local calculations; Redis stores/synchronizes last-request time.
/// 7. Stores key configs in Redis for persistence and dynamic updates.
#[derive(Clone)]
pub struct DistributedSlidingWindowRateLimiter {
    /// Redis connection pool.
    pool: Option<Arc<Pool>>,
    /// Distributed lock manager.
    lock_manager: Arc<DistributedLockManager>,
    /// Redis key prefix.
    key_prefix: String,
    /// Sub prefix
    sub_prefix: String,
    /// Redis key for default config.
    default_config_key: String,
    /// Redis key prefix for per-key configs.
    config_key_prefix: String,
    /// Default rate-limit config.
    default_config: RateLimitConfig,
    /// Local cache of last request time (`identifier -> last_request_timestamp`).
    local_last_request: Arc<DashMap<String, u64>>,
    /// Local config cache (`identifier -> RateLimitConfig`).
    local_configs: Arc<DashMap<String, RateLimitConfig>>,
    /// Local default-config cache.
    local_default_config: Arc<RwLock<Option<RateLimitConfig>>>,
    /// Local suspension cache (`identifier -> suspend_until_timestamp`).
    local_suspended: Arc<DashMap<String, u64>>,
    /// Local wait cache to reduce Redis contention (identifier -> wait_until_timestamp)
    local_wait_until: Arc<DashMap<String, u64>>,
    /// Time offset between local clock and Redis clock (local - redis)
    /// Used to reduce Redis TIME commands
    time_offset: Arc<RwLock<Option<(i64, Instant)>>>,
    /// Cache for suspension checks to reduce Redis load
    /// Key: identifier, Value: (Last Check Time, Suspend Until Timestamp)
    suspend_cache: Arc<DashMap<String, (Instant, Option<u64>)>>,
    /// 可选协调后端(内嵌 Raft 等)。无 Redis 池时,用其成员数把全局限额**按节点分摊**
    /// (近似分布式限流);有 Redis 池时不分摊(Redis 已提供原子全局限流)。
    coordination: Option<Arc<dyn crate::utils::coordination::CoordinationBackend>>,
}

impl std::fmt::Debug for DistributedSlidingWindowRateLimiter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DistributedSlidingWindowRateLimiter")
            .field("has_redis", &self.pool.is_some())
            .field("has_coordination", &self.coordination.is_some())
            .field("key_prefix", &self.key_prefix)
            .field("default_config", &self.default_config)
            .finish()
    }
}

const TIME_OFFSET_REFRESH_SECS: u64 = 60;

impl DistributedSlidingWindowRateLimiter {
    /// Creates a new distributed rate limiter instance.
    ///
    /// # Parameters
    /// * `pool` - Redis connection pool.
    /// * `key_prefix` - Redis key prefix used to namespace limiter instances.
    /// * `default_config` - Default rate-limit config.
    pub fn new(
        limit_pool: Option<Arc<Pool>>,
        locker: Arc<DistributedLockManager>,
        namespace: &str,
        default_config: RateLimitConfig,
    ) -> Self {
        Self::new_with_coordination(limit_pool, locker, None, namespace, default_config)
    }

    /// 带协调后端构造:无 Redis 池的集群模式下,把全局限额按成员数分摊到每个节点。
    pub fn new_with_coordination(
        limit_pool: Option<Arc<Pool>>,
        locker: Arc<DistributedLockManager>,
        coordination: Option<Arc<dyn crate::utils::coordination::CoordinationBackend>>,
        namespace: &str,
        default_config: RateLimitConfig,
    ) -> Self {
        let sub_prefix = "rate_limiter".to_string();
        let key_prefix = format!("{namespace}:{sub_prefix}");
        let default_config_key = format!("{key_prefix}:default_config");
        let config_key_prefix = format!("{key_prefix}:config");

        Self {
            pool: limit_pool,
            lock_manager: locker,
            key_prefix,
            sub_prefix,
            default_config_key,
            config_key_prefix,
            default_config,
            local_last_request: Arc::new(DashMap::new()),
            local_configs: Arc::new(DashMap::new()),
            local_default_config: Arc::new(RwLock::new(None)),
            local_suspended: Arc::new(DashMap::new()),
            local_wait_until: Arc::new(DashMap::new()),
            time_offset: Arc::new(RwLock::new(None)),
            suspend_cache: Arc::new(DashMap::new()),
            coordination,
        }
    }

    /// 把每秒限额按集群成员数分摊(仅无 Redis 池且有协调后端时);
    /// 有 Redis 池 → 原子全局限流,不分摊。返回值恒 > 0,避免区间计算除零。
    fn share_rps(&self, rps: f32) -> f32 {
        if self.pool.is_some() {
            return rps;
        }
        let n = self
            .coordination
            .as_ref()
            .map(|c| c.cluster_size().max(1))
            .unwrap_or(1) as f32;
        (rps / n).max(f32::MIN_POSITIVE)
    }

    /// 解析某标识符的**生效**限速配置:在 [`get_key_config`](Self::get_key_config)
    /// 的全局配置上应用集群分摊。所有限流判定路径都应经此,而非直接读全局配置。
    async fn effective_config(&self, identifier: &str) -> RateLimitConfig {
        let mut cfg = match self.get_key_config(identifier).await {
            Ok(c) => c,
            Err(e) => {
                error!("{e:?}");
                self.default_config.clone()
            }
        };
        cfg.max_requests_per_second = self.share_rps(cfg.max_requests_per_second);
        cfg
    }

    /// Returns Redis key for stored last-request timestamp.
    fn get_last_request_key(&self, identifier: &str) -> String {
        format!("{}:last_request:{}", self.key_prefix, identifier)
    }

    /// Returns distributed-lock key.
    fn get_lock_key(&self, identifier: &str) -> String {
        format!("{}:lock:{}", self.sub_prefix, identifier)
    }

    /// Returns per-key config key.
    fn get_config_key(&self, identifier: &str) -> String {
        format!("{}:{}", self.config_key_prefix, identifier)
    }

    /// Returns suspension key.
    fn get_suspended_key(&self, identifier: &str) -> String {
        format!("{}:suspended:{}", self.key_prefix, identifier)
    }

    /// Suspends requests for specified identifier (circuit breaker).
    pub async fn suspend(&self, identifier: &str, duration: Duration) -> Result<()> {
        let current_time = self.get_current_timestamp().await?;
        let suspend_until = current_time + duration.as_millis() as u64;

        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let key = self.get_suspended_key(identifier);
            // Set with TTL
            let _: () = conn
                .set_ex(&key, suspend_until, duration.as_secs())
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
        } else {
            self.local_suspended
                .insert(identifier.to_string(), suspend_until);
        }
        // Invalidate cache
        self.suspend_cache.remove(identifier);
        Ok(())
    }

    /// Checks whether identifier is currently suspended.
    async fn check_suspended(&self, identifier: &str) -> Result<Option<u64>> {
        // Check short-lived cache first.
        // IMPORTANT: Extract values immediately and drop the DashMap guard before
        // any `.await` — holding a `parking_lot` read-lock across a yield point
        // can deadlock the Tokio runtime when a writer on the same shard blocks
        // an OS thread.
        let cached = self.suspend_cache.get(identifier).map(|entry| {
            let (checked_at, suspend_until_opt) = entry.value();
            (*checked_at, *suspend_until_opt)
        });
        // guard dropped here
        if let Some((checked_at, suspend_until_opt)) = cached {
            if checked_at.elapsed() < Duration::from_millis(500) {
                let current_time = self.get_current_timestamp().await?;
                if let Some(suspend_until) = suspend_until_opt {
                    if suspend_until > current_time {
                        return Ok(Some(suspend_until - current_time));
                    }
                } else {
                    return Ok(None);
                }
            }
        }

        let current_time = self.get_current_timestamp().await?;
        let mut found_suspend_until: Option<u64> = None;

        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let key = self.get_suspended_key(identifier);
            if let Some(suspend_until) = conn
                .get::<_, Option<u64>>(&key)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?
            {
                if suspend_until > current_time {
                    found_suspend_until = Some(suspend_until);
                }
            }
        } else if let Some(suspend_until) = self.local_suspended.get(identifier) {
            if *suspend_until > current_time {
                found_suspend_until = Some(*suspend_until);
            } else {
                // Lazy cleanup
                drop(suspend_until); // release lock
                self.local_suspended.remove(identifier);
            }
        }

        // Update cache
        self.suspend_cache.insert(
            identifier.to_string(),
            (Instant::now(), found_suspend_until),
        );

        if let Some(suspend_until) = found_suspend_until {
            Ok(Some(suspend_until - current_time))
        } else {
            Ok(None)
        }
    }

    /// Returns current timestamp in milliseconds.
    ///
    /// Prefers Redis time when available to reduce clock skew across instances.
    async fn get_current_timestamp(&self) -> Result<u64> {
        if let Some(pool) = &self.pool {
            // Try to use cached offset
            if let Some((offset, last_refresh)) = *self.time_offset.read().await {
                if last_refresh.elapsed() < Duration::from_secs(TIME_OFFSET_REFRESH_SECS) {
                    let local_now = SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .unwrap()
                        .as_millis() as i64;
                    // redis_time = local_time - offset
                    let redis_time = local_now - offset;
                    return Ok(redis_time as u64);
                }
            }

            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            // TIME command returns (seconds, microseconds)
            let time: (u64, u64) = deadpool_redis::redis::cmd("TIME")
                .query_async(&mut conn)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            let redis_millis = time.0 * 1000 + time.1 / 1000;
            let local_now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_millis() as i64;

            // Calculate and cache offset: offset = local - redis
            let offset = local_now - redis_millis as i64;
            *self.time_offset.write().await = Some((offset, Instant::now()));

            Ok(redis_millis)
        } else {
            Ok(SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_millis() as u64)
        }
    }

    /// Sets rate-limit config for a specific key.
    ///
    /// # Parameters
    /// * `key` - Target key.
    /// * `config` - Rate-limit configuration.
    pub async fn set_key_config(&self, key: &str, config: RateLimitConfig) -> Result<()> {
        // Check cache first to avoid redundant Redis writes
        if let Some(existing) = self.local_configs.get(key) {
            if *existing == config {
                return Ok(());
            }
        }

        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError("redis connect error".into()))?;
            let config_key = self.get_config_key(key);
            let config_json = serde_json::to_string(&config)?;
            let _: () = conn
                .set(&config_key, &config_json)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            // Update cache
            self.local_configs.insert(key.to_string(), config);
        } else {
            self.local_configs.insert(key.to_string(), config);
        }
        Ok(())
    }

    pub async fn set_limit(&self, id: &str, limit: f32) -> Result<()> {
        let config = RateLimitConfig::new(limit);
        self.set_key_config(id, config).await
    }
    /// Sets global limit configuration (affects all keys).
    pub async fn set_all_limit(&self, limit: f32) -> Result<()> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            // Update default config.
            let mut default_config = self.default_config.clone();
            default_config.max_requests_per_second = limit;
            let config_json = serde_json::to_string(&default_config)?;
            let _: () = conn
                .set(&self.default_config_key, &config_json)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            // Update all configured keys.
            let pattern = format!("{}:*", self.config_key_prefix);
            let keys: Vec<String> = conn
                .keys(&pattern)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            for key in keys {
                let config_json: String = conn.get(&key).await.unwrap_or_default();
                if !config_json.is_empty()
                    && let Ok(mut config) = serde_json::from_str::<RateLimitConfig>(&config_json)
                {
                    config.max_requests_per_second = limit;
                    let updated_json = serde_json::to_string(&config)?;
                    let _: () = conn
                        .set(&key, &updated_json)
                        .await
                        .map_err(|e| RateLimitError::RedisError(e.into()))?;
                }
            }
        } else {
            // Update local default config.
            let mut default_config = self.default_config.clone();
            default_config.max_requests_per_second = limit;
            *self.local_default_config.write().await = Some(default_config);

            // Update all local configs.
            for mut entry in self.local_configs.iter_mut() {
                entry.value_mut().max_requests_per_second = limit;
            }
        }

        Ok(())
    }

    /// Sets rate-limit configs for multiple keys in batch.
    ///
    /// # Parameters
    /// * `configs` - Mapping from key to config.
    pub async fn set_key_configs(&self, configs: HashMap<String, RateLimitConfig>) -> Result<()> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let mut pipe = deadpool_redis::redis::pipe();

            let mut updates = Vec::new();

            for (key, config) in configs {
                // Check cache
                if let Some(existing) = self.local_configs.get(&key) {
                    if *existing == config {
                        continue;
                    }
                }

                let config_key = self.get_config_key(&key);
                let config_json = serde_json::to_string(&config)?;
                pipe.set(&config_key, &config_json);
                updates.push((key, config));
            }

            if !updates.is_empty() {
                let _: () = pipe
                    .query_async(&mut *conn)
                    .await
                    .map_err(|e| RateLimitError::RedisError(e.into()))?;

                // Update cache
                for (key, config) in updates {
                    self.local_configs.insert(key, config);
                }
            }
        } else {
            for (key, config) in configs {
                self.local_configs.insert(key, config);
            }
        }
        Ok(())
    }

    /// Removes a key-specific config (falls back to default config).
    ///
    /// # Parameters
    /// * `key` - Key whose config should be removed.
    pub async fn remove_key_config(&self, key: &str) -> Result<()> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let config_key = self.get_config_key(key);
            let _: usize = conn
                .del(&config_key)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
        } else {
            self.local_configs.remove(key);
        }
        Ok(())
    }

    /// Gets config for a specific key.
    ///
    /// # Parameters
    /// * `key` - Key to query.
    ///
    /// # Returns
    /// Key-specific config, or default config when not set.
    pub async fn get_key_config(&self, key: &str) -> Result<RateLimitConfig> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let config_key = self.get_config_key(key);

            // Try key-specific config first.
            let config_json: Option<String> = conn
                .get(&config_key)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            if let Some(json) = config_json
                && let Ok(config) = serde_json::from_str::<RateLimitConfig>(&json)
            {
                return Ok(config);
            }

            // Then try default config.
            let default_json: Option<String> = conn
                .get(&self.default_config_key)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            if let Some(json) = default_json
                && let Ok(config) = serde_json::from_str::<RateLimitConfig>(&json)
            {
                return Ok(config);
            }
        } else {
            // Local mode.
            if let Some(config) = self.local_configs.get(key) {
                return Ok(config.clone());
            }
            if let Some(config) = self.local_default_config.read().await.as_ref() {
                return Ok(config.clone());
            }
        }

        // Fallback to hard-coded default config.
        Ok(self.default_config.clone())
    }

    /// Returns all configured keys and their configs.
    pub async fn get_all_key_configs(&self) -> Result<HashMap<String, RateLimitConfig>> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let pattern = format!("{}:*", self.config_key_prefix);
            let keys: Vec<String> = conn
                .keys(&pattern)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            let mut configs = HashMap::new();
            for key in keys {
                let config_json: Option<String> = conn
                    .get(&key)
                    .await
                    .map_err(|e| RateLimitError::RedisError(e.into()))?;
                if let Some(json) = config_json
                    && let Ok(config) = serde_json::from_str::<RateLimitConfig>(&json)
                {
                    // Extract actual identifier (strip prefix).
                    let identifier = key
                        .strip_prefix(&format!("{}:", self.config_key_prefix))
                        .unwrap_or(&key);
                    configs.insert(identifier.to_string(), config);
                }
            }
            Ok(configs)
        } else {
            let mut configs = HashMap::new();
            for entry in self.local_configs.iter() {
                configs.insert(entry.key().clone(), entry.value().clone());
            }
            Ok(configs)
        }
    }

    /// Records one request.
    ///
    /// # Parameters
    /// * `identifier` - Identifier used to distinguish request sources.
    pub async fn record(&self, identifier: &str) -> Result<()> {
        let lock_key = self.get_lock_key(identifier);
        let last_request_key = self.get_last_request_key(identifier);

        // Acquire distributed lock to protect Redis operations.
        if let Ok(acquired) = self
            .lock_manager
            .acquire_lock(&lock_key, 30, Duration::from_secs(5))
            .await
        {
            if !acquired {
                return Err(RateLimitError::RedisError("Failed to acquire lock".into()).into());
            }

            let current_time = self.get_current_timestamp().await?;

            if let Some(pool) = &self.pool {
                let mut conn = pool
                    .get()
                    .await
                    .map_err(|e| RateLimitError::RedisError("redis connect error".into()))?;

                // Set TTL based on request interval so record remains available for next check.
                let config = self.effective_config(identifier).await;
                let min_interval_millis = (config.window_size_millis as f64
                    / config.max_requests_per_second as f64)
                    as u64;
                let ttl_millis = min_interval_millis * 2;
                let ttl_seconds = (ttl_millis / 1000).max(1);
                // Update last request time.
                let _: () = conn
                    .set_ex(&last_request_key, current_time, ttl_seconds)
                    .await
                    .map_err(|e| RateLimitError::RedisError(e.into()))?;
            } else {
                // Local mode.
                self.local_last_request
                    .insert(last_request_key, current_time);
            }

            // Release lock.
            let _ = self.lock_manager.release_lock(&lock_key).await;
        } else {
            return Err(RateLimitError::RedisError("Failed to acquire lock".into()).into());
        }

        Ok(())
    }

    /// Tries to acquire permit (atomic operation).
    pub async fn acquire(&self, identifier: &str, _permits: f64) -> Result<()> {
        let wait_ms = self.check_and_update(identifier).await?;
        if wait_ms > 0 {
            let wait_secs = wait_ms.div_ceil(1000);
            return Err(RateLimitError::WaitTime(wait_secs).into());
        }
        Ok(())
    }

    /// Verifies whether max limit has been reached.
    ///
    /// # Parameters
    /// * `identifier` - Identifier used to distinguish request sources.
    ///
    /// # Returns
    /// * `Ok(())` - Limit not reached.
    /// * `Err(u64)` - Limit reached; wait time in milliseconds.
    pub async fn verify(&self, identifier: &str) -> Result<Option<u64>> {
        // Check suspension first
        if let Some(wait) = self.check_suspended(identifier).await? {
            return Ok(Some(wait));
        }

        let config = self.effective_config(identifier).await;

        let last_request_key = self.get_last_request_key(identifier);
        let min_interval_millis =
            (config.window_size_millis as f64 / config.max_requests_per_second as f64) as u64;

        let current_time = self.get_current_timestamp().await?;

        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let script = r#"
            local key = KEYS[1]
            local current_time = tonumber(ARGV[1])
            local min_interval = tonumber(ARGV[2])

            local last_time = redis.call("GET", key)
            if last_time then
                local diff = current_time - tonumber(last_time)
                if diff < min_interval then
                    return min_interval - diff
                end
            end
            return 0
            "#;

            let wait_ms: u64 = deadpool_redis::redis::Script::new(script)
                .key(&last_request_key)
                .arg(current_time)
                .arg(min_interval_millis)
                .invoke_async(&mut conn)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            if wait_ms > 0 {
                Ok(Some(wait_ms))
            } else {
                Ok(None)
            }
        } else {
            // Local mode.
            if let Some(last_time) = self.local_last_request.get(&last_request_key).map(|v| *v) {
                let elapsed = current_time.saturating_sub(last_time);
                if elapsed < min_interval_millis {
                    return Ok(Some(min_interval_millis - elapsed));
                }
            }
            Ok(None)
        }
    }

    /// Atomically checks and updates rate-limit state (competitive model).
    ///
    /// Only ONE caller wins the permit per interval; others receive a wait
    /// duration and must retry.  Prefer [`wait_for_permit`](Self::wait_for_permit) for download
    /// tasks — it uses a reservation model that eliminates retry loops.
    ///
    /// # Returns
    /// * `Ok(0)` - Permit acquired.
    /// * `Ok(wait_ms)` - Must wait `wait_ms` milliseconds then retry.
    pub async fn check_and_update(&self, identifier: &str) -> Result<u64> {
        // Check suspension first
        if let Some(wait) = self.check_suspended(identifier).await? {
            return Ok(wait);
        }

        let current_time = self.get_current_timestamp().await?;

        // Check local wait cache to reduce Redis pressure
        if let Some(entry) = self.local_wait_until.get(identifier) {
            let wait_until = *entry;
            if wait_until > current_time {
                return Ok(wait_until - current_time);
            }
            drop(entry);
            self.local_wait_until.remove(identifier);
        }

        let config = self.effective_config(identifier).await;

        let last_request_key = self.get_last_request_key(identifier);
        let min_interval_millis =
            (config.window_size_millis as f64 / config.max_requests_per_second as f64) as u64;
        let ttl_millis = min_interval_millis * 2;
        let ttl_seconds = (ttl_millis / 1000).max(1);

        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            let script = r#"
            local key = KEYS[1]
            local current_time = tonumber(ARGV[1])
            local min_interval = tonumber(ARGV[2])
            local ttl = tonumber(ARGV[3])

            local last_time = redis.call("GET", key)
            if last_time then
                local diff = current_time - tonumber(last_time)
                if diff < min_interval then
                    return min_interval - diff
                end
            end

            redis.call("SETEX", key, ttl, current_time)
            return 0
            "#;

            let result: u64 = deadpool_redis::redis::Script::new(script)
                .key(&last_request_key)
                .arg(current_time)
                .arg(min_interval_millis)
                .arg(ttl_seconds)
                .invoke_async(&mut conn)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            if result > 0 {
                // Update local wait cache
                self.local_wait_until
                    .insert(identifier.to_string(), current_time + result);
            }

            Ok(result)
        } else {
            // Simple local-mode implementation (locking via DashMap entry).
            let entry = self.local_last_request.entry(last_request_key);
            match entry {
                dashmap::mapref::entry::Entry::Occupied(mut occ) => {
                    let last_time = *occ.get();
                    let elapsed = current_time.saturating_sub(last_time);
                    if elapsed < min_interval_millis {
                        return Ok(min_interval_millis - elapsed);
                    }
                    occ.insert(current_time);
                    Ok(0)
                }
                dashmap::mapref::entry::Entry::Vacant(vac) => {
                    vac.insert(current_time);
                    Ok(0)
                }
            }
        }
    }

    /// Reserves a unique rate-limit time slot for the given identifier.
    ///
    /// Unlike [`check_and_update`] (competitive — one winner per call),
    /// this method assigns each caller its own future slot atomically.
    /// With N concurrent callers the slots are T, T+interval, T+2·interval, …
    /// so every caller makes exactly **one** Redis round-trip.
    ///
    /// # Returns
    /// * `Ok(0)` — Permit acquired immediately.
    /// * `Ok(wait_ms)` — Slot reserved; caller should sleep then proceed.
    async fn reserve_permit(&self, identifier: &str) -> Result<u64> {
        let current_time = self.get_current_timestamp().await?;

        let config = self.effective_config(identifier).await;

        let last_request_key = self.get_last_request_key(identifier);
        let min_interval_millis =
            (config.window_size_millis as f64 / config.max_requests_per_second as f64) as u64;
        let base_ttl_seconds = (min_interval_millis * 2 / 1000).max(1);

        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            // Reservation-based Lua script.
            // Each caller atomically reads the last reserved time, computes the
            // next available slot, writes it back, and returns the wait duration.
            // Concurrent callers are serialised inside Redis so every caller
            // receives a **distinct** future slot — no thundering-herd.
            let script = r#"
            local key          = KEYS[1]
            local current_time = tonumber(ARGV[1])
            local min_interval = tonumber(ARGV[2])
            local base_ttl     = tonumber(ARGV[3])

            local last_time = redis.call("GET", key)
            local next_available
            if last_time then
                next_available = math.max(tonumber(last_time) + min_interval, current_time)
            else
                next_available = current_time
            end

            -- TTL must survive until the reserved slot expires
            local wait = next_available - current_time
            local ttl  = math.max(math.ceil((wait + min_interval * 2) / 1000), base_ttl)
            redis.call("SETEX", key, ttl, next_available)
            return wait
            "#;

            let wait_ms: u64 = deadpool_redis::redis::Script::new(script)
                .key(&last_request_key)
                .arg(current_time)
                .arg(min_interval_millis)
                .arg(base_ttl_seconds)
                .invoke_async(&mut conn)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            Ok(wait_ms)
        } else {
            // Local mode: DashMap entry lock gives atomicity on one process.
            let entry = self.local_last_request.entry(last_request_key);
            match entry {
                dashmap::mapref::entry::Entry::Occupied(mut occ) => {
                    let last_time = *occ.get();
                    let next_available = (last_time + min_interval_millis).max(current_time);
                    let wait = next_available.saturating_sub(current_time);
                    occ.insert(next_available);
                    Ok(wait)
                }
                dashmap::mapref::entry::Entry::Vacant(vac) => {
                    vac.insert(current_time);
                    Ok(0)
                }
            }
        }
    }

    /// Waits until a rate-limit permit is available, then returns.
    ///
    /// Recommended entry point for download tasks.  Handles:
    /// 1. **Suspension** (circuit breaker / 429 backoff) — waits until cleared.
    /// 2. **Slot reservation** — each caller gets a unique future time slot
    ///    via a single atomic Redis call, then sleeps exactly the right amount.
    ///
    /// No polling loop, no thundering-herd, no request abandonment.
    pub async fn wait_for_permit(&self, identifier: &str) -> Result<()> {
        // Phase 1: wait for any active suspension to clear.
        while let Some(remaining_ms) = self.check_suspended(identifier).await? {
            tokio::time::sleep(Duration::from_millis(remaining_ms)).await;
        }

        // Phase 2: reserve a slot in the rate-limit queue.
        let wait_ms = self.reserve_permit(identifier).await?;
        if wait_ms > 0 {
            tokio::time::sleep(Duration::from_millis(wait_ms)).await;
        }
        Ok(())
    }

    /// Returns time until next request can proceed (milliseconds).
    ///
    /// # Parameters
    /// * `identifier` - Identifier.
    ///
    /// # Returns
    /// Milliseconds to wait; `0` means request is immediately allowed.
    pub async fn get_next_available_time(&self, identifier: &str) -> Result<u64> {
        self.verify(identifier).await.map(|res| res.unwrap_or(0))
    }

    /// Decreases rate-limit throughput (backpressure).
    ///
    /// # Parameters
    /// * `identifier` - Identifier.
    /// * `factor` - Reduction factor (`0.0 - 1.0`), e.g. `0.5` halves throughput.
    pub async fn decrease_limit(&self, identifier: &str, factor: f32) -> Result<f32> {
        let mut config = self.get_key_config(identifier).await?;

        // Ensure `base_max_requests_per_second` is initialized.
        if config.base_max_requests_per_second.is_none() {
            config.base_max_requests_per_second = Some(config.max_requests_per_second);
        }

        // Compute new limit.
        let new_limit = config.max_requests_per_second * factor;
        // Apply minimum floor to avoid zero.
        config.max_requests_per_second = new_limit.max(0.1);

        self.set_key_config(identifier, config.clone()).await?;
        Ok(config.max_requests_per_second)
    }

    /// Tries to restore rate-limit throughput.
    ///
    /// # Parameters
    /// * `identifier` - Identifier.
    /// * `step_factor` - Restore step factor (`> 1.0`), e.g. `1.1` increases by 10%.
    pub async fn try_restore_limit(&self, identifier: &str, step_factor: f32) -> Result<f32> {
        let mut config = self.get_key_config(identifier).await?;

        if let Some(base) = config.base_max_requests_per_second
            && config.max_requests_per_second < base
        {
            let new_limit = config.max_requests_per_second * step_factor;
            config.max_requests_per_second = new_limit.min(base);
            self.set_key_config(identifier, config.clone()).await?;
        }

        Ok(config.max_requests_per_second)
    }

    /// Cleans all expired records.
    ///
    /// Can be called periodically to reclaim Redis memory.
    pub async fn cleanup(&self) -> Result<u64> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let pattern = format!("{}:last_request:*", self.key_prefix);
            let keys: Vec<String> = conn
                .keys(&pattern)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            let mut total_cleaned = 0u64;
            let current_time = self.get_current_timestamp().await?;

            for key in keys {
                // Check whether key is expired (`TTL <= 0`) or value is stale.
                let ttl: i64 = conn
                    .ttl(&key)
                    .await
                    .map_err(|e| RateLimitError::RedisError(e.into()))?;

                let should_delete = if ttl == -1 {
                    // No TTL set; determine staleness from stored value.
                    if let Ok(Some(last_time)) = conn.get::<_, Option<u64>>(&key).await {
                        // Use default config to compute max valid age.
                        let min_interval_millis = (self.default_config.window_size_millis as f64
                            / self.default_config.max_requests_per_second as f64)
                            as u64;
                        let max_valid_age = min_interval_millis * 2; // Keep consistent with TTL policy.
                        current_time.saturating_sub(last_time) > max_valid_age
                    } else {
                        true // Could not fetch value; delete key.
                    }
                } else {
                    ttl <= 0 // TTL expired.
                };

                if should_delete {
                    let deleted: usize = conn
                        .del(&key)
                        .await
                        .map_err(|e| RateLimitError::RedisError(e.into()))?;
                    if deleted > 0 {
                        total_cleaned += 1;
                    }
                }
            }

            Ok(total_cleaned)
        } else {
            // Local-mode cleanup.
            let mut total_cleaned = 0u64;
            let current_time = self.get_current_timestamp().await?;

            // Collect keys to delete to avoid mutating while iterating.
            let mut keys_to_remove = Vec::new();

            for entry in self.local_last_request.iter() {
                let last_time = *entry.value();

                // Try using corresponding config to compute expiry.
                // Simplified: fall back to default config when key-specific config is unavailable.
                // In practice identifier should be parsed from key; here key is already last_request_key.
                // Using default config is a conservative estimate.

                let min_interval_millis = (self.default_config.window_size_millis as f64
                    / self.default_config.max_requests_per_second as f64)
                    as u64;
                let max_valid_age = min_interval_millis * 2;

                if current_time.saturating_sub(last_time) > max_valid_age {
                    keys_to_remove.push(entry.key().clone());
                }
            }

            for key in keys_to_remove {
                if self.local_last_request.remove(&key).is_some() {
                    total_cleaned += 1;
                }
            }

            Ok(total_cleaned)
        }
    }

    /// Returns number of identifiers currently stored.
    pub async fn get_identifier_count(&self) -> Result<usize> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let pattern = format!("{}:last_request:*", self.key_prefix);
            let keys: Vec<String> = conn
                .keys(&pattern)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            Ok(keys.len())
        } else {
            Ok(self.local_last_request.len())
        }
    }

    /// Resets records for a specific identifier.
    pub async fn reset(&self, identifier: &str) -> Result<()> {
        let key = self.get_last_request_key(identifier);
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let _: usize = conn
                .del(&key)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
        } else {
            self.local_last_request.remove(&key);
        }
        Ok(())
    }

    /// Resets all records.
    pub async fn reset_all(&self) -> Result<()> {
        if let Some(pool) = &self.pool {
            let mut conn = pool
                .get()
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;
            let pattern = format!("{}:last_request:*", self.key_prefix);
            let keys: Vec<String> = conn
                .keys(&pattern)
                .await
                .map_err(|e| RateLimitError::RedisError(e.into()))?;

            if !keys.is_empty() {
                let _: usize = conn
                    .del(&keys)
                    .await
                    .map_err(|e| RateLimitError::RedisError(e.into()))?;
            }
        } else {
            self.local_last_request.clear();
        }

        Ok(())
    }

    /// Returns key prefix (for debugging).
    pub fn get_key_prefix(&self) -> &str {
        &self.key_prefix
    }
}

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

    /// 只覆写 `cluster_size` 的最小 CoordinationBackend,用于验证限额分摊。
    struct SizeBackend(usize);
    #[async_trait::async_trait]
    impl crate::utils::coordination::CoordinationBackend for SizeBackend {
        async fn publish(&self, _: &str, _: &[u8]) -> std::result::Result<(), String> {
            Ok(())
        }
        async fn subscribe(
            &self,
            _: &str,
        ) -> std::result::Result<tokio::sync::mpsc::Receiver<Vec<u8>>, String> {
            let (_tx, rx) = tokio::sync::mpsc::channel(1);
            Ok(rx)
        }
        async fn set(&self, _: &str, _: &[u8]) -> std::result::Result<(), String> {
            Ok(())
        }
        async fn get(&self, _: &str) -> std::result::Result<Option<Vec<u8>>, String> {
            Ok(None)
        }
        async fn cas(
            &self,
            _: &str,
            _: Option<&[u8]>,
            _: &[u8],
        ) -> std::result::Result<bool, String> {
            Ok(true)
        }
        async fn acquire_lock(
            &self,
            _: &str,
            _: &[u8],
            _: u64,
        ) -> std::result::Result<bool, String> {
            Ok(true)
        }
        async fn renew_lock(&self, _: &str, _: &[u8], _: u64) -> std::result::Result<bool, String> {
            Ok(true)
        }
        fn cluster_size(&self) -> usize {
            self.0
        }
    }

    #[test]
    fn shares_global_limit_by_cluster_size() {
        // 4 节点集群、无 Redis:每秒 8 的全局限额按节点分摊为每节点 2。
        let locker = Arc::new(DistributedLockManager::new(None, "t"));
        let backend: Arc<dyn crate::utils::coordination::CoordinationBackend> =
            Arc::new(SizeBackend(4));
        let limiter = DistributedSlidingWindowRateLimiter::new_with_coordination(
            None,
            locker,
            Some(backend),
            "ns",
            RateLimitConfig::new(8.0),
        );
        assert_eq!(limiter.share_rps(8.0), 2.0);

        // 无协调后端 → 不分摊(单机语义不变)。
        let plain = DistributedSlidingWindowRateLimiter::new(
            None,
            Arc::new(DistributedLockManager::new(None, "t")),
            "ns",
            RateLimitConfig::new(8.0),
        );
        assert_eq!(plain.share_rps(8.0), 8.0);
    }

    #[tokio::test]
    async fn test_rate_limiter_local() {
        let lock_manager = Arc::new(DistributedLockManager::new(None, "test"));
        let config = RateLimitConfig::new(1.0); // 1 request per second
        let limiter = DistributedSlidingWindowRateLimiter::new(None, lock_manager, "test", config);

        // First request should succeed
        assert!(limiter.verify("user1").await.unwrap().is_none());
        limiter.record("user1").await.unwrap();

        // Second request immediately after should be delayed
        // interval is 1000ms
        let res = limiter.verify("user1").await.unwrap();
        assert!(res.is_some());
    }

    #[tokio::test]
    async fn test_cleanup() {
        let lock_manager = Arc::new(DistributedLockManager::new(None, "test_cleanup"));
        let config = RateLimitConfig::new(10.0); // 10 req/s => 100ms interval => 200ms ttl
        let limiter = DistributedSlidingWindowRateLimiter::new(None, lock_manager, "test", config);

        limiter.record("user_cleanup").await.unwrap();
        // Check count (using internal method exposed via get_identifier_count)
        // Note: record inserts a key.
        assert!(limiter.get_identifier_count().await.unwrap() > 0);

        // Wait for expiration (TTL is roughly 200ms)
        tokio::time::sleep(Duration::from_millis(300)).await;

        let cleaned = limiter.cleanup().await.unwrap();
        assert!(cleaned > 0);
        assert_eq!(limiter.get_identifier_count().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_adaptive_rate_limit() {
        let lock_manager = Arc::new(DistributedLockManager::new(None, "test_adaptive"));
        let config = RateLimitConfig::new(10.0);
        let limiter = DistributedSlidingWindowRateLimiter::new(None, lock_manager, "test", config);

        limiter.decrease_limit("adaptive", 0.5).await.unwrap();
        let limit = limiter
            .get_key_config("adaptive")
            .await
            .unwrap()
            .max_requests_per_second;
        assert_eq!(limit, 5.0);

        limiter.try_restore_limit("adaptive", 1.5).await.unwrap();
        let limit = limiter
            .get_key_config("adaptive")
            .await
            .unwrap()
            .max_requests_per_second;
        assert_eq!(limit, 7.5);
    }

    #[tokio::test]
    async fn test_suspend() {
        let lock_manager = Arc::new(DistributedLockManager::new(None, "test_suspend"));
        let config = RateLimitConfig::new(10.0);
        let limiter = DistributedSlidingWindowRateLimiter::new(None, lock_manager, "test", config);

        limiter
            .suspend("user_suspend", Duration::from_millis(200))
            .await
            .unwrap();

        let res = limiter.verify("user_suspend").await.unwrap();
        assert!(res.is_some());

        tokio::time::sleep(Duration::from_millis(300)).await;

        // After wait, verify should pass (if no record exists, verify returns None)
        let res = limiter.verify("user_suspend").await.unwrap();
        assert!(res.is_none());
    }
}