arete-server 0.20.0

WebSocket server and projection handlers for Arete streaming pipelines
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
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, warn};

/// Rate limit window configuration
#[derive(Debug, Clone, Copy)]
pub struct RateLimitWindow {
    /// Maximum number of requests allowed in the window
    pub max_requests: u32,
    /// Window duration
    pub window_duration: Duration,
    /// Burst allowance (extra requests allowed temporarily)
    pub burst: u32,
}

impl RateLimitWindow {
    /// Create a new rate limit window
    pub fn new(max_requests: u32, window_duration: Duration) -> Self {
        Self {
            max_requests,
            window_duration,
            burst: 0,
        }
    }

    /// Add burst allowance
    pub fn with_burst(mut self, burst: u32) -> Self {
        self.burst = burst;
        self
    }
}

impl Default for RateLimitWindow {
    fn default() -> Self {
        Self {
            max_requests: 100,
            window_duration: Duration::from_secs(60),
            burst: 10,
        }
    }
}

/// Rate limit result
#[derive(Debug, Clone)]
pub enum RateLimitResult {
    /// Request is allowed
    Allowed { remaining: u32, reset_at: Instant },
    /// Request is denied due to rate limiting
    Denied { retry_after: Duration, limit: u32 },
}

/// A single rate limit bucket using sliding window algorithm
#[derive(Debug)]
struct RateLimitBucket {
    /// Request timestamps in the current window
    requests: Vec<Instant>,
    /// Window configuration
    window: RateLimitWindow,
}

impl RateLimitBucket {
    fn new(window: RateLimitWindow) -> Self {
        Self {
            requests: Vec::with_capacity((window.max_requests + window.burst) as usize),
            window,
        }
    }

    fn prune_expired(&mut self, now: Instant) {
        let cutoff = now - self.window.window_duration;
        self.requests.retain(|&t| t > cutoff);
    }

    /// Check if a request is allowed and record it.
    ///
    /// `limit_override` replaces the configured max+burst with a signed
    /// per-token limit (no additional burst) when present.
    fn check_and_record(&mut self, now: Instant, limit_override: Option<u32>) -> RateLimitResult {
        self.prune_expired(now);

        let limit = limit_override.unwrap_or(self.window.max_requests + self.window.burst);
        let current_count = self.requests.len() as u32;

        if current_count >= limit {
            let reported_limit = limit_override.unwrap_or(self.window.max_requests);
            // Calculate retry after time
            if let Some(oldest) = self.requests.first() {
                let retry_after =
                    (*oldest + self.window.window_duration).saturating_duration_since(now);
                RateLimitResult::Denied {
                    retry_after,
                    limit: reported_limit,
                }
            } else {
                RateLimitResult::Denied {
                    retry_after: self.window.window_duration,
                    limit: reported_limit,
                }
            }
        } else {
            self.requests.push(now);
            let reset_at = now + self.window.window_duration;
            RateLimitResult::Allowed {
                remaining: limit - current_count - 1,
                reset_at,
            }
        }
    }
}

/// Rate limiter configuration per key type
#[derive(Debug, Clone)]
pub struct RateLimiterConfig {
    /// Rate limit for handshake attempts per IP
    pub handshake_per_ip: RateLimitWindow,
    /// Rate limit for connection attempts per resolved consumer
    /// (falls back to the token subject for legacy tokens)
    pub connections_per_consumer: RateLimitWindow,
    /// Rate limit for connection attempts per resolved account
    /// (falls back to the metering key for legacy tokens)
    pub connections_per_account: RateLimitWindow,
    /// Rate limit for subscription requests per connection
    pub subscriptions_per_connection: RateLimitWindow,
    /// Rate limit for messages per connection
    pub messages_per_connection: RateLimitWindow,
    /// Rate limit for snapshot requests per connection
    pub snapshots_per_connection: RateLimitWindow,
    /// Enable rate limiting (can be disabled for testing)
    pub enabled: bool,
}

