Skip to content

Commit 8a4afb6

Browse files
authored
Merge pull request #319 from lightningdevkit/2024-06-start-with-runtime
Allow start from outer runtime
2 parents 13ec8df + f2074f1 commit 8a4afb6

File tree

6 files changed

+25
-16
lines changed

6 files changed

+25
-16
lines changed

src/event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ where
354354
network_graph: Arc<Graph>,
355355
payment_store: Arc<PaymentStore<L>>,
356356
peer_store: Arc<PeerStore<L>>,
357-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
357+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
358358
logger: L,
359359
config: Arc<Config>,
360360
}
@@ -369,7 +369,7 @@ where
369369
channel_manager: Arc<ChannelManager>, connection_manager: Arc<ConnectionManager<L>>,
370370
output_sweeper: Arc<Sweeper>, network_graph: Arc<Graph>,
371371
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
372-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
372+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, logger: L, config: Arc<Config>,
373373
) -> Self {
374374
Self {
375375
event_queue,

src/lib.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ uniffi::include_scaffolding!("ldk_node");
171171
///
172172
/// Needs to be initialized and instantiated through [`Builder::build`].
173173
pub struct Node {
174-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
174+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
175175
stop_sender: tokio::sync::watch::Sender<()>,
176176
event_handling_stopped_sender: tokio::sync::watch::Sender<()>,
177177
config: Arc<Config>,
@@ -211,6 +211,20 @@ impl Node {
211211
/// After this returns, the [`Node`] instance can be controlled via the provided API methods in
212212
/// a thread-safe manner.
213213
pub fn start(&self) -> Result<(), Error> {
214+
let runtime =
215+
Arc::new(tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap());
216+
self.start_with_runtime(runtime)
217+
}
218+
219+
/// Starts the necessary background tasks (such as handling events coming from user input,
220+
/// LDK/BDK, and the peer-to-peer network) on the the given `runtime`.
221+
///
222+
/// This allows to have LDK Node reuse an outer pre-existing runtime, e.g., to avoid stacking Tokio
223+
/// runtime contexts.
224+
///
225+
/// After this returns, the [`Node`] instance can be controlled via the provided API methods in
226+
/// a thread-safe manner.
227+
pub fn start_with_runtime(&self, runtime: Arc<tokio::runtime::Runtime>) -> Result<(), Error> {
214228
// Acquire a run lock and hold it until we're setup.
215229
let mut runtime_lock = self.runtime.write().unwrap();
216230
if runtime_lock.is_some() {
@@ -225,8 +239,6 @@ impl Node {
225239
self.config.network
226240
);
227241

228-
let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();
229-
230242
// Block to ensure we update our fee rate cache once on startup
231243
let fee_estimator = Arc::clone(&self.fee_estimator);
232244
let sync_logger = Arc::clone(&self.logger);
@@ -862,9 +874,6 @@ impl Node {
862874
);
863875
}
864876

865-
// Shutdown our runtime. By now ~no or only very few tasks should be left.
866-
runtime.shutdown_timeout(Duration::from_secs(10));
867-
868877
log_info!(self.logger, "Shutdown complete.");
869878
Ok(())
870879
}

src/payment/bolt11.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::time::SystemTime;
3333
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
3434
/// [`Node::bolt11_payment`]: crate::Node::bolt11_payment
3535
pub struct Bolt11Payment {
36-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
36+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
3737
channel_manager: Arc<ChannelManager>,
3838
connection_manager: Arc<ConnectionManager<Arc<FilesystemLogger>>>,
3939
keys_manager: Arc<KeysManager>,
@@ -46,7 +46,7 @@ pub struct Bolt11Payment {
4646

4747
impl Bolt11Payment {
4848
pub(crate) fn new(
49-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
49+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
5050
channel_manager: Arc<ChannelManager>,
5151
connection_manager: Arc<ConnectionManager<Arc<FilesystemLogger>>>,
5252
keys_manager: Arc<KeysManager>,

src/payment/bolt12.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
2828
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
2929
/// [`Node::bolt12_payment`]: crate::Node::bolt12_payment
3030
pub struct Bolt12Payment {
31-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
31+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
3232
channel_manager: Arc<ChannelManager>,
3333
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
3434
logger: Arc<FilesystemLogger>,
3535
}
3636

3737
impl Bolt12Payment {
3838
pub(crate) fn new(
39-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
39+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
4040
channel_manager: Arc<ChannelManager>,
4141
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>, logger: Arc<FilesystemLogger>,
4242
) -> Self {

src/payment/onchain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::sync::{Arc, RwLock};
1515
///
1616
/// [`Node::onchain_payment`]: crate::Node::onchain_payment
1717
pub struct OnchainPayment {
18-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
18+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
1919
wallet: Arc<Wallet>,
2020
channel_manager: Arc<ChannelManager>,
2121
config: Arc<Config>,
@@ -24,7 +24,7 @@ pub struct OnchainPayment {
2424

2525
impl OnchainPayment {
2626
pub(crate) fn new(
27-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, wallet: Arc<Wallet>,
27+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, wallet: Arc<Wallet>,
2828
channel_manager: Arc<ChannelManager>, config: Arc<Config>, logger: Arc<FilesystemLogger>,
2929
) -> Self {
3030
Self { runtime, wallet, channel_manager, config, logger }

src/payment/spontaneous.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::sync::{Arc, RwLock};
2323
///
2424
/// [`Node::spontaneous_payment`]: crate::Node::spontaneous_payment
2525
pub struct SpontaneousPayment {
26-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
26+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
2727
channel_manager: Arc<ChannelManager>,
2828
keys_manager: Arc<KeysManager>,
2929
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
@@ -33,7 +33,7 @@ pub struct SpontaneousPayment {
3333

3434
impl SpontaneousPayment {
3535
pub(crate) fn new(
36-
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
36+
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
3737
channel_manager: Arc<ChannelManager>, keys_manager: Arc<KeysManager>,
3838
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>, config: Arc<Config>,
3939
logger: Arc<FilesystemLogger>,

0 commit comments

Comments
 (0)