kindly-guard-server 0.11.14

KindlyGuard MCP server - Enterprise-grade security for AI model interactions
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
// Copyright 2025 Kindly Software Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Hierarchical Rate Limiter with Per-CPU Token Buckets
//! 
//! Achieves linear scaling to 64+ cores through:
//! - Per-CPU token buckets eliminating contention
//! - Work-stealing for load balancing
//! - Cache line alignment for NUMA efficiency
//! - Lock-free atomic operations

use crate::traits::{RateLimiter, RateLimitKey, RateLimitDecision, RateLimiterStats};
use anyhow::Result;
use async_trait::async_trait;
use crossbeam_deque::{Injector, Stealer, Worker};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::collections::HashMap;
use parking_lot::RwLock;

/// Cache line size for alignment (64 bytes on x86_64)
const CACHE_LINE_SIZE: usize = 64;

/// Maximum cores we support
const MAX_CORES: usize = 128;

/// Token redistribution threshold (when to steal)
const STEAL_THRESHOLD: f64 = 0.2;  // Steal when below 20% capacity

/// Per-CPU token bucket aligned to cache line
#[repr(align(64))]
struct CpuTokenBucket {
    /// Current tokens (packed: [tokens:32][version:32])
    state: AtomicU64,
    
    /// Refill rate (tokens per second)
    refill_rate: f64,
    
    /// Maximum capacity
    capacity: f64,
    
    /// Last refill timestamp (nanoseconds)
    last_refill_ns: AtomicU64,
    
    /// Statistics
    requests_served: AtomicU64,
    tokens_stolen: AtomicU64,
    
    /// Padding to ensure cache line alignment
    _padding: [u8; CACHE_LINE_SIZE - 48],
}

/// Work-stealing deque for token redistribution
struct TokenStealer {
    /// Worker queue for this CPU
    worker: Worker<TokenTransfer>,
    
    /// Stealers from other CPUs
    stealers: Vec<Stealer<TokenTransfer>>,
    
    /// Global injector for overflow
    injector: Arc<Injector<TokenTransfer>>,
}

/// Token transfer packet
#[derive(Clone, Copy)]
struct TokenTransfer {
    tokens: f64,
    from_cpu: usize,
    timestamp: u64,
}

/// Hierarchical rate limiter implementation
pub struct HierarchicalRateLimiter {
    /// Per-CPU buckets
    cpu_buckets: Vec<CpuTokenBucket>,
    
    /// Work-stealing infrastructure
    stealers: Vec<TokenStealer>,
    
    /// Global overflow injector
    global_injector: Arc<Injector<TokenTransfer>>,
    
    /// Client-specific limits (RwLock for rare updates)
    client_limits: Arc<RwLock<HashMap<String, ClientRateLimit>>>,
    
    /// Default configuration
    default_rpm: u32,
    default_burst: u32,
    
    /// Number of CPUs
    num_cpus: usize,
    
    /// Statistics
    total_requests: AtomicU64,
    total_steals: AtomicU64,
    cache_hits: AtomicU64,
}

/// Client-specific rate limit configuration
#[derive(Clone)]
struct ClientRateLimit {
    rpm: u32,
    burst: u32,
    priority: u8,
}

impl CpuTokenBucket {
    fn new(rpm: u32, burst: u32) -> Self {
        let capacity = burst as f64;
        let refill_rate = rpm as f64 / 60.0;
        
        // Pack initial state: full tokens, version 0
        let initial_state = ((capacity as u32) as u64) << 32;
        
        Self {
            state: AtomicU64::new(initial_state),
            refill_rate,
            capacity,
            last_refill_ns: AtomicU64::new(0),
            requests_served: AtomicU64::new(0),
            tokens_stolen: AtomicU64::new(0),
            _padding: [0; CACHE_LINE_SIZE - 48],
        }
    }
    
