Skip to content

Commit 91bce83

Browse files
committed
Do an initial poll of the ChannelManager write future immediately
In 7d856fe5b05d69b301fdb8a48352f3f3a16efca9 we moved to doing all of our persistence operations in the (async) background processor in parallel. This is great for slow persistence operations (eg with remote persistence). However, the `ChannelManager` persistence is very latency-sensitive (delays may lead to accidental force-closures today) and thus we don't really want to wait on network graph pruning or scorer time-stepping (which can be somewhat slow). Instead, we keep the new bulk-polling of the persistence operations but immediately do a single step of the `ChannelManager` persistence future after it is created. This should give it a chance to get going - initializing whatever network sends or connections need to happen - before we get busy doing CPU-intensive stuff. While it may not actually make the persist happen in full, it at least gives it a chance, and should make as much progress as possible until a network resources is exhausted (eg send buffer is full), at which point we need to wait on the network which is almost certainly slow than the 1ms or so it will take to time-step the scorer.
1 parent 664511b commit 91bce83

File tree

1 file changed

+17
-1
lines changed
  • lightning-background-processor/src

1 file changed

+17
-1
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,9 @@ pub(crate) mod futures_util {
482482
pub(crate) fn set_a(&mut self, fut: A) {
483483
self.a = JoinerResult::Pending(Some(fut));
484484
}
485+
pub(crate) fn set_a_res(&mut self, res: Result<(), E>) {
486+
self.a = JoinerResult::Ready(res);
487+
}
485488
pub(crate) fn set_b(&mut self, fut: B) {
486489
self.b = JoinerResult::Pending(Some(fut));
487490
}
@@ -937,7 +940,20 @@ where
937940
.await
938941
};
939942
// TODO: Once our MSRV is 1.68 we should be able to drop the Box
940-
futures.set_a(Box::pin(fut));
943+
let mut fut = Box::pin(fut);
944+
945+
// Because persisting the ChannelManager is important to avoid accidental
946+
// force-closures, go ahead and poll the future once before we do slightly more
947+
// CPU-intensive tasks in the form of NetworkGraph pruning or scorer time-stepping
948+
// below. This will get it moving but won't block us for too long if the underlying
949+
// future is actually async.
950+
use core::future::Future;
951+
let mut waker = dummy_waker();
952+
let mut ctx = task::Context::from_waker(&mut waker);
953+
match core::pin::Pin::new(&mut fut).poll(&mut ctx) {
954+
task::Poll::Ready(res) => futures.set_a_res(res),
955+
task::Poll::Pending => futures.set_a(fut),
956+
}
941957

942958
log_trace!(logger, "Done persisting ChannelManager.");
943959
}

0 commit comments

Comments
 (0)