Skip to content

feat: add IP-based rate limiting with Redis-backed sliding window#18

Merged
mattevans merged 4 commits intomasterfrom
feat/rate-limiting-middleware
Oct 28, 2025
Merged

feat: add IP-based rate limiting with Redis-backed sliding window#18
mattevans merged 4 commits intomasterfrom
feat/rate-limiting-middleware

Conversation

@mattevans
Copy link
Member

@mattevans mattevans commented Oct 28, 2025

Introduce configurable rate limiting to protect expensive endpoints and prevent abuse. The implementation uses Redis for distributed state so limits are enforced consistently across multiple server instances.

Key additions:

  • New rate_limiting config section with rules, exempt IPs, and failure mode (fail-open/fail-closed).
  • ratelimit.Service backed by Redis INCR+EXPIRE sliding-window logic.
  • Middleware that applies the first matching rule based on request path regex, adds standard X-RateLimit-* and Retry-After headers, and emits Prometheus metrics.
  • Comprehensive unit and integration tests, including miniredis-based concurrency and failure-mode scenarios.

The middleware is inserted after CORS but before recovery so that rate-limit responses are returned early and consistently.

==========================================
  Burst Rate Limiting Test
==========================================

Configuration:
  URL:      http://localhost:8080/api/v1/mainnet/bounds
  Requests: 70 (all at once)

Firing 70 concurrent requests NOW...

✓ All requests completed

Results:
  ✓ Allowed:       30
  ✗ Denied:        40

Sample 429 Response:
{"error":"rate limit exceeded","retry_after":59,"status":429}


✓ Rate limiting works! Denied       40 out of 70 requests

Introduce configurable rate limiting to protect expensive endpoints and
prevent abuse. The implementation uses Redis for distributed state so
limits are enforced consistently across multiple server instances.

Key additions:
- New `rate_limiting` config section with rules, exempt IPs, and failure
 mode (fail-open/fail-closed).
- `ratelimit.Service` backed by Redis INCR+EXPIRE sliding-window logic.
- Middleware that applies the first matching rule based on request path
 regex, adds standard `X-RateLimit-*` and `Retry-After` headers, and
 emits Prometheus metrics.
- Comprehensive unit and integration tests, including miniredis-based
 concurrency and failure-mode scenarios.

The middleware is inserted after CORS but before recovery so that
rate-limit responses are returned early and consistently.
@mattevans mattevans self-assigned this Oct 28, 2025
…ignature

refactor(server.go): reorder RateLimit parameters to logger-first convention
Check Redis availability during Start() and log the result.
When failure_mode is "fail_closed", abort startup if Redis is
unreachable to prevent the service from running in a broken state.
@mattevans mattevans merged commit 7cbf34d into master Oct 28, 2025
3 checks passed
@mattevans mattevans deleted the feat/rate-limiting-middleware branch October 28, 2025 00:33
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.

2 participants