Description
The gateway currently uses a single token-bucket per key in src/services/rateLimiter.ts with one default maxRequests/windowMs pair. To support paid plans and abuse control, we need per-tier policies (e.g. free, pro, enterprise) resolved from the API key record so that /v1/call/* enforces the correct ceiling for each tier without code changes.
Requirements and Context
- Extend
RateLimiterConfig to accept a tier -> { maxRequests, windowMs } map and a tier resolver that reads from apiKeyRepository
- Wire
gatewayApiKeyAuth middleware to set res.locals.apiKeyTier so proxyRoutes passes it into rateLimiter.check
- Keep backwards-compatible defaults when no tier is present
- Must be secure, tested, and documented
- Should be efficient and easy to review
Suggested Execution
- Fork the repo and create a branch
git checkout -b feature/tiered-rate-limits
- Implement changes
src/services/rateLimiter.ts
src/middleware/gatewayApiKeyAuth.ts
src/routes/proxyRoutes.ts
src/repositories/apiKeyRepository.ts
- Test and commit
npm test -- rateLimiter
- Cover edge cases (missing tier, unknown tier, hot-reload)
- Include test output and notes in the PR
Example commit message
feat: tiered rate limit policies driven by api key plan
Acceptance Criteria
Guidelines
- Backend (Node/TS): real service/route/module paths, tests with Jest, input validation, error handling, minimum 90% test coverage
- Clear documentation and inline comments
- Timeframe: 96 hours
Description
The gateway currently uses a single token-bucket per key in
src/services/rateLimiter.tswith one defaultmaxRequests/windowMspair. To support paid plans and abuse control, we need per-tier policies (e.g.free,pro,enterprise) resolved from the API key record so that/v1/call/*enforces the correct ceiling for each tier without code changes.Requirements and Context
RateLimiterConfigto accept atier -> { maxRequests, windowMs }map and a tier resolver that reads fromapiKeyRepositorygatewayApiKeyAuthmiddleware to setres.locals.apiKeyTiersoproxyRoutespasses it intorateLimiter.checkSuggested Execution
src/services/rateLimiter.tssrc/middleware/gatewayApiKeyAuth.tssrc/routes/proxyRoutes.tssrc/repositories/apiKeyRepository.tsnpm test -- rateLimiterExample commit message
Acceptance Criteria
rateLimiter.checkhonors per-tiermaxRequests/windowMsdocs/describing tier configGuidelines