Comments (10)
Great idea
from ratelimit.
Perhaps it's our documentation that's unclear - if you do 5 per 5 minutes here you will effectively get a token every minute, rather than 5 every 5 minutes. Per is just a syntactic sugar to make the division simpler.
Ah I see, I was under the impression that your package differed from x/time/rate
in this manner. I'll look into making my own solution, sorry for the bother!
from ratelimit.
The feature on its own sounds reasonable, however but before any PRs we should discuss what exactly Allow
would do.
- does it just
peak
or alsopop
the available limit? Should it take some arguments likeAllow(count int)
Or should it perhaps beAllow(count int, at time.Time)
? - how does it work with
Take
? Do we allow both to be called, or do they work exclusively? - I'm not entirely sure whether you can squeeze it into the code easily, the implementation assumes "moving time"
Most important, have you considered just using https://pkg.go.dev/golang.org/x/time/rate#Limiter.Allow? This sounds like exactly what you need, and I don't see value in copying the the same functionality/API here.
from ratelimit.
In regards to how the function would work, I envisaged it peeking the limit, and providing both a count and time as paramters would be ideal. It shouldn't affect the functionality of take, and both should be able to work in harmony.
I have looked into using the Allow method from time/rate
, however the issue I experienced was that there is no Per
option like in this package. This makes rates such as '5 per 5 minutes' impossible, because the bucket refills at a rate proportional to the limit.
If you think that implementing this functionality here wouldn't be worthwhile/it would be more suitable to request an implementation of Per
in time/rate
I would be happy to go down that route instead 😄
from ratelimit.
I have looked into using the Allow method from time/rate, however the issue I experienced was that there is no Per option like in this package. This makes rates such as '5 per 5 minutes' impossible, because the bucket refills at a rate proportional to the limit.
Hm, I'm now not sure if I understand x/time/rate
correctly. Per https://pkg.go.dev/golang.org/x/time/rate#Limit we see
Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.
If I read this correctly, you could provide a very small float (0.016), that would effectively be the same as our Per
option. As far as I understand, buckets refill in the same way here and in x/time/rate
(?).
from ratelimit.
Perhaps it's our documentation that's unclear - if you do 5 per 5 minutes
here you will effectively get a token every minute, rather than 5 every 5 minutes. Per
is just a syntactic sugar to make the division simpler.
from ratelimit.
Out of curiosity, what's the functionality you're looking for?
from ratelimit.
A token bucket system where the refill rate can be entirely customised - for example you could create a new limiter with NewLimiter(5, 5 * time.Minute)
and this would refill the bucket to 5 after the 5 minute interval.
The initial requirement came from limiting an endpoint to 5 requests every 5 minutes, which doesn't seem possible with any current offering because this duration is simplified into '1 per minute up to a maximum of 5'.
from ratelimit.
Yeah, but could you bubble up a bit on the stack? What use-case requires this kind of rate-limiting?
The initial requirement came from limiting an endpoint to 5 requests every 5 minutes, which doesn't seem possible with any current offering because this duration is simplified into '1 per minute up to a maximum of 5'.
So that's not entirely precise. With Slack
, if you don't make requests for 5 minutes, the system will allow you to make 5 calls immediately. So you get the bustiness - if that's the requirement. After that it will be 1 per minute
though, yeah.
from ratelimit.
What use-case requires this kind of rate-limiting?
The requirement came from the desire to limit file uploads to a strict limit per 5-minute period. With slack/burst they'd be able to upload 5 files immediately, followed by another 5 across the next 5 minute period (because the bucket would refill at a rate of 1 per minute), which was not ideal.
As it seems most packages act like this, I'll probably end up just using this functionality. Thanks for the help!
from ratelimit.
Related Issues (20)
- X-Rate-Limit
- this line of doc is wrong
- When one of my operations has not called the atomicInt64Limiter.task() function for a long time, the speed limit may fail to be called again HOT 1
- change limit after start HOT 1
- Release all recent changes with a new v0.3.0 tag. HOT 1
- Unexpected results on CI with higher loop iterations
- This is rate shaping, not limiting HOT 1
- Take always return without block if it enters the case branch HOT 3
- [Docs] Need to illustrate the option `Slack` HOT 6
- The "slack" option doesn't make effect when use newAtomicInt64Based version rate limit
- Behavior of take in goroutine HOT 1
- Supply clock interface for testing? HOT 2
- context.Context support for Take? HOT 7
- If add some check in New function?
- Support for increasing/decreasing limits on the fly HOT 5
- What's the major diff v.s. https://pkg.go.dev/golang.org/x/time/rate HOT 1
- Why does the example_test fail when I run it locally? HOT 2
- atomicInt64Limiter WithoutSlack doesn't block HOT 18
- Can you support the take method of non blocking sleep
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.