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.
oh, now I understood better why this check is necessary. I removed it from the promise equivalent package:
https://github.com/tj/node-ratelimiter/pull/44/files#diff-168726dbe96b3ce427e7fedce31bb0bcR77
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.