impl Default for RateLimiterConfig {
    fn default() -> Self {
        Self {
            handshake_per_ip: RateLimitWindow::new(60, Duration::from_secs(60)).with_burst(10),
            connections_per_consumer: RateLimitWindow::new(30, Duration::from_secs(60))
                .with_burst(5),
            connections_per_account: RateLimitWindow::new(100, Duration::from_secs(60))
                .with_burst(20),
            subscriptions_per_connection: RateLimitWindow::new(120, Duration::from_secs(60))
                .with_burst(10),
            messages_per_connection: RateLimitWindow::new(1000, Duration::from_secs(60))
                .with_burst(100),
            snapshots_per_connection: RateLimitWindow::new(30, Duration::from_secs(60))
                .with_burst(5),
            enabled: true,
        }
    }
}

impl RateLimiterConfig {
    /// Load a rate limit window from `{prefix}_MAX` and `{prefix}_WINDOW_SECS`.
    fn window_from_env(prefix: &str) -> Option<RateLimitWindow> {
        let max = std::env::var(format!("{prefix}_MAX")).ok()?.parse().ok()?;
        let secs = std::env::var(format!("{prefix}_WINDOW_SECS"))
            .ok()?
            .parse()
            .ok()?;
        Some(RateLimitWindow::new(max, Duration::from_secs(secs)))
    }

    /// Load a window from its current env prefix, falling back to a
    /// deprecated alias with a startup warning.
    fn window_from_env_with_alias(prefix: &str, deprecated: &str) -> Option<RateLimitWindow> {
        if let Some(window) = Self::window_from_env(prefix) {
            return Some(window);
        }
        let window = Self::window_from_env(deprecated)?;
        tracing::warn!(
            deprecated_prefix = deprecated,
            replacement_prefix = prefix,
            "deprecated rate-limit environment variables are set; \
             rename them before the alias is removed"
        );
        Some(window)
    }

    /// Load configuration from environment variables
    pub fn from_env() -> Self {
        let mut config = Self::default();

        // Handshake rate limit
        if let Some(window) = Self::window_from_env("ARETE_RATE_LIMIT_HANDSHAKE_PER_IP") {
            config.handshake_per_ip = window;
        }

        // Connection attempts per resolved consumer
        // (deprecated alias: per-subject variables)
        if let Some(window) = Self::window_from_env_with_alias(
            "ARETE_RATE_LIMIT_CONNECTIONS_PER_CONSUMER",
            "ARETE_RATE_LIMIT_CONNECTIONS_PER_SUBJECT",
        ) {
            config.connections_per_consumer = window;
        }

        // Connection attempts per resolved account
        // (deprecated alias: per-metering-key variables)
        if let Some(window) = Self::window_from_env_with_alias(
            "ARETE_RATE_LIMIT_CONNECTIONS_PER_ACCOUNT",
            "ARETE_RATE_LIMIT_CONNECTIONS_PER_METERING_KEY",
        ) {
            config.connections_per_account = window;
        }

        // Subscriptions per connection
        if let Some(window) = Self::window_from_env("ARETE_RATE_LIMIT_SUBSCRIPTIONS_PER_CONNECTION")
        {
            config.subscriptions_per_connection = window;
        }

        // Messages per connection
        if let Some(window) = Self::window_from_env("ARETE_RATE_LIMIT_MESSAGES_PER_CONNECTION") {
            config.messages_per_connection = window;
        }

        // Snapshots per connection
        if let Some(window) = Self::window_from_env("ARETE_RATE_LIMIT_SNAPSHOTS_PER_CONNECTION") {
            config.snapshots_per_connection = window;
        }

        // Enable/disable
        if let Ok(enabled) = std::env::var("ARETE_RATE_LIMITING_ENABLED") {
            config.enabled = enabled.parse().unwrap_or(true);
        }

        config
    }

    /// Disable rate limiting (useful for testing)
    pub fn disabled() -> Self {
        Self {
            enabled: false,
            ..Default::default()
        }
    }
}

