From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id CA9AF1FF09C for ; Mon, 19 Oct 2026 18:04:39 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 44EAA21603; Mon, 19 Oct 2026 18:04:37 +0200 (CEST) From: Shan Shaji 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 Message-ID: <20261019160359.20851-10-s.shaji@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261019160359.20851-1-s.shaji@proxmox.com> References: <20261019160359.20851-1-s.shaji@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1792425859174 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.392 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: EJ2YMIEA5YISNPJ62YNB2XKAA3ZYDTGD X-Message-ID-Hash: EJ2YMIEA5YISNPJ62YNB2XKAA3ZYDTGD X-MailFrom: s.shaji@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: factor out LogThrottle implementation from pbs-client to proxmox-log crate. Signed-off-by: Shan Shaji --- 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