Open
Conversation
Contributor
Author
|
@akiraonstarknet |
Collaborator
|
@Benjtalkshow please deploy on vercel and include the deployment link in the description |
EjembiEmmanuel
requested changes
Dec 16, 2024
src/middleware.ts
Outdated
|
|
||
| const ratelimit = new Ratelimit({ | ||
| redis, | ||
| limiter: Ratelimit.slidingWindow(20, '10 s'), |
Collaborator
There was a problem hiding this comment.
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 }, |
Collaborator
There was a problem hiding this comment.
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
Contributor
Author
There was a problem hiding this comment.
…ng details in response headers
…ng details in response headers
EjembiEmmanuel
approved these changes
Dec 19, 2024
Collaborator
|
@akiraonstarknet lgtm, over to you |
Contributor
Author
|
@akiraonstarknet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.tsRate 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.localfile 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