/// Multi-tenant rate limiter with per-key tracking
#[derive(Debug)]
pub struct WebSocketRateLimiter {
    config: RateLimiterConfig,
    /// Per-IP handshake rate limits
    ip_buckets: Arc<RwLock<HashMap<String, RateLimitBucket>>>,
    /// Per-consumer connection rate limits (subject for legacy tokens)
    consumer_buckets: Arc<RwLock<HashMap<String, RateLimitBucket>>>,
    /// Per-account connection rate limits (metering key for legacy tokens)
    account_buckets: Arc<RwLock<HashMap<String, RateLimitBucket>>>,
    /// Per-consumer subscription-create rate limits (signed limit only)
    consumer_subscription_buckets: Arc<RwLock<HashMap<String, RateLimitBucket>>>,
    /// Per-account subscription-create rate limits (signed limit only)
    account_subscription_buckets: Arc<RwLock<HashMap<String, RateLimitBucket>>>,
    /// Per-connection subscription rate limits
    subscription_buckets: Arc<RwLock<HashMap<uuid::Uuid, RateLimitBucket>>>,
    /// Per-connection message rate limits
    message_buckets: Arc<RwLock<HashMap<uuid::Uuid, RateLimitBucket>>>,
    /// Per-connection snapshot rate limits
    snapshot_buckets: Arc<RwLock<HashMap<uuid::Uuid, RateLimitBucket>>>,
}

