Comments (18)
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.
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.
+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.
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.
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.
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.
we have recently added ratelimit analytics, does that solve your problem?
from ratelimit.
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.
I see
the tricky thing is that it's different for each algorithm, which one are you using?
from ratelimit.
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.
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.
Ah sorry, yes you are correct.
It returns the used amount in the current window
from ratelimit.
Very cool, I think this would be nice to be documented. Shall I make a PR?
from ratelimit.
that'd be great, thanks
from ratelimit.
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:
ratelimit/packages/sdk/src/single.ts
Lines 158 to 160 in c509628
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.
from ratelimit.
yeah that looks like an oversight
it can return negative, but the check if it may pass also accounts for that
from ratelimit.
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.
This issue was closed because it has been stalled for 30 days with no activity.
from ratelimit.
Related Issues (20)
- Version 1.1.2 throws errors HOT 3
- Is there a way to fetch the remaining requests for a given key HOT 5
- Ratelimit.tokenBucket default tokens value HOT 3
- Dynamic token cost on Ratelimit.tokenBucket algorithm HOT 3
- Ratelimit doesn't seem to return proper reset time HOT 4
- Ratelimit usage with ioredis HOT 4
- Question about rate limit HOT 1
- SlidingWindow dose not work properly in the latest release HOT 1
- Rate Limiter takes 5s to complete API call HOT 5
- blockUntilReady and cf workers HOT 8
- Limiting Concurrency HOT 9
- custom rates support HOT 21
- Upstash Ratelimit Error in GH Action HOT 8
- cf workers: TypeError: Cannot read properties of undefined (reading 'eval') HOT 7
- Support getting and resetting limit of a specific identifier HOT 11
- Self-hosted Redis use? HOT 3
- How to set one hour limit for sliding window? HOT 2
- Awaiting `pending` promise is required in Lambda HOT 1
- Enabling analytics leads to timeouts in response-streaming AWS Lambda HOT 7
- No TTL set for slidingWindow rate limit keys on latest version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ratelimit.