How ankitktrivedi/rate-limiter works
This is a distributed Redis-backed rate-limiting service for Express. Requests enter through middleware, which builds a client/route context and asks the service to resolve applicable policies, generate keys, run each selected algorithm, and return a decision. The middleware then sets rate-limit headers, rejects over-limit requests with HTTP 429, or continues. The repository also documents four algorithms, configurable policies, failure strategies, metrics, and Redis readiness checks. Some subsystem links are based on README documentation or the composition root; unsampled implementation details are not inferred.
Main entry points: RateLimitService.ts Rate-limit service · AlgorithmRegistry.ts Algorithm registry · server.ts Server startup · app.ts Express app
Components 28
HTTP application
- Server startup
src/server.ts - Express app
src/app.ts - Rate-limit middleware
src/middleware/rateLimiter.ts - Response headers
src/http/RateLimitHeaders.ts - Health routes
src/routes/health.ts
Policy and decision
- Rate-limit service
src/core/RateLimitService.ts - Policy engine
src/core/PolicyEngine.ts - Static policy resolver
src/policies/StaticPolicyResolver.ts - Key generator
src/core/DefaultRateLimitKeyGenerator.ts - Algorithm registry
src/core/AlgorithmRegistry.ts - Failure strategies
src/core/failure - Metrics
src/observability/ConsoleRateLimitMetrics.ts - Policy validation
src/validation/RateLimitPolicyValidator.ts - Rate-limit result
src/core/RateLimitResult.ts
Algorithms and state
- Fixed window
src/algorithms/fixed-window/FixedWindow.ts - Sliding window
src/algorithms/sliding-window/SlidingWindow.ts - Token bucket
src/algorithms/token-bucket/TokenBucket.ts - Leaky bucket
src/algorithms/leaky-bucket/LeakyBucket.ts - Store contracts
src/core/RateLimitStore.ts - Redis stores
src/stores/RedisStores.ts - Sliding store
src/stores/RedisSlidingWindowStore.ts - Token store
src/stores/RedisTokenBucketStore.ts - Leaky store
src/stores/RedisLeakyBucketStore.ts
Redis and operations
- Redis connection
src/config/redis.ts - Redis
- Operation timeout
src/infrastructure/RedisOperationExecutor.ts - Redis health check
src/health/RedisHealthCheck.ts
Other components
- HTTP client
Connections 33
- HTTP client → Rate-limit middleware: sends request
- Server startup → Redis connection: connects
src/server.ts - Server startup → Policy validation: validates policies
src/server.ts - Server startup → Express app: starts
src/server.ts - Server startup → Rate-limit service: creates
src/server.ts - Express app → Rate-limit middleware: registers
src/app.ts - Express app → Health routes: mounts
src/app.ts - Rate-limit middleware → Rate-limit service: checks context
src/middleware/rateLimiter.ts - Rate-limit middleware → Response headers: sets headers
src/middleware/rateLimiter.ts - Health routes → Redis health check: checks readiness
src/routes/health.ts - Rate-limit service → Policy engine: resolves policies
src/core/RateLimitService.ts - Rate-limit service → Algorithm registry: creates algorithm
src/core/RateLimitService.ts - Rate-limit service → Failure strategies: handles errors
src/core/RateLimitService.ts - Rate-limit service → Metrics: records outcomes
src/core/RateLimitService.ts - Policy engine → Static policy resolver: resolves policies
src/core/PolicyEngine.ts - Policy engine → Key generator: generates keys
src/core/PolicyEngine.ts - Algorithm registry → Fixed window: selects algorithm
src/config/createRateLimiter.ts - Algorithm registry → Sliding window: selects algorithm
src/config/createRateLimiter.ts - Algorithm registry → Token bucket: selects algorithm
src/config/createRateLimiter.ts - Algorithm registry → Leaky bucket: selects algorithm
src/config/createRateLimiter.ts - Fixed window → Store contracts: uses store
src/algorithms/fixed-window/FixedWindow.ts - Sliding window → Store contracts: uses store
src/algorithms/sliding-window/SlidingWindow.ts - Token bucket → Store contracts: uses store
src/algorithms/token-bucket/TokenBucket.ts - Leaky bucket → Store contracts: uses store
src/algorithms/leaky-bucket/LeakyBucket.ts - Redis stores → Redis: stores state
- Sliding store → Redis: stores state
- Token store → Redis: stores state
- Leaky store → Redis: stores state
- Sliding store → Operation timeout: executes operations
src/stores/RedisSlidingWindowStore.ts - Token store → Operation timeout: executes operations
src/stores/RedisTokenBucketStore.ts - Leaky store → Operation timeout: executes operations
src/stores/RedisLeakyBucketStore.ts - Redis connection → Redis: connects
src/config/redis.ts - Express app → Redis health check: constructs
src/app.ts