-
Notifications
You must be signed in to change notification settings - Fork 62
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
Return accurate reset time for each limited call #44
Conversation
@noamshemesh any feedback on this? |
Sorry probably missed the previous mail. Just to make sure we're on the same page here, taking your example:
Unless I'm missing something, that's pretty weird. I wouldn't expect the remaining to be on one call bigger than a later call ( What do you think? |
@noamshemesh What I would like to have is a reset value computed using the configured limit to be accurate. It is illustrated by the test I added I couldn't find where this example come from but, with this PR, the limiter should look something like this:
As you can see, all limited requests (t0.2, t3.0, t3.1, t3.4) return a reset that depends on the oldest timestamps in the last 2 received requests. An alternative solution could be to use |
var count = parseInt(Array.isArray(res[0]) ? res[1][1] : res[1]); | ||
var oldest = parseInt(Array.isArray(res[0]) ? res[3][1] : res[3]); | ||
|
||
var isIoRedis = Array.isArray(res[0]); |
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.
oh, now I understood better why this check is necessary. I removed it from the promise equivalent package:
Making it a bit faster, thanks 🙂
(I will add this PR as well)
You're right. Merging. Thanks for the PR |
Since the rate limit strategy saves all the calls in Redis, the reset time returned needs to depend on the oldest call in the range of the limiter.
In practice, the oldest call to use to calculate the reset time is the max-th element from the end or the first element of the sorted set.
Closes #43.