    /// Try to consume tokens with atomic CAS
    fn try_consume(&self, tokens: f64) -> bool {
        // First, refill based on elapsed time
        self.refill();
        
        // Atomic CAS loop
        let mut current = self.state.load(Ordering::Acquire);
        
        loop {
            let current_tokens = (current >> 32) as f32;
            let version = current as u32;
            
            if current_tokens < tokens as f32 {
                return false;  // Not enough tokens
            }
            
            // Calculate new state
            let new_tokens = current_tokens - tokens as f32;
            let new_state = ((new_tokens as u32) as u64) << 32 | ((version + 1) as u64);
            
            match self.state.compare_exchange_weak(
                current,
                new_state,
                Ordering::Release,
                Ordering::Acquire,
            ) {
                Ok(_) => {
                    self.requests_served.fetch_add(1, Ordering::Relaxed);
                    return true;
                }
                Err(actual) => current = actual,
            }
        }
    }
    
    /// Refill tokens based on elapsed time
    fn refill(&self) {
        let now_ns = Instant::now().elapsed().as_nanos() as u64;
        let last_ns = self.last_refill_ns.load(Ordering::Acquire);
        
        // Only refill if enough time has passed (avoid contention)
        if now_ns.saturating_sub(last_ns) < 1_000_000 { // 1ms minimum
            return;
        }
        
        // Try to update last refill time
        match self.last_refill_ns.compare_exchange(
            last_ns,
            now_ns,
            Ordering::Release,
            Ordering::Acquire,
        ) {
            Ok(_) => {
                // We won the refill race
                let elapsed_secs = (now_ns - last_ns) as f64 / 1_000_000_000.0;
                let tokens_to_add = elapsed_secs * self.refill_rate;
                
                // Add tokens atomically
                let mut current = self.state.load(Ordering::Acquire);
                loop {
                    let current_tokens = (current >> 32) as f32;
                    let version = current as u32;
                    
                    let new_tokens = (current_tokens + tokens_to_add as f32).min(self.capacity as f32);
                    let new_state = ((new_tokens as u32) as u64) << 32 | ((version + 1) as u64);
                    
                    match self.state.compare_exchange_weak(
                        current,
                        new_state,
                        Ordering::Release,
                        Ordering::Acquire,
                    ) {
                        Ok(_) => break,
                        Err(actual) => current = actual,
                    }
                }
            }
            Err(_) => {
                // Another thread is refilling
            }
        }
    }
    
    /// Get current token count
    fn tokens(&self) -> f64 {
        let state = self.state.load(Ordering::Acquire);
        (state >> 32) as f32 as f64
    }
    
    /// Steal tokens from this bucket
    fn steal_tokens(&self, amount: f64) -> Option<f64> {
        let mut current = self.state.load(Ordering::Acquire);
        
        loop {
            let current_tokens = (current >> 32) as f32;
            let version = current as u32;
            
            // Only steal if we have excess tokens
            if current_tokens < self.capacity as f32 * 0.5 {
                return None;
            }
            
            let steal_amount = amount.min((current_tokens as f64 - self.capacity * 0.3).max(0.0));
            if steal_amount < 1.0 {
                return None;
            }
            
            let new_tokens = current_tokens - steal_amount as f32;
            let new_state = ((new_tokens as u32) as u64) << 32 | ((version + 1) as u64);
            
            match self.state.compare_exchange_weak(
                current,
                new_state,
                Ordering::Release,
                Ordering::Acquire,
            ) {
                Ok(_) => {
                    self.tokens_stolen.fetch_add(steal_amount as u64, Ordering::Relaxed);
                    return Some(steal_amount);
                }
                Err(actual) => current = actual,
            }
        }
    }
    
    /// Inject tokens into this bucket
    fn inject_tokens(&self, amount: f64) {
        let mut current = self.state.load(Ordering::Acquire);
        
        loop {
            let current_tokens = (current >> 32) as f32;
            let version = current as u32;
            
            let new_tokens = (current_tokens + amount as f32).min(self.capacity as f32);
            let new_state = ((new_tokens as u32) as u64) << 32 | ((version + 1) as u64);
            
            match self.state.compare_exchange_weak(
                current,
                new_state,
                Ordering::Release,
                Ordering::Acquire,
            ) {
                Ok(_) => break,
                Err(actual) => current = actual,
            }
        }
    }
}