impl WebSocketRateLimiter {
    /// Create a new rate limiter with the given configuration
    pub fn new(config: RateLimiterConfig) -> Self {
        Self {
            config,
            ip_buckets: Arc::new(RwLock::new(HashMap::new())),
            consumer_buckets: Arc::new(RwLock::new(HashMap::new())),
            account_buckets: Arc::new(RwLock::new(HashMap::new())),
            consumer_subscription_buckets: Arc::new(RwLock::new(HashMap::new())),
            account_subscription_buckets: Arc::new(RwLock::new(HashMap::new())),
            subscription_buckets: Arc::new(RwLock::new(HashMap::new())),
            message_buckets: Arc::new(RwLock::new(HashMap::new())),
            snapshot_buckets: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Check if handshake is allowed from the given IP
    pub async fn check_handshake(&self, addr: SocketAddr) -> RateLimitResult {
        if !self.config.enabled {
            return RateLimitResult::Allowed {
                remaining: u32::MAX,
                reset_at: Instant::now() + Duration::from_secs(60),
            };
        }

        let ip = addr.ip().to_string();
        let mut buckets = self.ip_buckets.write().await;
        let bucket = buckets
            .entry(ip.clone())
            .or_insert_with(|| RateLimitBucket::new(self.config.handshake_per_ip));

        let result = bucket.check_and_record(Instant::now(), None);

        match &result {
            RateLimitResult::Denied { retry_after, limit } => {
                warn!(
                    ip = %ip,
                    retry_after_secs = retry_after.as_secs(),
                    limit = limit,
                    "Rate limit exceeded for handshake"
                );
            }
            RateLimitResult::Allowed { remaining, .. } => {
                debug!(
                    ip = %ip,
                    remaining = remaining,
                    "Handshake rate limit check passed"
                );
            }
        }

        result
    }

    fn allowed_unlimited() -> RateLimitResult {
        RateLimitResult::Allowed {
            remaining: u32::MAX,
            reset_at: Instant::now() + Duration::from_secs(60),
        }
    }

    async fn check_keyed_bucket(
        &self,
        buckets: &RwLock<HashMap<String, RateLimitBucket>>,
        window: RateLimitWindow,
        key: &str,
        limit_override: Option<u32>,
    ) -> RateLimitResult {
        let mut buckets = buckets.write().await;
        let bucket = buckets
            .entry(key.to_string())
            .or_insert_with(|| RateLimitBucket::new(window));
        bucket.check_and_record(Instant::now(), limit_override)
    }

    /// Check if a connection attempt is allowed for the resolved consumer.
    ///
    /// `limit_override` carries the signed
    /// `limits.max_connection_attempts_per_minute` when present; the
    /// configured window applies otherwise.
    pub async fn check_connection_for_consumer(
        &self,
        consumer: &str,
        limit_override: Option<u32>,
    ) -> RateLimitResult {
        if !self.config.enabled {
            return Self::allowed_unlimited();
        }
        self.check_keyed_bucket(
            &self.consumer_buckets,
            self.config.connections_per_consumer,
            consumer,
            limit_override,
        )
        .await
    }

    /// Check if a connection attempt is allowed for the resolved account.
    ///
    /// `limit_override` carries the signed
    /// `account_limits.max_connection_attempts_per_minute` when present; the
    /// configured window applies otherwise.
    pub async fn check_connection_for_account(
        &self,
        account: &str,
        limit_override: Option<u32>,
    ) -> RateLimitResult {
        if !self.config.enabled {
            return Self::allowed_unlimited();
        }
        self.check_keyed_bucket(
            &self.account_buckets,
            self.config.connections_per_account,
            account,
            limit_override,
        )
        .await
    }

    /// Check the signed per-consumer subscription-create rate.
    ///
    /// Enforced only when the token carries
    /// `limits.max_subscription_creates_per_minute`; a `None` limit is
    /// allowed without creating bucket state.
    pub async fn check_subscription_create_for_consumer(
        &self,
        consumer: &str,
        limit: Option<u32>,
    ) -> RateLimitResult {
        let Some(limit) = limit else {
            return Self::allowed_unlimited();
        };
        if !self.config.enabled {
            return Self::allowed_unlimited();
        }
        self.check_keyed_bucket(
            &self.consumer_subscription_buckets,
            RateLimitWindow::new(limit, Duration::from_secs(60)),
            consumer,
            Some(limit),
        )
        .await
    }

    /// Check the signed per-account subscription-create rate.
    ///
    /// Enforced only when the token carries
    /// `account_limits.max_subscription_creates_per_minute`; a `None` limit
    /// is allowed without creating bucket state.
    pub async fn check_subscription_create_for_account(
        &self,
        account: &str,
        limit: Option<u32>,
    ) -> RateLimitResult {
        let Some(limit) = limit else {
            return Self::allowed_unlimited();
        };
        if !self.config.enabled {
            return Self::allowed_unlimited();
        }
        self.check_keyed_bucket(
            &self.account_subscription_buckets,
            RateLimitWindow::new(limit, Duration::from_secs(60)),
            account,
            Some(limit),
        )
        .await
    }

    /// Check if connection is allowed for the given subject
    #[deprecated(note = "use check_connection_for_consumer with the resolved consumer identity")]
    pub async fn check_connection_for_subject(&self, subject: &str) -> RateLimitResult {
        self.check_connection_for_consumer(subject, None).await
    }

    /// Check if connection is allowed for the given metering key
    #[deprecated(note = "use check_connection_for_account with the resolved account identity")]
    pub async fn check_connection_for_metering_key(&self, metering_key: &str) -> RateLimitResult {
        self.check_connection_for_account(metering_key, None).await
    }

    /// Check if subscription is allowed for the given connection
    pub async fn check_subscription(&self, client_id: uuid::Uuid) -> RateLimitResult {
        if !self.config.enabled {
            return RateLimitResult::Allowed {
                remaining: u32::MAX,
                reset_at: Instant::now() + Duration::from_secs(60),
            };
        }

        let mut buckets = self.subscription_buckets.write().await;
        let bucket = buckets
            .entry(client_id)
            .or_insert_with(|| RateLimitBucket::new(self.config.subscriptions_per_connection));

        bucket.check_and_record(Instant::now(), None)
    }

    /// Check if message is allowed for the given connection
    pub async fn check_message(&self, client_id: uuid::Uuid) -> RateLimitResult {
        if !self.config.enabled {
            return RateLimitResult::Allowed {
                remaining: u32::MAX,
                reset_at: Instant::now() + Duration::from_secs(60),
            };
        }

        let mut buckets = self.message_buckets.write().await;
        let bucket = buckets
            .entry(client_id)
            .or_insert_with(|| RateLimitBucket::new(self.config.messages_per_connection));

        bucket.check_and_record(Instant::now(), None)
    }

    /// Check if snapshot is allowed for the given connection
    pub async fn check_snapshot(&self, client_id: uuid::Uuid) -> RateLimitResult {
        if !self.config.enabled {
            return RateLimitResult::Allowed {
                remaining: u32::MAX,
                reset_at: Instant::now() + Duration::from_secs(60),
            };
        }

        let mut buckets = self.snapshot_buckets.write().await;
        let bucket = buckets
            .entry(client_id)
            .or_insert_with(|| RateLimitBucket::new(self.config.snapshots_per_connection));

        bucket.check_and_record(Instant::now(), None)
    }

    /// Clean up stale buckets to prevent memory growth
    pub async fn cleanup_stale_buckets(&self) {
        let now = Instant::now();

        // Clean up IP buckets
        {
            let mut buckets = self.ip_buckets.write().await;
            buckets.retain(|_, bucket| {
                bucket.prune_expired(now);
                !bucket.requests.is_empty()
            });
        }

        // Clean up consumer/account connection and subscription-create buckets
        for buckets in [
            &self.consumer_buckets,
            &self.account_buckets,
            &self.consumer_subscription_buckets,
            &self.account_subscription_buckets,
        ] {
            let mut buckets = buckets.write().await;
            buckets.retain(|_, bucket| {
                bucket.prune_expired(now);
                !bucket.requests.is_empty()
            });
        }

        // Clean up connection-specific buckets for disconnected clients
        // These should be explicitly removed when clients disconnect
    }

    /// Remove all rate limit buckets for a disconnected client
    pub async fn remove_client_buckets(&self, client_id: uuid::Uuid) {
        let mut sub_buckets = self.subscription_buckets.write().await;
        sub_buckets.remove(&client_id);
        drop(sub_buckets);

        let mut msg_buckets = self.message_buckets.write().await;
        msg_buckets.remove(&client_id);
        drop(msg_buckets);

        let mut snap_buckets = self.snapshot_buckets.write().await;
        snap_buckets.remove(&client_id);
    }

    /// Start a background task to periodically clean up stale buckets
    pub fn start_cleanup_task(&self) {
        let limiter = self.clone();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(60));
            loop {
                interval.tick().await;
                limiter.cleanup_stale_buckets().await;
            }
        });
    }
}

