public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Shan Shaji <s.shaji@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox 9/9] proxmox-log: add LogThrottle implementation
Date: Mon, 19 Oct 2026 18:03:59 +0200	[thread overview]
Message-ID: <20261019160359.20851-10-s.shaji@proxmox.com> (raw)
In-Reply-To: <20261019160359.20851-1-s.shaji@proxmox.com>

factor out LogThrottle implementation from pbs-client to proxmox-log
crate.

Signed-off-by: Shan Shaji <s.shaji@proxmox.com>
---
 proxmox-log/src/lib.rs          |  1 +
 proxmox-log/src/log_throttle.rs | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
 create mode 100644 proxmox-log/src/log_throttle.rs

diff --git a/proxmox-log/src/lib.rs b/proxmox-log/src/lib.rs
index 2d321f20..e82ab258 100644
--- a/proxmox-log/src/lib.rs
+++ b/proxmox-log/src/lib.rs
@@ -14,6 +14,7 @@ mod pve_task_formatter;
 mod tasklog_layer;
 
 pub mod builder;
+pub mod log_throttle;
 pub use builder::Logger;
 pub use file_logger::{FileLogOptions, FileLogger};
 
diff --git a/proxmox-log/src/log_throttle.rs b/proxmox-log/src/log_throttle.rs
new file mode 100644
index 00000000..e88e116f
--- /dev/null
+++ b/proxmox-log/src/log_throttle.rs
@@ -0,0 +1,39 @@
+use std::time::{Duration, Instant};
+
+pub const INITIAL_INTERVAL: Duration = Duration::from_secs(5);
+pub const MAX_INTERVAL: Duration = Duration::from_secs(60);
+
+/// Rate-limits recurring log output, such as progress lines, with an exponential backoff.
+///
+/// [`should_log`](Self::should_log) returns `true` at most once per the current interval. Starting
+/// from an initial interval, the delay doubles after each hit up to a configured maximum, giving
+/// frequent feedback early on while avoiding log spam during long-running operations.
+pub struct LogThrottle {
+    last: Instant,
+    interval: Duration,
+    max_interval: Duration,
+}
+
+impl LogThrottle {
+    /// Creates a throttle with the given initial interval, doubling up to `max_interval` after each
+    /// hit. The initial interval is capped at `max_interval`.
+    pub fn new(initial_interval: Duration, max_interval: Duration) -> Self {
+        Self {
+            last: Instant::now(),
+            interval: initial_interval.min(max_interval),
+            max_interval,
+        }
+    }
+
+    /// Returns `true` if the current interval has elapsed since the last hit, advancing the
+    /// throttle and doubling the interval (capped at the maximum); returns `false` otherwise.
+    pub fn should_log(&mut self) -> bool {
+        let now = Instant::now();
+        if now.duration_since(self.last) < self.interval {
+            return false;
+        }
+        self.last = now;
+        self.interval = (self.interval * 2).min(self.max_interval);
+        true
+    }
+}
-- 
2.47.3





      parent reply	other threads:[~2026-10-19 16:04 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 16:03 [PATCH proxmox{-backup,} 0/9] fix #7903: rebuild local S3 chunk markers Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 1/9] refactor: remove local `LogThrottle` in favor of proxmox-log Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 2/9] datastore: factor out s3 chunk objects pagination into a helper Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 3/9] pbs-datastore: inherit proxmox-log crate from workspace Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 4/9] datastore: add method to create zero-byte chunk markers Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 5/9] fix #7908: datastore: api: add option " Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 6/9] fix #7908: datastore: ui: " Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 7/9] fix #7908: backup-manager: " Shan Shaji
2026-10-19 16:03 ` [PATCH proxmox-backup 8/9] docs: add explanation regarding ``rebuild-chunk-markers`` option Shan Shaji
2026-10-19 16:03 ` Shan Shaji [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261019160359.20851-10-s.shaji@proxmox.com \
    --to=s.shaji@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal