Skip to content
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

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open

Conversation

Benjtalkshow
Copy link
Contributor

@Benjtalkshow Benjtalkshow commented Dec 12, 2024

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:

    • This file contains the rate limiting middleware that checks the number of requests made by each IP address and enforces the rate limit.
  • src/lib/redis.ts:

    • This file sets up the Redis client using Upstash Redis, allowing the middleware to store and retrieve rate limit data.
  • src/scripts/test-rate-limit.ts:

    • This script is used to test the rate limiting implementation locally by making multiple requests to the API endpoints.
  • src/types/redis.d.ts

Rate Limit Configuration

  • Rate Limit: 20 requests
  • Interval: 10 seconds

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

  1. Set Up Environment Variables:
    Create a .env.local file in the root of the project and add the following variables:

    UPSTASH_REDIS_REST_URL=https://your-upstash-redis-url
    UPSTASH_REDIS_REST_TOKEN=your-upstash-redis-token
    # Rate limiting configuration
    RATE_LIMIT_REQUESTS=50
    RATE_LIMIT_WINDOW=60 s
    
  2. Run the Development Server:
    Start the local development server:

    yarn dev
  3. Test the Rate Limiting:
    Run the rate limit test script to verify the implementation:

    yarn ts-node --project tsconfig.server.json src/scripts/test-rate-limit.ts

Live Demo

Result

Screenshot 2024-12-17 at 2 29 58 PM

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

  • I have performed a self-review of my code
  • I assure there is no similar/duplicate pull request regarding same issue
  • My PR passes all checks (build, lint, formatting, etc)

@Benjtalkshow
Copy link
Contributor Author

@akiraonstarknet
Please review

@EjembiEmmanuel
Copy link
Collaborator

@Benjtalkshow please deploy on vercel and include the deployment link in the description


const ratelimit = new Ratelimit({
redis,
limiter: Ratelimit.slidingWindow(20, '10 s'),
Copy link
Collaborator

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.


if (!success) {
return NextResponse.json(
{ message: 'Rate limit exceeded', limit, remaining, reset },
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EjembiEmmanuel
Copy link
Collaborator

@akiraonstarknet lgtm, over to you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: Add rate limit to public APIs
2 participants