impl Clone for WebSocketRateLimiter {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            ip_buckets: Arc::clone(&self.ip_buckets),
            consumer_buckets: Arc::clone(&self.consumer_buckets),
            account_buckets: Arc::clone(&self.account_buckets),
            consumer_subscription_buckets: Arc::clone(&self.consumer_subscription_buckets),
            account_subscription_buckets: Arc::clone(&self.account_subscription_buckets),
            subscription_buckets: Arc::clone(&self.subscription_buckets),
            message_buckets: Arc::clone(&self.message_buckets),
            snapshot_buckets: Arc::clone(&self.snapshot_buckets),
        }
    }
}

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

    fn test_config() -> RateLimiterConfig {
        RateLimiterConfig {
            enabled: true,
            handshake_per_ip: RateLimitWindow::new(60, Duration::from_secs(60)).with_burst(10),
            connections_per_consumer: RateLimitWindow::new(30, Duration::from_secs(60))
                .with_burst(5),
            connections_per_account: RateLimitWindow::new(100, Duration::from_secs(60))
                .with_burst(20),
            subscriptions_per_connection: RateLimitWindow::new(120, Duration::from_secs(60))
                .with_burst(10),
            messages_per_connection: RateLimitWindow::new(1000, Duration::from_secs(60))
                .with_burst(100),
            snapshots_per_connection: RateLimitWindow::new(30, Duration::from_secs(60))
                .with_burst(5),
        }
    }

    #[tokio::test]
    async fn test_rate_limiter_allows_within_limit() {
        let config = RateLimiterConfig {
            handshake_per_ip: RateLimitWindow::new(5, Duration::from_secs(60)),
            ..test_config()
        };
        let limiter = WebSocketRateLimiter::new(config);

        let addr: SocketAddr = "127.0.0.1:12345".parse().unwrap();

        // Should allow first 5 requests
        for i in 0..5 {
            let result = limiter.check_handshake(addr).await;
            match result {
                RateLimitResult::Allowed { remaining, .. } => {
                    assert_eq!(
                        remaining,
                        4 - i,
                        "Request {} should have {} remaining",
                        i,
                        4 - i
                    );
                }
                RateLimitResult::Denied { .. } => {
                    panic!("Request {} should be allowed", i);
                }
            }
        }
    }

    #[tokio::test]
    async fn test_rate_limiter_denies_over_limit() {
        let config = RateLimiterConfig {
            handshake_per_ip: RateLimitWindow::new(2, Duration::from_secs(60)),
            ..test_config()
        };
        let limiter = WebSocketRateLimiter::new(config);

        let addr: SocketAddr = "127.0.0.1:12345".parse().unwrap();

        // First 2 should be allowed
        limiter.check_handshake(addr).await;
        limiter.check_handshake(addr).await;

        // Third should be denied
        let result = limiter.check_handshake(addr).await;
        assert!(
            matches!(result, RateLimitResult::Denied { .. }),
            "Third request should be denied"
        );
    }

    #[tokio::test]
    async fn test_rate_limiter_with_burst() {
        let config = RateLimiterConfig {
            handshake_per_ip: RateLimitWindow::new(2, Duration::from_secs(60)).with_burst(2),
            ..test_config()
        };
        let limiter = WebSocketRateLimiter::new(config);

        let addr: SocketAddr = "127.0.0.1:12345".parse().unwrap();

        // First 4 should be allowed (2 base + 2 burst)
        for i in 0..4 {
            let result = limiter.check_handshake(addr).await;
            assert!(
                matches!(result, RateLimitResult::Allowed { .. }),
                "Request {} should be allowed with burst",
                i
            );
        }

        // Fifth should be denied
        let result = limiter.check_handshake(addr).await;
        assert!(
            matches!(result, RateLimitResult::Denied { .. }),
            "Fifth request should be denied"
        );
    }

    #[tokio::test]
    async fn test_rate_limiter_disabled() {
        let limiter = WebSocketRateLimiter::new(RateLimiterConfig::disabled());

        let addr: SocketAddr = "127.0.0.1:12345".parse().unwrap();

        // Should allow unlimited when disabled
        for _ in 0..100 {
            let result = limiter.check_handshake(addr).await;
            assert!(
                matches!(result, RateLimitResult::Allowed { .. }),
                "Should be allowed when disabled"
            );
        }
    }

    #[tokio::test]
    async fn test_consumer_rate_limiting() {
        let config = RateLimiterConfig {
            connections_per_consumer: RateLimitWindow::new(3, Duration::from_secs(60)),
            ..test_config()
        };
        let limiter = WebSocketRateLimiter::new(config);

        // First 3 connections allowed
        for i in 0..3 {
            let result = limiter
                .check_connection_for_consumer("user-123", None)
                .await;
            assert!(
                matches!(result, RateLimitResult::Allowed { remaining, .. } if remaining == 2 - i),
                "Connection {} should be allowed",
                i
            );
        }

        // Fourth denied
        let result = limiter
            .check_connection_for_consumer("user-123", None)
            .await;
        assert!(
            matches!(result, RateLimitResult::Denied { .. }),
            "Fourth connection should be denied"
        );

        // Different consumer should still work
        let result = limiter
            .check_connection_for_consumer("user-456", None)
            .await;
        assert!(
            matches!(result, RateLimitResult::Allowed { .. }),
            "Different consumer should be allowed"
        );
    }

    #[tokio::test]
    async fn signed_limits_override_configured_connection_windows() {
        let limiter = WebSocketRateLimiter::new(test_config());

        // The signed account limit (2/min) wins over the configured window.
        for _ in 0..2 {
            assert!(matches!(
                limiter
                    .check_connection_for_account("account:42", Some(2))
                    .await,
                RateLimitResult::Allowed { .. }
            ));
        }
        assert!(matches!(
            limiter
                .check_connection_for_account("account:42", Some(2))
                .await,
            RateLimitResult::Denied { .. }
        ));

        // A different account is unaffected.
        assert!(matches!(
            limiter
                .check_connection_for_account("account:43", Some(2))
                .await,
            RateLimitResult::Allowed { .. }
        ));
    }

    #[tokio::test]
    async fn subscription_create_limits_apply_only_when_signed() {
        let limiter = WebSocketRateLimiter::new(test_config());

        // No signed limit: allowed and no state is created.
        for _ in 0..10 {
            assert!(matches!(
                limiter
                    .check_subscription_create_for_account("account:42", None)
                    .await,
                RateLimitResult::Allowed { .. }
            ));
        }
        assert!(limiter.account_subscription_buckets.read().await.is_empty());

        // Signed limit of 1/min: second create denied, other accounts fine.
        assert!(matches!(
            limiter
                .check_subscription_create_for_account("account:42", Some(1))
                .await,
            RateLimitResult::Allowed { .. }
        ));
        assert!(matches!(
            limiter
                .check_subscription_create_for_account("account:42", Some(1))
                .await,
            RateLimitResult::Denied { .. }
        ));
        assert!(matches!(
            limiter
                .check_subscription_create_for_consumer("consumer:a", Some(1))
                .await,
            RateLimitResult::Allowed { .. }
        ));
    }

    #[tokio::test]
    async fn test_cleanup_stale_buckets_removes_expired_buckets() {
        let limiter = WebSocketRateLimiter::new(test_config());
        let stale_request = Instant::now() - Duration::from_secs(600);

        {
            let mut buckets = limiter.ip_buckets.write().await;
            let mut bucket = RateLimitBucket::new(limiter.config.handshake_per_ip);
            bucket.requests.push(stale_request);
            buckets.insert("127.0.0.1".to_string(), bucket);
        }

        {
            let mut buckets = limiter.consumer_buckets.write().await;
            let mut bucket = RateLimitBucket::new(limiter.config.connections_per_consumer);
            bucket.requests.push(stale_request);
            buckets.insert("user-123".to_string(), bucket);
        }

        {
            let mut buckets = limiter.account_buckets.write().await;
            let mut bucket = RateLimitBucket::new(limiter.config.connections_per_account);
            bucket.requests.push(stale_request);
            buckets.insert("account-123".to_string(), bucket);
        }

        limiter.cleanup_stale_buckets().await;

        assert!(limiter.ip_buckets.read().await.is_empty());
        assert!(limiter.consumer_buckets.read().await.is_empty());
        assert!(limiter.account_buckets.read().await.is_empty());
    }

    #[tokio::test]
    async fn test_remove_client_buckets_clears_connection_specific_state() {
        let limiter = WebSocketRateLimiter::new(test_config());
        let client_id = uuid::Uuid::new_v4();

        let _ = limiter.check_subscription(client_id).await;
        let _ = limiter.check_message(client_id).await;
        let _ = limiter.check_snapshot(client_id).await;

        assert!(limiter
            .subscription_buckets
            .read()
            .await
            .contains_key(&client_id));
        assert!(limiter
            .message_buckets
            .read()
            .await
            .contains_key(&client_id));
        assert!(limiter
            .snapshot_buckets
            .read()
            .await
            .contains_key(&client_id));

        limiter.remove_client_buckets(client_id).await;

        assert!(!limiter
            .subscription_buckets
            .read()
            .await
            .contains_key(&client_id));
        assert!(!limiter
            .message_buckets
            .read()
            .await
            .contains_key(&client_id));
        assert!(!limiter
            .snapshot_buckets
            .read()
            .await
            .contains_key(&client_id));
    }
}