[pbs-devel] [PATCH proxmox 5/7] RateLimiter: avoid panic in time computations

Dietmar Maurer dietmar at proxmox.com
Tue Nov 9 07:52:47 CET 2021


---
 proxmox-http/src/client/rate_limiter.rs | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/proxmox-http/src/client/rate_limiter.rs b/proxmox-http/src/client/rate_limiter.rs
index 4742387..677dfb1 100644
--- a/proxmox-http/src/client/rate_limiter.rs
+++ b/proxmox-http/src/client/rate_limiter.rs
@@ -36,7 +36,7 @@ impl RateLimiter {
 
     /// Returns the average rate (since `start_time`)
     pub fn average_rate(&self, current_time: Instant) -> f64 {
-        let time_diff = (current_time - self.start_time).as_secs_f64();
+        let time_diff = current_time.saturating_duration_since(self.start_time).as_secs_f64();
         if time_diff <= 0.0 {
             0.0
         } else {
@@ -45,12 +45,15 @@ impl RateLimiter {
     }
 
     fn refill_bucket(&mut self, current_time: Instant) {
-        let time_diff = (current_time - self.last_update).as_nanos();
+        let time_diff = match current_time.checked_duration_since(self.last_update) {
+            Some(duration) => duration.as_nanos(),
+            None => {
+                //log::error!("update_time: got negative time diff");
+                return;
+            }
+        };
 
-        if time_diff <= 0 {
-            //log::error!("update_time: got negative time diff");
-            return;
-        }
+        if time_diff == 0 { return; }
 
         self.last_update = current_time;
 
-- 
2.30.2






More information about the pbs-devel mailing list