Skip to content

Commit

Permalink
Merge pull request #319 from lightningdevkit/2024-06-start-with-runtime
Browse files Browse the repository at this point in the history
Allow start from outer runtime
  • Loading branch information
tnull authored Jul 11, 2024
2 parents 13ec8df + f2074f1 commit 8a4afb6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ where
network_graph: Arc<Graph>,
payment_store: Arc<PaymentStore<L>>,
peer_store: Arc<PeerStore<L>>,
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
logger: L,
config: Arc<Config>,
}
Expand All @@ -369,7 +369,7 @@ where
channel_manager: Arc<ChannelManager>, connection_manager: Arc<ConnectionManager<L>>,
output_sweeper: Arc<Sweeper>, network_graph: Arc<Graph>,
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, logger: L, config: Arc<Config>,
) -> Self {
Self {
event_queue,
Expand Down
21 changes: 15 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ uniffi::include_scaffolding!("ldk_node");
///
/// Needs to be initialized and instantiated through [`Builder::build`].
pub struct Node {
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
stop_sender: tokio::sync::watch::Sender<()>,
event_handling_stopped_sender: tokio::sync::watch::Sender<()>,
config: Arc<Config>,
Expand Down Expand Up @@ -211,6 +211,20 @@ impl Node {
/// After this returns, the [`Node`] instance can be controlled via the provided API methods in
/// a thread-safe manner.
pub fn start(&self) -> Result<(), Error> {
let runtime =
Arc::new(tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap());
self.start_with_runtime(runtime)
}

/// Starts the necessary background tasks (such as handling events coming from user input,
/// LDK/BDK, and the peer-to-peer network) on the the given `runtime`.
///
/// This allows to have LDK Node reuse an outer pre-existing runtime, e.g., to avoid stacking Tokio
/// runtime contexts.
///
/// After this returns, the [`Node`] instance can be controlled via the provided API methods in
/// a thread-safe manner.
pub fn start_with_runtime(&self, runtime: Arc<tokio::runtime::Runtime>) -> Result<(), Error> {
// Acquire a run lock and hold it until we're setup.
let mut runtime_lock = self.runtime.write().unwrap();
if runtime_lock.is_some() {
Expand All @@ -225,8 +239,6 @@ impl Node {
self.config.network
);

let runtime = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();

// Block to ensure we update our fee rate cache once on startup
let fee_estimator = Arc::clone(&self.fee_estimator);
let sync_logger = Arc::clone(&self.logger);
Expand Down Expand Up @@ -862,9 +874,6 @@ impl Node {
);
}

// Shutdown our runtime. By now ~no or only very few tasks should be left.
runtime.shutdown_timeout(Duration::from_secs(10));

log_info!(self.logger, "Shutdown complete.");
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use std::time::SystemTime;
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
/// [`Node::bolt11_payment`]: crate::Node::bolt11_payment
pub struct Bolt11Payment {
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
channel_manager: Arc<ChannelManager>,
connection_manager: Arc<ConnectionManager<Arc<FilesystemLogger>>>,
keys_manager: Arc<KeysManager>,
Expand All @@ -46,7 +46,7 @@ pub struct Bolt11Payment {

impl Bolt11Payment {
pub(crate) fn new(
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
channel_manager: Arc<ChannelManager>,
connection_manager: Arc<ConnectionManager<Arc<FilesystemLogger>>>,
keys_manager: Arc<KeysManager>,
Expand Down
4 changes: 2 additions & 2 deletions src/payment/bolt12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
/// [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
/// [`Node::bolt12_payment`]: crate::Node::bolt12_payment
pub struct Bolt12Payment {
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
channel_manager: Arc<ChannelManager>,
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
logger: Arc<FilesystemLogger>,
}

impl Bolt12Payment {
pub(crate) fn new(
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
channel_manager: Arc<ChannelManager>,
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>, logger: Arc<FilesystemLogger>,
) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/payment/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::{Arc, RwLock};
///
/// [`Node::onchain_payment`]: crate::Node::onchain_payment
pub struct OnchainPayment {
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager>,
config: Arc<Config>,
Expand All @@ -24,7 +24,7 @@ pub struct OnchainPayment {

impl OnchainPayment {
pub(crate) fn new(
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, wallet: Arc<Wallet>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>, wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager>, config: Arc<Config>, logger: Arc<FilesystemLogger>,
) -> Self {
Self { runtime, wallet, channel_manager, config, logger }
Expand Down
4 changes: 2 additions & 2 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::{Arc, RwLock};
///
/// [`Node::spontaneous_payment`]: crate::Node::spontaneous_payment
pub struct SpontaneousPayment {
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
channel_manager: Arc<ChannelManager>,
keys_manager: Arc<KeysManager>,
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>,
Expand All @@ -33,7 +33,7 @@ pub struct SpontaneousPayment {

impl SpontaneousPayment {
pub(crate) fn new(
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
runtime: Arc<RwLock<Option<Arc<tokio::runtime::Runtime>>>>,
channel_manager: Arc<ChannelManager>, keys_manager: Arc<KeysManager>,
payment_store: Arc<PaymentStore<Arc<FilesystemLogger>>>, config: Arc<Config>,
logger: Arc<FilesystemLogger>,
Expand Down

0 comments on commit 8a4afb6

Please sign in to comment.