]> git.proxmox.com Git - proxmox.git/commitdiff
RateLimiter: add update_rate method
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 8 Nov 2021 05:34:40 +0000 (06:34 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 10 Nov 2021 08:51:08 +0000 (09:51 +0100)
proxmox-http/src/client/rate_limiter.rs

index 677dfb1001979ca85d3bc164fa4854a0da751d4a..37362b1064ccb526eeefcbad4aa8f5c9af7f07aa 100644 (file)
@@ -34,6 +34,17 @@ impl RateLimiter {
         }
     }
 
+    /// Update rate and bucket size
+    pub fn update_rate(&mut self, rate: u64, bucket_size: u64) {
+        self.rate = rate;
+
+        if bucket_size < self.bucket_size && self.consumed_tokens > bucket_size {
+            self.consumed_tokens = bucket_size; // start again
+        }
+
+        self.bucket_size = bucket_size;
+    }
+
     /// Returns the average rate (since `start_time`)
     pub fn average_rate(&self, current_time: Instant) -> f64 {
         let time_diff = current_time.saturating_duration_since(self.start_time).as_secs_f64();