net: add reload() fn which reloads settings. currently all stubs except outgoing conns setting

This commit is contained in:
jkds
2025-12-31 05:06:38 +01:00
parent 0897ce2f8c
commit bda9b64d09
9 changed files with 45 additions and 14 deletions

View File

@@ -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;
}
});

View File

@@ -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>) {
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()

View File

@@ -430,6 +430,8 @@ impl Session for DirectSession {
fn type_id(&self) -> SessionBitFlag {
SESSION_DIRECT
}
async fn reload(self: Arc<Self>) {}
}
struct ChannelTask {

View File

@@ -223,4 +223,6 @@ impl Session for InboundSession {
fn type_id(&self) -> SessionBitFlag {
SESSION_INBOUND
}
async fn reload(self: Arc<Self>) {}
}

View File

@@ -108,6 +108,8 @@ impl Session for ManualSession {
fn type_id(&self) -> SessionBitFlag {
SESSION_MANUAL
}
async fn reload(self: Arc<Self>) {}
}
struct Slot {

View File

@@ -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<Self>);
}

View File

@@ -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<Self>, n: usize) -> Result<()> {
async fn set_outbound_connections(self: Arc<Self>, 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<Self>, slots: &mut Vec<Arc<Slot>>, target: usize) {
@@ -209,6 +207,11 @@ impl Session for OutboundSession {
fn type_id(&self) -> SessionBitFlag {
SESSION_OUTBOUND
}
async fn reload(self: Arc<Self>) {
let outbound_connections = self.p2p().settings().read().await.outbound_connections;
self.set_outbound_connections(outbound_connections).await;
}
}
struct Slot {

View File

@@ -179,6 +179,8 @@ impl Session for RefineSession {
fn type_id(&self) -> SessionBitFlag {
SESSION_REFINE
}
async fn reload(self: Arc<Self>) {}
}
/// Periodically probes entries in the greylist.

View File

@@ -145,6 +145,8 @@ impl Session for SeedSyncSession {
fn type_id(&self) -> SessionBitFlag {
SESSION_SEED
}
async fn reload(self: Arc<Self>) {}
}
struct Slot {