upstash/ratelimit-js

Get reset time with a function

someguy9 opened this issue · 4 comments

Would love to see ratelimit.getRemaining also return the reset time (like limit does) or a dedicated ratelimit.getReset function. The issue I am having is that .limit always adds 1 to the limit even if { rate: 0 } is set. So there is no way to retrieve the time at which the limit will be reset without triggering a use.

Hi @someguy9,

I think having a getReset method makes sense. I will work on adding it.

Using .limit with rate: 0 shouldn't have reduced the rate. Which rate limiting algorithm are you using? That should be fixed aswell.

I noticed the issue when using tokenBucket. And great! can't wait to see this stuff fixed

I looked into adding reset to the result of getRemaining here: feat: add reset field to getRemaining.

Here is the planned getRemaining method:

public getRemaining = async (identifier: string): Promise<{
  remaining: number;
  reset: number;
}> => {

It is backwards incompatible but I think it's a simple change. I will discuss it with the team.

I didn't wan't to make a seperate getReset method because it would need to make similar requests to redis as getRemaining. reset values of fixedWindow/slidingWindow algorithms can be calculated without calling redis but I feel like that reset information is not very valueable without remaining field

Hi @someguy9,

we released v2.0.0 which includes this change.