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 Strategies #500

Closed
theduke opened this issue Apr 21, 2021 · 4 comments · Fixed by #724
Closed

Rate Limiting Strategies #500

theduke opened this issue Apr 21, 2021 · 4 comments · Fixed by #724
Labels
client kube Client related

Comments

@theduke
Copy link

theduke commented Apr 21, 2021

The official Go client provides rate limiters to prevent spamming the API with requests.

See https://pkg.go.dev/k8s.io/[email protected]/util/workqueue

It would be nice to get similar functionality in this project.

Useful would be:

  • Global rate limiter for the whole client
  • Api<T> specific rate limiter
  • Pluggable rate limiting strategies, like in the Go client (exponential backoff, etc)
@MikailBag
Copy link
Contributor

I think kube should somehow reuse existing rate limit implementations from the Tower ecosystem.

In that case, this issue looks like "ability to inject arbitrary Services on client or Api level".

@clux clux added the client kube Client related label Apr 22, 2021
@kazk
Copy link
Member

kazk commented Jun 6, 2021

Custom client with layers is now supported. You can do this for global:

use kube::{client::ConfigExt, Client, Config};
use tower::{limit::RateLimitLayer, ServiceBuilder};

let config = Config::infer().await?;
let https = config.native_tls_https_connector()?;
let client = Client::new(
    ServiceBuilder::new()
        .layer(config.base_uri_layer())
        .layer(RateLimitLayer::new(num, duration))
        .service(hyper::Client::builder().build(https))
);

@theduke
Copy link
Author

theduke commented Jun 6, 2021

Awesome, thanks!

@clux clux changed the title Rate Limiter Rate Limiting Strategies Nov 3, 2021
@clux
Copy link
Member

clux commented Nov 3, 2021

Summary of status after going through issues a bit.

    1. Global rate limiter strategy above
    1. Invidual api calls (something that glues backoff with futures - like tokio-retry)
    1. workqueue based system on the Api type or better ways (by doing multiple manual configs from 1)

For 2; It would be nice to have some examples showcasing individual method retries here. Also plays into #34

Not sure we should do more on the last one here. It would be complicated. This could be improved in the RateLimitLayer in tower by making it configurable based on paths, or by injecting an optional rate limiter deep within Api, or by thinking about if we could clone the Client in such a way that we could customize the layers per copy. (Pretty sure that if we instantiate two instances of a Custom client, you'd be able to get different rate limit layers on each of them. But it's not designed for that). Anyway, investigative work here would be appreciated.

But for now going to limit the scope of this ticket to just get examples that show how to do this:

@clux clux added the help wanted Not immediately prioritised, please help! label Nov 3, 2021
@clux clux added this to Kube Roadmap Nov 3, 2021
@clux clux moved this to Defining in Kube Roadmap Nov 3, 2021
@clux clux added help wanted Not immediately prioritised, please help! and removed help wanted Not immediately prioritised, please help! labels Nov 20, 2021
clux added a commit that referenced this issue Nov 20, 2021
using custom-client-trace example - for #500

Signed-off-by: clux <[email protected]>
@clux clux linked a pull request Nov 20, 2021 that will close this issue
@clux clux closed this as completed in #724 Nov 21, 2021
clux added a commit that referenced this issue Nov 21, 2021
* showcase rate limiting strategies in example

using custom-client-trace example - for #500

Signed-off-by: clux <[email protected]>

* fmt

Signed-off-by: clux <[email protected]>
Repository owner moved this from Defining to Done in Kube Roadmap Nov 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client kube Client related
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants