From bda9b64d0983d8dedf657354c4e31e5f07709c4c Mon Sep 17 00:00:00 2001 From: jkds Date: Wed, 31 Dec 2025 05:06:38 +0100 Subject: [PATCH] net: add reload() fn which reloads settings. currently all stubs except outgoing conns setting --- bin/app/src/plugin/darkirc.rs | 14 ++++---------- src/net/p2p.rs | 23 ++++++++++++++++++++++- src/net/session/direct_session.rs | 2 ++ src/net/session/inbound_session.rs | 2 ++ src/net/session/manual_session.rs | 2 ++ src/net/session/mod.rs | 3 +++ src/net/session/outbound_session.rs | 9 ++++++--- src/net/session/refine_session.rs | 2 ++ src/net/session/seedsync_session.rs | 2 ++ 9 files changed, 45 insertions(+), 14 deletions(-) diff --git a/bin/app/src/plugin/darkirc.rs b/bin/app/src/plugin/darkirc.rs index f7c9cbaea..d51d6468a 100644 --- a/bin/app/src/plugin/darkirc.rs +++ b/bin/app/src/plugin/darkirc.rs @@ -567,11 +567,8 @@ impl DarkIrc { let start_task = ex.spawn(async move { while let Ok(_) = start_recv.recv().await { i!("App started: set outbound connections to {P2P_OUTBOUND_ACTIVE}"); - if let Err(e) = - p2p.session_outbound().set_outbound_connections(P2P_OUTBOUND_ACTIVE).await - { - e!("Failed to set outbound connections: {e}"); - } + p2p.settings().write().await.outbound_connections = P2P_OUTBOUND_ACTIVE; + p2p.clone().reload().await; } }); @@ -581,11 +578,8 @@ impl DarkIrc { let stop_task = ex.spawn(async move { while let Ok(_) = stop_recv.recv().await { i!("App stopped: set outbound connections to {P2P_OUTBOUND_SLEEP}"); - if let Err(e) = - p2p.session_outbound().set_outbound_connections(P2P_OUTBOUND_SLEEP).await - { - e!("Failed to set outbound connections: {e}"); - } + p2p.settings().write().await.outbound_connections = P2P_OUTBOUND_SLEEP; + p2p.clone().reload().await; } }); diff --git a/src/net/p2p.rs b/src/net/p2p.rs index 1b26203a8..98d84ad85 100644 --- a/src/net/p2p.rs +++ b/src/net/p2p.rs @@ -36,7 +36,7 @@ use super::{ session::{ DirectSession, DirectSessionPtr, InboundSession, InboundSessionPtr, ManualSession, ManualSessionPtr, OutboundSession, OutboundSessionPtr, RefineSession, RefineSessionPtr, - SeedSyncSession, SeedSyncSessionPtr, + SeedSyncSession, SeedSyncSessionPtr, Session, }, settings::Settings, }; @@ -227,6 +227,27 @@ impl P2p { Arc::clone(&self.settings) } + /// Reload settings and apply any changes to the running P2P subsystem. + /// + /// Users should modify settings through the settings lock, then call this + /// method to apply the changes: + /// ```rust + /// let mut settings = p2p.settings().write().await; + /// settings.outbound_connections = new_value; + /// drop(settings); + /// p2p.reload().await; + /// ``` + pub async fn reload(self: Arc) { + self.session_manual().reload().await; + self.session_inbound().reload().await; + self.session_outbound().reload().await; + self.session_refine().reload().await; + self.session_seedsync().reload().await; + self.session_direct().reload().await; + + debug!(target: "net::p2p::reload", "P2P settings reloaded successfully"); + } + /// Return an atomic pointer to the list of hosts pub fn hosts(&self) -> HostsPtr { self.hosts.clone() diff --git a/src/net/session/direct_session.rs b/src/net/session/direct_session.rs index 2a2245795..1bf995d2d 100644 --- a/src/net/session/direct_session.rs +++ b/src/net/session/direct_session.rs @@ -430,6 +430,8 @@ impl Session for DirectSession { fn type_id(&self) -> SessionBitFlag { SESSION_DIRECT } + + async fn reload(self: Arc) {} } struct ChannelTask { diff --git a/src/net/session/inbound_session.rs b/src/net/session/inbound_session.rs index a7f5666fc..018722a37 100644 --- a/src/net/session/inbound_session.rs +++ b/src/net/session/inbound_session.rs @@ -223,4 +223,6 @@ impl Session for InboundSession { fn type_id(&self) -> SessionBitFlag { SESSION_INBOUND } + + async fn reload(self: Arc) {} } diff --git a/src/net/session/manual_session.rs b/src/net/session/manual_session.rs index 4dc0f09f1..eb31092e5 100644 --- a/src/net/session/manual_session.rs +++ b/src/net/session/manual_session.rs @@ -108,6 +108,8 @@ impl Session for ManualSession { fn type_id(&self) -> SessionBitFlag { SESSION_MANUAL } + + async fn reload(self: Arc) {} } struct Slot { diff --git a/src/net/session/mod.rs b/src/net/session/mod.rs index 607fa542e..54130cc74 100644 --- a/src/net/session/mod.rs +++ b/src/net/session/mod.rs @@ -254,4 +254,7 @@ pub trait Session: Sync { /// Return the session bit flag for the session type fn type_id(&self) -> SessionBitFlag; + + /// Reload settings for this session + async fn reload(self: Arc); } diff --git a/src/net/session/outbound_session.rs b/src/net/session/outbound_session.rs index eb180d2fc..13c70401d 100644 --- a/src/net/session/outbound_session.rs +++ b/src/net/session/outbound_session.rs @@ -140,7 +140,7 @@ impl OutboundSession { /// Sets the number of outbound connections. /// If the number is less than the current, then it will first drop empty slots. - pub async fn set_outbound_connections(self: Arc, n: usize) -> Result<()> { + async fn set_outbound_connections(self: Arc, n: usize) { // Guaranteed to be correct since slots is locked for the duration of this method. let mut slots = self.slots.lock().await; let slots_len = slots.len(); @@ -151,8 +151,6 @@ impl OutboundSession { self.remove_slots(&mut slots, n).await; } // Do nothing when n == current - - Ok(()) } async fn add_slots(self: Arc, slots: &mut Vec>, target: usize) { @@ -209,6 +207,11 @@ impl Session for OutboundSession { fn type_id(&self) -> SessionBitFlag { SESSION_OUTBOUND } + + async fn reload(self: Arc) { + let outbound_connections = self.p2p().settings().read().await.outbound_connections; + self.set_outbound_connections(outbound_connections).await; + } } struct Slot { diff --git a/src/net/session/refine_session.rs b/src/net/session/refine_session.rs index f52019ca9..872100650 100644 --- a/src/net/session/refine_session.rs +++ b/src/net/session/refine_session.rs @@ -179,6 +179,8 @@ impl Session for RefineSession { fn type_id(&self) -> SessionBitFlag { SESSION_REFINE } + + async fn reload(self: Arc) {} } /// Periodically probes entries in the greylist. diff --git a/src/net/session/seedsync_session.rs b/src/net/session/seedsync_session.rs index 96db78233..874808c92 100644 --- a/src/net/session/seedsync_session.rs +++ b/src/net/session/seedsync_session.rs @@ -145,6 +145,8 @@ impl Session for SeedSyncSession { fn type_id(&self) -> SessionBitFlag { SESSION_SEED } + + async fn reload(self: Arc) {} } struct Slot {