impl HierarchicalRateLimiter {
    pub fn new(default_rpm: u32, default_burst: u32) -> Self {
        let num_cpus = num_cpus::get().min(MAX_CORES);
        
        // Create per-CPU buckets
        let mut cpu_buckets = Vec::with_capacity(num_cpus);
        for _ in 0..num_cpus {
            cpu_buckets.push(CpuTokenBucket::new(default_rpm, default_burst));
        }
        
        // Create work-stealing infrastructure
        let global_injector = Arc::new(Injector::new());
        let mut workers = Vec::with_capacity(num_cpus);
        let mut stealers_list = Vec::with_capacity(num_cpus);
        
        for _ in 0..num_cpus {
            let worker = Worker::new_fifo();
            stealers_list.push(worker.stealer());
            workers.push(worker);
        }
        
        // Create stealer structures
        let mut stealers = Vec::with_capacity(num_cpus);
        for (i, worker) in workers.into_iter().enumerate() {
            let mut other_stealers = Vec::with_capacity(num_cpus - 1);
            for (j, stealer) in stealers_list.iter().enumerate() {
                if i != j {
                    other_stealers.push(stealer.clone());
                }
            }
            
            stealers.push(TokenStealer {
                worker,
                stealers: other_stealers,
                injector: global_injector.clone(),
            });
        }
        
        Self {
            cpu_buckets,
            stealers,
            global_injector,
            client_limits: Arc::new(RwLock::new(HashMap::new())),
            default_rpm,
            default_burst,
            num_cpus,
            total_requests: AtomicU64::new(0),
            total_steals: AtomicU64::new(0),
            cache_hits: AtomicU64::new(0),
        }
    }
    
    /// Get CPU index using thread-local optimization
    fn get_cpu_index(&self) -> usize {
        // In production, use thread_local with CPU affinity
        // For now, use simple hash of thread ID
        let thread_id = std::thread::current().id();
        let hash = thread_id.as_u64().get() as usize;
        hash % self.num_cpus
    }
    
    /// Try work-stealing to balance load
    fn try_work_stealing(&self, cpu_idx: usize, needed_tokens: f64) -> Option<f64> {
        let stealer = &self.stealers[cpu_idx];
        
        // First, check local work queue
        if let Some(transfer) = stealer.worker.pop() {
            return Some(transfer.tokens);
        }
        
        // Try stealing from other CPUs
        for (i, other_stealer) in stealer.stealers.iter().enumerate() {
            if let Some(transfer) = other_stealer.steal().success() {
                self.total_steals.fetch_add(1, Ordering::Relaxed);
                return Some(transfer.tokens);
            }
            
            // Also try direct stealing from buckets
            let other_idx = if i >= cpu_idx { i + 1 } else { i };
            if let Some(stolen) = self.cpu_buckets[other_idx].steal_tokens(needed_tokens) {
                self.total_steals.fetch_add(1, Ordering::Relaxed);
                return Some(stolen);
            }
        }
        
        // Finally, check global injector
        if let Some(transfer) = stealer.injector.steal().success() {
            self.total_steals.fetch_add(1, Ordering::Relaxed);
            return Some(transfer.tokens);
        }
        
        None
    }
    
    /// Get rate limits for a client
    fn get_limits(&self, client_id: &str) -> (u32, u32) {
        let limits = self.client_limits.read();
        if let Some(limit) = limits.get(client_id) {
            (limit.rpm, limit.burst)
        } else {
            (self.default_rpm, self.default_burst)
        }
    }
}

#[async_trait]
impl RateLimiter for HierarchicalRateLimiter {
    async fn check_rate_limit(&self, key: &RateLimitKey) -> Result<RateLimitDecision> {
        self.total_requests.fetch_add(1, Ordering::Relaxed);
        
        // Get CPU-local bucket
        let cpu_idx = self.get_cpu_index();
        let bucket = &self.cpu_buckets[cpu_idx];
        
        // Try local bucket first (hot path)
        if bucket.try_consume(1.0) {
            self.cache_hits.fetch_add(1, Ordering::Relaxed);
            return Ok(RateLimitDecision {
                allowed: true,
                tokens_remaining: bucket.tokens(),
                reset_after: Duration::from_secs(1),
            });
        }
        
        // Local bucket empty, try work-stealing
        if let Some(stolen_tokens) = self.try_work_stealing(cpu_idx, 1.0) {
            bucket.inject_tokens(stolen_tokens);
            
            // Retry with injected tokens
            if bucket.try_consume(1.0) {
                return Ok(RateLimitDecision {
                    allowed: true,
                    tokens_remaining: bucket.tokens(),
                    reset_after: Duration::from_secs(1),
                });
            }
        }
        
        // All buckets exhausted
        Ok(RateLimitDecision {
            allowed: false,
            tokens_remaining: 0.0,
            reset_after: Duration::from_secs(1),
        })
    }
    
