-
Notifications
You must be signed in to change notification settings - Fork 113
Listen on all provided addresses #644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,6 @@ mod wallet; | |
|
|
||
| use std::default::Default; | ||
| use std::net::ToSocketAddrs; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
| use std::sync::{Arc, Mutex, RwLock}; | ||
| use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; | ||
|
|
||
|
|
@@ -189,7 +188,6 @@ pub struct Node { | |
| peer_store: Arc<PeerStore<Arc<Logger>>>, | ||
| payment_store: Arc<PaymentStore>, | ||
| is_running: Arc<RwLock<bool>>, | ||
| is_listening: Arc<AtomicBool>, | ||
| node_metrics: Arc<RwLock<NodeMetrics>>, | ||
| om_mailbox: Option<Arc<OnionMessageMailbox>>, | ||
| async_payments_role: Option<AsyncPaymentsRole>, | ||
|
|
@@ -293,9 +291,7 @@ impl Node { | |
| if let Some(listening_addresses) = &self.config.listening_addresses { | ||
| // Setup networking | ||
| let peer_manager_connection_handler = Arc::clone(&self.peer_manager); | ||
| let mut stop_listen = self.stop_sender.subscribe(); | ||
| let listening_logger = Arc::clone(&self.logger); | ||
| let listening_indicator = Arc::clone(&self.is_listening); | ||
|
|
||
| let mut bind_addrs = Vec::with_capacity(listening_addresses.len()); | ||
|
|
||
|
|
@@ -313,45 +309,62 @@ impl Node { | |
| bind_addrs.extend(resolved_address); | ||
| } | ||
|
|
||
| self.runtime.spawn_cancellable_background_task(async move { | ||
| { | ||
| let listener = | ||
| tokio::net::TcpListener::bind(&*bind_addrs).await | ||
| .unwrap_or_else(|e| { | ||
| log_error!(listening_logger, "Failed to bind to listen addresses/ports - is something else already listening on it?: {}", e); | ||
| panic!( | ||
| "Failed to bind to listen address/port - is something else already listening on it?", | ||
| ); | ||
| }); | ||
|
|
||
| listening_indicator.store(true, Ordering::Release); | ||
|
|
||
| loop { | ||
| let peer_mgr = Arc::clone(&peer_manager_connection_handler); | ||
| tokio::select! { | ||
| _ = stop_listen.changed() => { | ||
| log_debug!( | ||
| listening_logger, | ||
| "Stopping listening to inbound connections." | ||
| let logger = Arc::clone(&listening_logger); | ||
| let listeners = self.runtime.block_on(async move { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to block if we want to return an error.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I see the point. I guess we need to if we want to abort on error for now, but we'll probably make
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternative is to use non-tokio binds here. |
||
| let mut listeners = Vec::new(); | ||
|
|
||
| // Try to bind to all addresses | ||
| for addr in &*bind_addrs { | ||
| match tokio::net::TcpListener::bind(addr).await { | ||
| Ok(listener) => { | ||
| log_trace!(logger, "Listener bound to {}", addr); | ||
| listeners.push(listener); | ||
| }, | ||
| Err(e) => { | ||
| log_error!( | ||
| logger, | ||
| "Failed to bind to {}: {} - is something else already listening?", | ||
| addr, | ||
| e | ||
| ); | ||
| break; | ||
| } | ||
| res = listener.accept() => { | ||
| let tcp_stream = res.unwrap().0; | ||
| tokio::spawn(async move { | ||
| lightning_net_tokio::setup_inbound( | ||
| Arc::clone(&peer_mgr), | ||
| tcp_stream.into_std().unwrap(), | ||
| ) | ||
| .await; | ||
| }); | ||
| } | ||
| return Err(Error::InvalidSocketAddress); | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| listening_indicator.store(false, Ordering::Release); | ||
| }); | ||
| Ok(listeners) | ||
| })?; | ||
|
|
||
| for listener in listeners { | ||
| let logger = Arc::clone(&listening_logger); | ||
| let peer_mgr = Arc::clone(&peer_manager_connection_handler); | ||
| let mut stop_listen = self.stop_sender.subscribe(); | ||
| let runtime = Arc::clone(&self.runtime); | ||
| self.runtime.spawn_cancellable_background_task(async move { | ||
| loop { | ||
| tokio::select! { | ||
| _ = stop_listen.changed() => { | ||
| log_debug!( | ||
| logger, | ||
| "Stopping listening to inbound connections." | ||
| ); | ||
| break; | ||
| } | ||
| res = listener.accept() => { | ||
| let tcp_stream = res.unwrap().0; | ||
| let peer_mgr = Arc::clone(&peer_mgr); | ||
| runtime.spawn_cancellable_background_task(async move { | ||
| lightning_net_tokio::setup_inbound( | ||
| Arc::clone(&peer_mgr), | ||
| tcp_stream.into_std().unwrap(), | ||
| ) | ||
| .await; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Regularly reconnect to persisted peers. | ||
|
|
@@ -666,7 +679,6 @@ impl Node { | |
| /// Returns the status of the [`Node`]. | ||
| pub fn status(&self) -> NodeStatus { | ||
| let is_running = *self.is_running.read().unwrap(); | ||
| let is_listening = self.is_listening.load(Ordering::Acquire); | ||
| let current_best_block = self.channel_manager.current_best_block().into(); | ||
| let locked_node_metrics = self.node_metrics.read().unwrap(); | ||
| let latest_lightning_wallet_sync_timestamp = | ||
|
|
@@ -684,7 +696,6 @@ impl Node { | |
|
|
||
| NodeStatus { | ||
| is_running, | ||
| is_listening, | ||
| current_best_block, | ||
| latest_lightning_wallet_sync_timestamp, | ||
| latest_onchain_wallet_sync_timestamp, | ||
|
|
@@ -1495,9 +1506,6 @@ impl Drop for Node { | |
| pub struct NodeStatus { | ||
| /// Indicates whether the [`Node`] is running. | ||
| pub is_running: bool, | ||
| /// Indicates whether the [`Node`] is listening for incoming connections on the addresses | ||
| /// configured via [`Config::listening_addresses`]. | ||
| pub is_listening: bool, | ||
| /// The best block to which our Lightning wallet is currently synced. | ||
| pub current_best_block: BestBlock, | ||
| /// The timestamp, in seconds since start of the UNIX epoch, when we last successfully synced | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.