-
Notifications
You must be signed in to change notification settings - Fork 16
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
base: main
Are you sure you want to change the base?
Conversation
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.
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.
// 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, | ||
}); | ||
} | ||
} |
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.
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
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.
Understood the requirement. We can I guess. Let me work on it.
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.