Skip to content

feat: add per-tenant token bucket rate limiting middleware (closes #115)#208

Merged
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
akordavid373:feat/issue-115-tenant-rate-limit-middleware
Jul 21, 2026
Merged

feat: add per-tenant token bucket rate limiting middleware (closes #115)#208
elizabetheonoja-art merged 1 commit into
Utility-Protocol:mainfrom
akordavid373:feat/issue-115-tenant-rate-limit-middleware

Conversation

@akordavid373

Copy link
Copy Markdown
Contributor

Summary

Implements per-tenant token bucket rate limiting middleware for the API (Issue #115).

Changes

  • src/api/middleware.rs — Added TenantRateLimiter struct with per-tenant independent token buckets, configurable limits per tenant with override support, and periodic pruning of inactive tenants. Added tenant_rate_limit_layer Axum middleware that extracts tenant identity from the X-Tenant-ID header (falls back to _anonymous).
  • src/api/mod.rs — Added TenantRateLimiter to AppState with FromRef impl.
  • src/api/router.rs — Wired in the tenant_rate_limit_layer middleware (before existing IP-based rate limiter) and added /api/v1/tenant-rate-limiter/status endpoint.
  • src/api/handlers.rs — Added tenant_rate_limiter_status handler returning per-tenant limits and rejection counts.
  • src/main.rs — Initialized TenantRateLimiter with default 1000 tokens/sec.
  • tests/rate_limit_integration.rs — Added integration tests for tenant-specific buckets, separate bucket isolation, and anonymous fallback.

How it works

  1. Clients send requests with an X-Tenant-ID header identifying their tenant
  2. Each tenant gets its own token bucket (independent refill + burst capacity)
  3. When a bucket is exhausted, the request is rejected with HTTP 429
  4. Operator can configure per-tenant overrides via TenantRateLimiter::set_tenant_limit() (e.g., for premium tenants)
  5. Anonymous requests (no header) share a single _anonymous bucket
  6. Inactive tenants are pruned after 10 minutes of inactivity

Closes #115

…ility-Protocol#115)

- Add TenantRateLimiter with per-tenant token buckets, configurable
  limits, override support, and background tenant pruning
- Add tenant_rate_limit_layer Axum middleware that extracts tenant
  from X-Tenant-ID header (falls back to _anonymous)
- Add /api/v1/tenant-rate-limiter/status endpoint for observability
- Integrate into AppState, router, and main.rs
- Add comprehensive unit and integration tests

Implements: Utility-Protocol#115
@elizabetheonoja-art
elizabetheonoja-art merged commit 2b56712 into Utility-Protocol:main Jul 21, 2026
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Rate Limiting Middleware with Per-Tenant Token Buckets

2 participants