feat(api): add global rate-limiting and circuit breaker for API routes (fixes #701) - #705
Conversation
|
@Diwakar-odds is attempting to deploy a commit to the vishnukothakapu's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Caution Review failedAn error occurred during the review process. Please try again later. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hi maintainers, Thank you for assigning me to this issue! This PR implements the requested Edge middleware rate-limiting (Token Bucket via Upstash Redis) and the Circuit Breaker pattern. Technical Analysis & XP Label Justification:
Because these changes involve core backend logic, architectural patterns (Circuit Breaker), and security/performance hardening, this PR firmly qualifies for Level 3 and good-backend. Could you please add the ECSoC26, Level 3, good-backend, and good-pr labels to reflect this complex backend contribution? Looking forward to your review! 🚀 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@Diwakar-odds , I noticed the Vercel build is failing on your The Prisma/database step is passing now, but the TypeScript build fails in Just wanted to flag this so you can take a look. |
Summary
This PR implements a robust Edge middleware utilizing a Token Bucket rate-limiting algorithm and a Circuit Breaker pattern for external dependencies, resolving #701.
Motivation
Closes #701. The application was previously exposed to distributed brute-force attacks on NextAuth login endpoints and malicious link spamming on the
api/linksAPI. Additionally, third-party API dependencies had no fault tolerance when they experienced downtime. This PR introduces a robustlib/rate-limit.tsusing Redis, and a Circuit Breaker pattern inlib/circuit-breaker.tsto fail fast and protect the application.Changes
lib/circuit-breaker.ts: Implemented an in-memoryCircuitBreakerclass managingCLOSED,OPEN, andHALF_OPENstates for fault tolerance.lib/rate-limit.ts: Configured Upstash RedisRatelimitusing the Token Bucket algorithm (5 requests/15m for/api/authand 30 requests/1m for/api/links).middleware.ts: Integrated the new Token Bucket rate-limiting logic at the edge before hitting serverless functions.app/api/auth/[...nextauth]/route.ts: Wrapped the NextAuth handler with the Circuit Breaker to return a 503 upon failure.app/api/links/route.ts: Wrapped Prisma DB interactions with the Circuit Breaker to prevent hanging queries.Acceptance Criteria
middleware.ts.Impact & Side Effects
No breaking changes. This ensures the service doesn't cascade failures when upstream DB or OAuth providers go down.
How to Test
npm run devwith Upstash Redis credentials./api/auth/signin6 times in 15 minutes to observe the 429 response./api/linksendpoint repeatedly, and observe the 503 fail-fast response.Quality Checklist