upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-upstash.vercel.app/
MIT License
1.72k stars 33 forks source link

fix: token bucket refill logic #50

Closed TudorZgimbau closed 1 year ago

TudorZgimbau commented 1 year ago

The problem with the current approach is that by calling PEXPIRE with the refill interval, the refill rate will be ignore and always replaced with the maximum number of tokens - the current request.

Instead of that, I suggest that we allow the remaining requests to go below 0 (to be able to check if the request should pass or not). Then, if an interval has passed, we check the number of remaining tokens. If it its negative, we simply replace it with the refill rate, which is otherwise added to the remaining tokens.

vercel[bot] commented 1 year ago

@TudorZgimbau is attempting to deploy a commit to the Upstash Team on Vercel.

A member of the Team first needs to authorize it.

chronark commented 1 year ago

Closing this in favor of #53 then the CI can run with secrets