Comments (7)
Ping!
Just found this library and it has helping me doing some basic ratelimiting, so thank you for that :)
I am feeling some other features are needed though here, context cancelation being one of them (if there is another lib or fork I should look at, please let me know!). If you are so inclined I can open other issues for features I think it would be nice to have
Regardless, for context cancelation I am currently resorting to this (used within an httpClient impl)
func (c *httpClient) applyRateLimit(ctx context.Context) error {
// the following goroutine + select allow us to abort waiting on
// rate limiting if the request context is done
limitingDone := make(chan struct{})
go func() {
c.limiter.Take()
close(limitingDone)
}()
select {
case <-limitingDone:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
from ratelimit.
For my future self - #11 tried to do the same. Some code has changed, but the comments there are valuable.
I don't see why we wouldn't extend the API to allow cancellation.
from ratelimit.
The API extension sounds reasonable, but this is not very high in my priority list - it'll likely take weeks if not months. PRs are welcome though :)
Happy to get more feature requests/issues - we can at least track them. But like above, I don't expect to be able to address them soon.
from ratelimit.
I can take on this issue. However, I'm not sure how Take()
should behave.
I know that the signature could be TakeCtx(ctx context.Context) (time.Time, error)
. I'm unsure if I can just exit the function with an error or if I have to rollback the rate limit state since it technically errored out.
from ratelimit.
Also, why do we have 3 different implementations of Limiter? Do I update all of them? It appears that only one is used.
from ratelimit.
I would recommend using https://pkg.go.dev/golang.org/x/time/rate#Limiter.Wait if it fits your requirements instead.
Given recent issues with testing & complexity of the questions you listed above, we're likely to stay with the current minimal API.
from ratelimit.
I would recommend using https://pkg.go.dev/golang.org/x/time/rate#Limiter.Wait if it fits your requirements instead.
This is what I ended up using. I just forgot it existed.
It's still weird to me to use a blocking API without context support. That's just me, though.
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
- 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
- Adding support for an `Allow` method HOT 10
- 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.