From: Matt Corallo Date: Tue, 17 Jan 2023 00:03:43 +0000 (+0000) Subject: Use the user-provided `SleepFuture` for interval checks in BP X-Git-Tag: v0.0.114-beta~55^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e59b3847a363afba5d30e1f1271341e3163f7591;p=rust-lightning Use the user-provided `SleepFuture` for interval checks in BP `background-processor` does a number of jobs on various timers. Instead of doing those by interrogating `std::time::Instant`, this change swaps to using the existing user-provided sleep future. Fixes #1864. --- diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index e93139bf0..b2ee86547 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -36,7 +36,7 @@ use std::time::{Duration, Instant}; use std::ops::Deref; #[cfg(feature = "futures")] -use futures_util::{select_biased, future::FutureExt}; +use futures_util::{select_biased, future::FutureExt, task}; /// `BackgroundProcessor` takes care of tasks that (1) need to happen periodically to keep /// Rust-Lightning running properly, and (2) either can or should be run in the background. Its @@ -364,7 +364,7 @@ pub async fn process_events_async< PM: 'static + Deref> + Send + Sync, S: 'static + Deref + Send + Sync, SC: WriteableScore<'a>, - SleepFuture: core::future::Future, + SleepFuture: core::future::Future + core::marker::Unpin, Sleeper: Fn(Duration) -> SleepFuture >( persister: PS, event_handler: EventHandler, chain_monitor: M, channel_manager: CM, @@ -411,7 +411,12 @@ where false } } - }, |_| Instant::now(), |time: &Instant, dur| time.elapsed().as_secs() > dur) + }, |t| sleeper(Duration::from_secs(t)), + |fut: &mut SleepFuture, _| { + let mut waker = task::noop_waker(); + let mut ctx = task::Context::from_waker(&mut waker); + core::pin::Pin::new(fut).poll(&mut ctx).is_ready() + }) } impl BackgroundProcessor {