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 81800629DD for ; Mon, 21 Dec 2020 14:56:25 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 76B77208D7 for ; Mon, 21 Dec 2020 14:56:25 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 08D98208BF for ; Mon, 21 Dec 2020 14:56:21 +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 C632B4558A for ; Mon, 21 Dec 2020 14:56:20 +0100 (CET) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Mon, 21 Dec 2020 14:56:10 +0100 Message-Id: <20201221135611.14456-2-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201221135611.14456-1-s.reiter@proxmox.com> References: <20201221135611.14456-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.033 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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 Subject: [pbs-devel] [RFC proxmox 1/2] add tools::future with TimeoutFutureExt 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: Mon, 21 Dec 2020 13:56:25 -0000 Implements shorthands to automatically cancel a long-running future after a timeout is reached. Signed-off-by: Stefan Reiter --- proxmox/src/tools/future.rs | 48 +++++++++++++++++++++++++++++++++++++ proxmox/src/tools/mod.rs | 1 + 2 files changed, 49 insertions(+) create mode 100644 proxmox/src/tools/future.rs diff --git a/proxmox/src/tools/future.rs b/proxmox/src/tools/future.rs new file mode 100644 index 0000000..476fd11 --- /dev/null +++ b/proxmox/src/tools/future.rs @@ -0,0 +1,48 @@ +//! Common extensions for Futures +use anyhow::Error; +use futures::future::{select, Either, FutureExt}; +use std::future::Future; +use std::time::Duration; +use tokio::time::delay_for; + +impl TimeoutFutureExt for T where T: Future {} + +/// Implements a timeout for futures, automatically aborting them if the timeout runs out before +/// the base future completes. +pub trait TimeoutFutureExt: Future { + /// Returned Future returns 'None' in case the timeout was reached, otherwise the original + /// return value. + fn or_timeout<'a>( + self, + timeout: Duration, + ) -> Box> + Unpin + Send + 'a> + where + Self: Sized + Unpin + Send + 'a, + { + let timeout_fut = delay_for(timeout); + Box::new(select(self, timeout_fut).map(|res| match res { + Either::Left((result, _)) => Some(result), + Either::Right(((), _)) => None, + })) + } + + /// Returned Future returns either the original result, or `Err` in case the timeout is + /// reached. Basically a shorthand to flatten a future that returns a `Result<_, Error>` with a + /// timeout. The base Future can return any kind of Error that can be made into an + /// `anyhow::Error`. + fn or_timeout_err<'a, O, E>( + self, + timeout: Duration, + err: Error, + ) -> Box> + Unpin + Send + 'a> + where + Self: Sized + Unpin + Send + 'a, + Self::Output: Into>, + E: Into + std::error::Error + Send + Sync + 'static, + { + Box::new(self.or_timeout(timeout).map(|res| match res { + Some(res) => res.into().map_err(Error::from), + None => Err(err), + })) + } +} diff --git a/proxmox/src/tools/mod.rs b/proxmox/src/tools/mod.rs index ff3a720..5d1f46e 100644 --- a/proxmox/src/tools/mod.rs +++ b/proxmox/src/tools/mod.rs @@ -20,6 +20,7 @@ pub mod serde; pub mod time; pub mod uuid; pub mod vec; +pub mod future; #[cfg(feature = "websocket")] pub mod websocket; -- 2.20.1