    async fn record_request(&self, _key: &RateLimitKey) -> Result<()> {
        // Already recorded in check_rate_limit
        Ok(())
    }
    
    async fn apply_penalty(&self, client_id: &str, factor: f32) -> Result<()> {
        // Apply penalty by reducing effective rate
        let mut limits = self.client_limits.write();
        let (rpm, burst) = self.get_limits(client_id);
        
        limits.insert(client_id.to_string(), ClientRateLimit {
            rpm: (rpm as f32 * factor) as u32,
            burst: (burst as f32 * factor) as u32,
            priority: 0,
        });
        
        Ok(())
    }
    
    fn get_stats(&self) -> RateLimiterStats {
        let total_requests = self.total_requests.load(Ordering::Relaxed);
        let cache_hits = self.cache_hits.load(Ordering::Relaxed);
        let cache_hit_rate = if total_requests > 0 {
            (cache_hits as f64 / total_requests as f64) * 100.0
        } else {
            0.0
        };
        
        // Calculate total tokens across all CPUs
        let mut total_tokens = 0.0;
        let mut total_served = 0u64;
        
        for bucket in &self.cpu_buckets {
            total_tokens += bucket.tokens();
            total_served += bucket.requests_served.load(Ordering::Relaxed);
        }
        
        RateLimiterStats {
            requests_allowed: total_served,
            requests_denied: total_requests - total_served,
            active_buckets: self.num_cpus as u64,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use tokio::sync::Barrier;
    
    #[test]
    fn test_cpu_token_bucket() {
        let bucket = CpuTokenBucket::new(60, 10);
        
        // Should start with full capacity
        assert_eq!(bucket.tokens() as u32, 10);
        
        // Consume some tokens
        assert!(bucket.try_consume(5.0));
        assert_eq!(bucket.tokens() as u32, 5);
        
        // Can't consume more than available
        assert!(!bucket.try_consume(10.0));
        
        // Can consume remaining
        assert!(bucket.try_consume(5.0));
        assert_eq!(bucket.tokens() as u32, 0);
    }
    
    #[tokio::test]
    async fn test_hierarchical_rate_limiter() {
        let limiter = Arc::new(HierarchicalRateLimiter::new(600, 100));
        
        let key = RateLimitKey {
            client_id: "test".to_string(),
            method: None,
        };
        
        // Should allow initial burst
        for _ in 0..50 {
            let decision = limiter.check_rate_limit(&key).await.unwrap();
            assert!(decision.allowed);
        }
        
        // Check stats
        let stats = limiter.get_stats();
        assert!(stats.requests_allowed >= 50);
    }
    
    #[tokio::test]
    async fn test_concurrent_access() {
        let limiter = Arc::new(HierarchicalRateLimiter::new(10000, 1000));
        let barrier = Arc::new(Barrier::new(10));
        
        let mut handles = vec![];
        
        // Spawn 10 concurrent tasks
        for i in 0..10 {
            let limiter_clone = limiter.clone();
            let barrier_clone = barrier.clone();
            
            handles.push(tokio::spawn(async move {
                // Wait for all tasks to be ready
                barrier_clone.wait().await;
                
                let key = RateLimitKey {
                    client_id: format!("client_{}", i),
                    method: None,
                };
                
                // Each task makes 100 requests
                let mut allowed = 0;
                for _ in 0..100 {
                    let decision = limiter_clone.check_rate_limit(&key).await.unwrap();
                    if decision.allowed {
                        allowed += 1;
                    }
                }
                
                allowed
            }));
        }
        
        // Wait for all tasks
        let mut total_allowed = 0;
        for handle in handles {
            total_allowed += handle.await.unwrap();
        }
        
        // Should handle concurrent access correctly
        assert!(total_allowed > 500); // Most requests should succeed
        
        // Check cache hit rate
        let stats = limiter.get_stats();
        println!("Cache hit rate: {:.2}%", 
            (limiter.cache_hits.load(Ordering::Relaxed) as f64 / 
             limiter.total_requests.load(Ordering::Relaxed) as f64) * 100.0);
    }
}