From b9a1d62e47d5e70a5aaa74d179f9484db5f18919 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 14 Nov 2021 08:15:42 +0100 Subject: [PATCH] proxmox-http: RateLimit - remove average_rate Instead, add a method to return overall traffic. --- proxmox-http/src/client/rate_limiter.rs | 28 +++++++++++-------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/proxmox-http/src/client/rate_limiter.rs b/proxmox-http/src/client/rate_limiter.rs index f3af7704..bc83dd8c 100644 --- a/proxmox-http/src/client/rate_limiter.rs +++ b/proxmox-http/src/client/rate_limiter.rs @@ -6,8 +6,8 @@ pub trait RateLimit { /// Update rate and bucket size fn update_rate(&mut self, rate: u64, bucket_size: u64); - /// Returns the average rate (since `start_time`) - fn average_rate(&self, current_time: Instant) -> f64; + /// Returns the overall traffic (since started) + fn traffic(&self) -> u64; /// Register traffic, returning a proposed delay to reach the /// expected rate. @@ -19,17 +19,19 @@ pub trait RateLimit { /// This is useful for types providing internal mutability (Mutex). pub trait ShareableRateLimit: Send + Sync { fn update_rate(&self, rate: u64, bucket_size: u64); - fn average_rate(&self, current_time: Instant) -> f64; + fn traffic(&self) -> u64; fn register_traffic(&self, current_time: Instant, data_len: u64) -> Duration; } /// Token bucket based rate limiter -#[repr(C)] // So that we can use it in shared memory +/// +/// IMPORTANT: We use this struct in shared memory, so please do not +/// change/modify the layout (do not add fields) +#[repr(C)] pub struct RateLimiter { rate: u64, // tokens/second - start_time: Instant, + bucket_size: u64, // TBF bucket size traffic: u64, // overall traffic - bucket_size: u64, last_update: Instant, consumed_tokens: u64, } @@ -48,7 +50,6 @@ impl RateLimiter { pub fn with_start_time(rate: u64, bucket_size: u64, start_time: Instant) -> Self { Self { rate, - start_time, traffic: 0, bucket_size, last_update: start_time, @@ -89,13 +90,8 @@ impl RateLimit for RateLimiter { self.bucket_size = bucket_size; } - fn average_rate(&self, current_time: Instant) -> f64 { - let time_diff = current_time.saturating_duration_since(self.start_time).as_secs_f64(); - if time_diff <= 0.0 { - 0.0 - } else { - (self.traffic as f64) / time_diff - } + fn traffic(&self) -> u64 { + self.traffic } fn register_traffic(&mut self, current_time: Instant, data_len: u64) -> Duration { @@ -117,8 +113,8 @@ impl ShareableRateLimit for std::sync::Mutex { self.lock().unwrap().update_rate(rate, bucket_size); } - fn average_rate(&self, current_time: Instant) -> f64 { - self.lock().unwrap().average_rate(current_time) + fn traffic(&self) -> u64 { + self.lock().unwrap().traffic() } fn register_traffic(&self, current_time: Instant, data_len: u64) -> Duration {