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

Added rate limiter for all APIs #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Tiwariji-07
Copy link

@Tiwariji-07 Tiwariji-07 commented Oct 12, 2024

Closes #10

Using the token bucket algorithm, I have added the rate Limit to all the endpoints.

Modify these constants accordingly
` const REQUEST_LIMIT = 5;

// Time frame in milliseconds
const WINDOW_SIZE = 10000; `

Note: There are other approaches, like using Redis or a 3rd party library. Currently, this approach is memory efficient.

Improvement: We could limit the number of requests made to the server for each user individually using their IP address or user ID or token.

Screenshot 2024-10-12 at 8 43 10 PM

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 +1 to +34
// Rate Limiting using Token bucket algorithm

// Maximum number of requests allowed in a given time frame
const REQUEST_LIMIT = 5;

// Time frame in milliseconds
const WINDOW_SIZE = 10000;

// Initialize with REQUEST_LIMIT number of tokens
const tokens = Array(REQUEST_LIMIT).fill(1);

var time = new Date().getTime();

export const rateLimiter = (req, res, next) => {
if (tokens.length && new Date().getTime() - time < WINDOW_SIZE) {
tokens.pop();
next();
} else {
if (new Date().getTime() - time >= WINDOW_SIZE) {
const len = tokens.length;
for (let i = 0; i < 5 - len; i++) {
tokens.push(1);
}
time = new Date().getTime();
rateLimiter(req, res, next);
} else {
res.status(429).json({
message: `You have exceeded the ${REQUEST_LIMIT} requests in ${WINDOW_SIZE} ms limit !! , ${
new Date().getTime() - time
}`,
status: 429,
});
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use some library that provides a lot more functionality and use build that ?
Basically RateLimiter class with multiple configurations like algorithm, different rate limit, and so on

Copy link
Author

Choose a reason for hiding this comment

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

Understood the requirement. We can I guess. Let me work on it.

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.

Add rate limiter on all APIs
2 participants