-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement rate limit #195
base: dev
Are you sure you want to change the base?
implement rate limit #195
Conversation
@akiraonstarknet |
@Benjtalkshow please deploy on vercel and include the deployment link in the description |
src/middleware.ts
Outdated
|
||
const ratelimit = new Ratelimit({ | ||
redis, | ||
limiter: Ratelimit.slidingWindow(20, '10 s'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use env
variables to configure tokens
and window
arguments with a fallback to the default values.
src/middleware.ts
Outdated
|
||
if (!success) { | ||
return NextResponse.json( | ||
{ message: 'Rate limit exceeded', limit, remaining, reset }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include rate-limiting details in response headers (X-RateLimit-Limit
, X-RateLimit-Remaining
, X-RateLimit-Reset
), as clients may rely on headers instead of JSON for such information
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…ng details in response headers
…ng details in response headers
@akiraonstarknet lgtm, over to you |
Resolves #148
Rate Limiting Implementation
This pull request implements rate limiting for the API endpoints in the project using Upstash Redis. The implementation ensures that users are limited in the number of requests they can make to the API within a specified time frame.
Files and Folders Created
src/middleware.ts
:src/lib/redis.ts
:src/scripts/test-rate-limit.ts
:src/types/redis.d.ts
Rate Limit Configuration
This means that each IP address is allowed to make up to 20 requests within a 10-second window. After exceeding this limit, further requests will receive a 429 status until the window resets.
Testing the Implementation Locally
Set Up Environment Variables:
Create a
.env.local
file in the root of the project and add the following variables:Run the Development Server:
Start the local development server:
Test the Rate Limiting:
Run the rate limit test script to verify the implementation:
Live Demo
Result
Finally
This implementation provides a robust rate limiting solution for the API, ensuring fair usage and preventing abuse. Please review the changes and let me know if you have any questions or feedback.
This implementation works both in Development and Production. Check your console on production.
Checklist before requesting a review