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

Add rate limiter on all APIs #10 #28

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Targter
Copy link

@Targter Targter commented Oct 13, 2024

Issue #10 : Add Rate Limiter on All APIs

### Objective:
Implement a rate limiter on all API endpoints to control the number of requests a client can make in a given time frame.

### Details:

Rate Limiting Library:
Utilize the express-rate-limit middleware for implementing rate limiting in the Express application.

Rate Limit Configuration:
Set a limit of 5 requests per second for each client to prevent abuse and ensure fair usage of the API.

Response for Exceeding Limit:
Configure the response behavior when the limit is exceeded:
Send a response with a message indicating the client should "Try again later" to inform users of the rate limit.

### Implementation Steps:

  • Install the express-rate-limit package via npm:
  • npm install express-rate-limit
  • Import the library in your main application file.
  • Create a rate limiter middleware using the following configuration:
  • app.use(limiter);

Screenshots

WhatsApp Image 2024-10-13 at 17 44 31_60da0861

In this image, only 5 requests are sent successfully; subsequent requests receive a 429 error indicating 'Too Many Requests

WhatsApp Image 2024-10-13 at 17 45 08_46fa0140

This image shows the received requests in our backend, highlighting that only 5 requests are processed successfully, while subsequent requests result in a 429 error indicating 'Too Many Requests.

Testing:

  • Ensure to test the implementation thoroughly by attempting to exceed the request limit and confirming that the appropriate response is received.

Benefits:

Helps maintain API performance by preventing excessive requests from any single user.
Enhances the overall user experience by providing clear feedback when limits are exceeded.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

You've made your first pull request - awesome! Let's collaborate to make this project even better

If you're fixing a bug, please refer to the issue number in the description.

If you are implementing a feature request, please check with the maintainers that the feature will be accepted first.

Comment on lines +17 to +39
const limiter = rateLimit({
windowMs: 1000, // 1 second window
max: 5, // limit each IP to 5 requests per windowMs (adjust this as needed)
message: "Too many requests from this IP, please try again after a second",
headers: true, // Sends rate limit info in response headers
handler: (req, res) => {
// Custom error response
return res.status(429).json({
error: "Rate limit exceeded",
message:
"You have exceeded the number of allowed requests. Please try again later.",
});
},
});
// apply limit to all request
app.use(limiter);

// log the changes
// app.use((req, res, next) => {
// console.log(`[${new Date().toISOString()}] ${req.method} ${req.originalUrl}`);
// next();
// });

Copy link
Member

Choose a reason for hiding this comment

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

Don't do this directly.
Instead create a new RateLimiter class with multiple configurations like algorithms, rate limit and so on.
So we can apply different rate limit and algorithm on different APIs.

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.

2 participants