You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::time::{Duration, Instant};
use tower::{limit::rate::*, util::*};
#[tokio::main]
async fn main() {
async fn foo(duration: Duration) -> Result<Instant, ()> {
tokio::time::sleep(duration).await;
Ok(Instant::now())
}
let svc = service_fn(foo);
let rate = Rate::new(1, Duration::from_secs(1));
let ref mut svc = RateLimit::new(svc, rate);
let x = svc.oneshot(Duration::from_secs_f64(1.1)).await.unwrap();
let y = svc.oneshot(Duration::from_secs_f32(0.1)).await.unwrap();
println!("{}", (y - x).as_secs_f32());
}
prints ~0.1 because the rate limit is calculated over calls to the service rather than inflight requests. This is in contrast to ConcurrencyLimit whose Service::Future holds a semaphore permit until the request is complete.
I'd argue this behavior is unexpected - imagine tokio::time::sleep here represents some network jitter and the Rate variables correspond to a rate limit enforced by some server, under the current design using RateLimit<Client> can cause the server to receive twice the allowed requests in an interval.
The text was updated successfully, but these errors were encountered:
The following code
prints ~
0.1
because the rate limit is calculated over calls to the service rather than inflight requests. This is in contrast toConcurrencyLimit
whoseService::Future
holds a semaphore permit until the request is complete.I'd argue this behavior is unexpected - imagine
tokio::time::sleep
here represents some network jitter and theRate
variables correspond to a rate limit enforced by some server, under the current design usingRateLimit<Client>
can cause the server to receive twice the allowed requests in an interval.The text was updated successfully, but these errors were encountered: