Skip to content

feat: rate limiting for REST API#90

Open
Samaro1 wants to merge 1 commit intoCalloraOrg:mainfrom
Samaro1:feature/rest-rate-limit
Open

feat: rate limiting for REST API#90
Samaro1 wants to merge 1 commit intoCalloraOrg:mainfrom
Samaro1:feature/rest-rate-limit

Conversation

@Samaro1
Copy link

@Samaro1 Samaro1 commented Feb 25, 2026

Implements REST API rate limiting for the Callora backend using a two-tier model:
Global per-IP limit (100 req/min)
Per-user limit for authenticated routes (200 req/min)
This is independent of the existing gateway per-key limits.

What’s Included
Rate Limiting Logic
Token bucket algorithm for accurate request control

In-memory store with:
Cleanup every 5 minutes
30-minute TTL for inactive entries
Sub-millisecond evaluation time

Middleware
globalRateLimit() → applied to all routes (IP-based)
perUserRateLimit() → applied to authenticated routes (JWT sub claim)

Proper IP resolution:
X-Forwarded-For
X-Real-IP
Fallback to socket address

Headers
All successful responses include:
X-RateLimit-Limit
X-RateLimit-Remaining
Rate-limited responses (429) include:
Retry-After

JSON body:
{
"error": "Too Many Requests",
"message": "Rate limit exceeded. Maximum 100 requests per minute per IP.",
"retryAfter": 45
}

Testing
31 tests (all passing)
Token bucket correctness
Isolation between global and per-user limits
Header validation
Middleware integration coverage

Run:

npm test
Configuration (Defaults)
Global: 100 requests/minute per IP
Per-user: 200 requests/minute per user
Window: 60 seconds
Cleanup: 5 minutes
Entry TTL: 30 minutes

Configurable via:

const rateLimiter = new RateLimiter(
{ windowMs: 60000, maxRequests: 150 },
{ windowMs: 60000, maxRequests: 300 }
);
image

Closes #60

- Global rate limit: 100 requests/min per IP
- Per-user rate limit: 200 requests/min per authenticated user
- Token bucket algorithm for precise rate limiting
- In-memory storage with automatic cleanup
- Returns 429 with Retry-After header
- Comprehensive documentation and test coverage
- Includes IP extraction, JWT user ID parsing
- Non-blocking for gateway operations
@greatest0fallt1me
Copy link
Contributor

please resolve the conflicts

@greatest0fallt1me
Copy link
Contributor

@Samaro1 Can you resolve the conflicts?

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.

Add Rate Limiting for REST API (Global and Per-User)

2 participants