Commit 60f07cb
committed
feat(rate-limiter): add rust-backed execution engine with benchmarks and review fixes
Introduce a Rust-backed rate limiter engine via PyO3 for the hot-path
evaluation, keeping Python as the lifecycle/policy owner with full
fallback support.
Engine:
- RateLimiterEngine with single evaluate_many() call per hook invocation
- Memory and Redis backends with all three algorithms (fixed_window,
sliding_window, token_bucket)
- EVALSHA caching with NOSCRIPT fallback for Redis (REDIS-02)
- Process-global monotonic clock for cross-thread correctness
- Amortized key eviction for idle memory-backend keys (MEM-06)
- Process-unique sorted-set members preventing multi-instance collision
- Saturating arithmetic for token bucket overflow safety
Python integration:
- Rust fast-path with async Redis bridge (evaluate_many_async)
- Python fallback preserved when Rust unavailable
- Pre-parsed rate strings at init for both Rust and Python paths
- Zero-count rate string rejection matching Rust validation
- allow_many() return length consistency fix
- Fail-open and retry_after=min(blocked) policies documented as contracts
Auth:
- Centralised resolve_session_teams() for session-token team resolution
- JWT intersection policy via _narrow_by_jwt_teams()
- Cache stores raw DB teams, not narrowed intersection
- tenant_id propagation from team_id for by_tenant rate limiting
Tests and benchmarks:
- 167 unit tests (15 new) covering Rust path, sweep, identity fallback
- Criterion benchmarks measuring steady-state under-limit hot path
- Python vs Rust comparison harness (compare_performance.py)
- Three-tier load tests: correctness, scale, Redis capacity
- HTTP 429 classification fix in load test response handling
Signed-off-by: Pratik Gandhi <gandhipratik203@gmail.com>1 parent 4bfc250 commit 60f07cb
25 files changed
Lines changed: 7593 additions & 64 deletions
File tree
- plugins_rust/rate_limiter
- benches
- python/rate_limiter_rust
- src
- bin
- plugins
- rate_limiter
- tests
- integration
- loadtest
- unit/mcpgateway/plugins/plugins/rate_limiter
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
| 306 | + | |
306 | 307 | | |
307 | 308 | | |
308 | 309 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2322 | 2322 | | |
2323 | 2323 | | |
2324 | 2324 | | |
| 2325 | + | |
2325 | 2326 | | |
2326 | 2327 | | |
2327 | 2328 | | |
| 2329 | + | |
| 2330 | + | |
2328 | 2331 | | |
2329 | 2332 | | |
2330 | 2333 | | |
| |||
2445 | 2448 | | |
2446 | 2449 | | |
2447 | 2450 | | |
2448 | | - | |
| 2451 | + | |
2449 | 2452 | | |
2450 | 2453 | | |
2451 | 2454 | | |
| |||
2475 | 2478 | | |
2476 | 2479 | | |
2477 | 2480 | | |
| 2481 | + | |
| 2482 | + | |
| 2483 | + | |
| 2484 | + | |
| 2485 | + | |
| 2486 | + | |
| 2487 | + | |
| 2488 | + | |
| 2489 | + | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
2478 | 2511 | | |
2479 | 2512 | | |
2480 | 2513 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
| 217 | + | |
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
| |||
0 commit comments