Pārlūkot izejas kodu

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

jkds 7 mēneši atpakaļ
vecāks
revīzija
bda9b64d09

+ 4 - 10
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;
             }
         });
 

+ 22 - 1
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>) {
+        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()

+ 2 - 0
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<Self>) {}
 }
 
 struct ChannelTask {

+ 2 - 0
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<Self>) {}
 }

+ 2 - 0
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<Self>) {}
 }
 
 struct Slot {

+ 3 - 0
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<Self>);
 }

+ 6 - 3
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<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 {

+ 2 - 0
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<Self>) {}
 }
 
 /// Periodically probes entries in the greylist.

+ 2 - 0
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<Self>) {}
 }
 
 struct Slot {