Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate limiting outgoing requests #256

Closed
janekolszak opened this issue Jul 18, 2019 · 10 comments
Closed

Rate limiting outgoing requests #256

janekolszak opened this issue Jul 18, 2019 · 10 comments

Comments

@janekolszak
Copy link

Hi!
Thank you so much for this project, it saved me a lot of time!

Do you plan on adding a per-endpoint rate limiter, or a global rate-limiter of outgoing requests?

Thanks

@jeevatkm
Copy link
Member

@janekolszak Thank you for your appreciation and suggesting a new feature in Resty as client-side rate-limiting. It will be a good addition.

Typically server-side rate-limiting is known for, it would be good to try out on client-side too. If you have a design suggestion or recommendation. Please post here on the thread.

Currently, I'm bit occupied in at my day work. I will get to it once I get time. Thanks!

@jeevatkm jeevatkm added feature v2 For resty v2 labels Jul 20, 2019
@janekolszak
Copy link
Author

One options is to use github.com/didip/tollbooth since it 's already battle tested and simple.

Create the limiter with something like this

limiter = tollbooth.NewLimiter(ratePerSecond, &limiter.ExpirableOptions{
	DefaultExpirationTTL: ....,
	ExpireJobInterval:    ....,
})
limiter.SetBurst(int(math.Max(1, float64(burstSize))))

When making the request just use

if limiter.LimitReached(<some key>) {
// error
}

For a global, per-client limiter would be a const string. But one could also use per remote endpoint limiting.

This would be an overkill if we only need a global per-client limiter. It'd introduce a separate goroutine for caching limited keys.

@janekolszak
Copy link
Author

I think that the best option is to use https://godoc.org/golang.org/x/time/rate and just create one limiter per resty client. When making the request we'd just run limiter.Wait(ctx) this way we could achieve a useful feature - constant rate of outgoing requests.

Let me know if you want me to write it.

@jeevatkm jeevatkm removed the v2 For resty v2 label Sep 12, 2021
@lavoiesl
Copy link
Contributor

I am also interested in having rate limiting.

However, to avoid locking into one particular interface, I suggest adding the ability to wrap executors and let users add limiters how they see fit: #477

@jeevatkm
Copy link
Member

@lavoiesl Thanks for showing your interest, I agreed about not locking into one particular interface.

@dbriemann
Copy link
Contributor

Hi, I needed client side rate-limiting for a personal project so I did a short implementation based on golang.org/x/time/rate as mentioned by @janekolszak .

I created a pull request: #494

If you think this would be useful for Resty don't hesitate to ask for changes.

@alarbada
Copy link

Hello there

I implemented my own rate limiter with httpTransport and friends, but I wonder if the rate limiting affects the timeout of the request, because I am getting a lot of those errors on my project, where I do thousands of requests per sec.

@morningvera
Copy link

hey @jeevatkm, i'm also interested in client side rate limiting! any chance you'd be open to a PR for it? or could you take a look at the PR above?

@jeevatkm
Copy link
Member

jeevatkm commented Oct 1, 2023

@dbriemann @SVilgelm have contributed this feature. It has been released in v2.9.0

@jeevatkm jeevatkm closed this as completed Oct 1, 2023
@mariusgrigoriu
Copy link

mariusgrigoriu commented May 9, 2024

Is the intention to make callers wait for the rate limit? It would be more useful if resty did the waiting instead of immediately throwing an error. Having to add waiting on the rate limiter in my caller means I don't need to set a rate limiter in resty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

7 participants