Git Product home page Git Product logo

Comments (18)

chronark avatar chronark commented on May 21, 2024 1

ah ok that one is fairly trivial

const intervalDuration = 1000 // 1s - change this to your setting
const prefix = "@upstash/ratelimit"
const key = [prefix, identifier, Math.floor(Date.now() / intervalDuration)].join(":")

const remaining = await redis.get<number>(key)

from ratelimit.

chronark avatar chronark commented on May 21, 2024

That variable number is actually just the timestamp of the current window
For example in fixedWindow you could simply recreate the key like this:

const bucket = Math.floor(Date.now() / windowDuration);
const key = [identifier, bucket].join(":");

const counter = await redis.get(key)

windowDuration is the number of milliseconds in your chosen window

Sounds useful, I'll add it to my backlog

from ratelimit.

AtotheY avatar AtotheY commented on May 21, 2024

+1 on this request - having a limiter.checkLimit(key); would be really helpful - otherwise the only way for users to know how many requests they have left is to actually use a credit

from ratelimit.

AtotheY avatar AtotheY commented on May 21, 2024

Also wondering if there is a timeline for this - I know you mentioned it was backlog so just curious if it's coming anytime soon 👀 👍

from ratelimit.

chronark avatar chronark commented on May 21, 2024

Damn, I didn't see your responses, sorry about that
@AtotheY @jamesward1
I'm working on some improvements and will ship something for this as well: tomorrow or next week.

from ratelimit.

Than-DE avatar Than-DE commented on May 21, 2024

Hi @chronark just wondering if this was still in the backlog. I am trying to look into how the increment events are being counted on redis but I can't seem to figure out how I would implement such a checkLimit() feature. 🙏

from ratelimit.

chronark avatar chronark commented on May 21, 2024

we have recently added ratelimit analytics, does that solve your problem?

from ratelimit.

Than-DE avatar Than-DE commented on May 21, 2024

Ah not really, unfortunately @chronark. What I'd need is a way to check for a given identifier what is the remaining limit for the current window (fixed in my case). The same way remaining is returned by the .limit() method.

This is useful so that the user can see their remaining limit without having to actually make another request. I am using this rate limiting for an expensive OpenAI API call and I'm only allowing X requests per day.

The node-rate-limiter has a getRemainingTokens() method that does exactly what I need, but since I am using a serverless function, that package is not an option 😢

from ratelimit.

chronark avatar chronark commented on May 21, 2024

I see
the tricky thing is that it's different for each algorithm, which one are you using?

from ratelimit.

Than-DE avatar Than-DE commented on May 21, 2024

I am using the fixed window. I assume it is difficult because neither the identifier string entry nor the event hash in redis actually tells you how many tokens were consumed at what time?

from ratelimit.

Than-DE avatar Than-DE commented on May 21, 2024

Thanks, that is good to hear. I did try this but have a mismatch between what .limit() returns and what this calculation returns. Do you think it has something to do with the fact that I am testing this in the middle of an ongoing window and by tomorrow (my window is 1 day = 1000 * 60 * 60 * 24) these two values will sync?

Edit: Actually - I think this calculation does not return the remaining but the already consumed tokens, meaning the value of this should be subtracted from the maximum tokens specified in the limiter configuration. Am I understanding this correctly?

from ratelimit.

chronark avatar chronark commented on May 21, 2024

Ah sorry, yes you are correct.
It returns the used amount in the current window

from ratelimit.

Than-DE avatar Than-DE commented on May 21, 2024

Very cool, I think this would be nice to be documented. Shall I make a PR?

from ratelimit.

chronark avatar chronark commented on May 21, 2024

that'd be great, thanks

from ratelimit.

Than-DE avatar Than-DE commented on May 21, 2024

Revisiting this because I'm not 100% sure if this remaining token calculation for the fixed window really works. I checked the .limit() method and as far as I understand, it increments the redis key first before checking whether to set success to true:

const usedTokensAfterUpdate = (await ctx.redis.eval(script, [key], [windowDuration])) as number;
const success = usedTokensAfterUpdate <= tokens;

This means that when I call .limit() and check the returned remaining number it can also return a negative remaining value. For some reason, this doesn't happen every time, though.

CleanShot 2023-06-19 at 19 39 55

from ratelimit.

chronark avatar chronark commented on May 21, 2024

yeah that looks like an oversight
it can return negative, but the check if it may pass also accounts for that

from ratelimit.

github-actions avatar github-actions commented on May 21, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

from ratelimit.

github-actions avatar github-actions commented on May 21, 2024

This issue was closed because it has been stalled for 30 days with no activity.

from ratelimit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.