From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9FEFD7D5D0 for ; Tue, 9 Nov 2021 07:53:39 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id C8DD12E155 for ; Tue, 9 Nov 2021 07:53:07 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 8590F2DF68 for ; Tue, 9 Nov 2021 07:53:01 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6BE1E42775; Tue, 9 Nov 2021 07:52:56 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Tue, 9 Nov 2021 07:52:45 +0100 Message-Id: <20211109065253.980304-9-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211109065253.980304-1-dietmar@proxmox.com> References: <20211109065253.980304-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.510 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox 4/7] RateLimitedStream: allow periodic limiter updates X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Nov 2021 06:53:39 -0000 --- .../src/client/rate_limited_stream.rs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/proxmox-http/src/client/rate_limited_stream.rs b/proxmox-http/src/client/rate_limited_stream.rs index 00ba066..ea99383 100644 --- a/proxmox-http/src/client/rate_limited_stream.rs +++ b/proxmox-http/src/client/rate_limited_stream.rs @@ -19,6 +19,8 @@ pub struct RateLimitedStream { read_delay: Option>>, write_limiter: Option>>, write_delay: Option>>, + update_limiter_cb: Option (Option>>, Option>>) + Send>>, + last_limiter_update: Instant, stream: S, } @@ -43,9 +45,44 @@ impl RateLimitedStream { read_delay: None, write_limiter, write_delay: None, + update_limiter_cb: None, + last_limiter_update: Instant::now(), stream, } } + + /// Creates a new instance with limiter update callback. + /// + /// The fuction is called every minute to update/change the used limiters. + /// + /// Note: This function is called within an async context, so it + /// should be fast and must not block. + pub fn with_limiter_update_cb (Option>>, Option>>) + Send + 'static>( + stream: S, + update_limiter_cb: F, + ) -> Self { + let (read_limiter, write_limiter) = update_limiter_cb(); + Self { + read_limiter, + read_delay: None, + write_limiter, + write_delay: None, + update_limiter_cb: Some(Box::new(update_limiter_cb)), + last_limiter_update: Instant::now(), + stream, + } + } + + fn update_limiters(&mut self) { + if let Some(ref update_limiter_cb) = self.update_limiter_cb { + if self.last_limiter_update.elapsed().as_secs() >= 5 { + self.last_limiter_update = Instant::now(); + let (read_limiter, write_limiter) = update_limiter_cb(); + self.read_limiter = read_limiter; + self.write_limiter = write_limiter; + } + } + } } fn register_traffic( @@ -90,6 +127,8 @@ impl AsyncWrite for RateLimitedStream { this.write_delay = None; + this.update_limiters(); + let result = Pin::new(&mut this.stream).poll_write(ctx, buf); if let Some(ref limiter) = this.write_limiter { @@ -118,6 +157,8 @@ impl AsyncWrite for RateLimitedStream { this.write_delay = None; + this.update_limiters(); + let result = Pin::new(&mut this.stream).poll_write_vectored(ctx, bufs); if let Some(ref limiter) = this.write_limiter { @@ -161,6 +202,8 @@ impl AsyncRead for RateLimitedStream { this.read_delay = None; + this.update_limiters(); + let filled_len = buf.filled().len(); let result = Pin::new(&mut this.stream).poll_read(ctx, buf); -- 2.30.2