upstash / ratelimit-js

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

Get reset time with a function #111

Closed someguy9 closed 2 months ago

someguy9 commented 3 months ago

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.

CahidArda commented 2 months ago

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.

someguy9 commented 2 months ago

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

CahidArda commented 2 months ago

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

CahidArda commented 2 months ago

Hi @someguy9,

we released v2.0.0 which includes this change.