Browse Source

refine_session: clear the darklist once a day

This helps reduce the tradeoff space between a) ensuring all transports
are propagated, even those we do not support b) ensuring hosts shared
around the network are valid.

We assume that a one day period is sufficient for valid hosts to
propagate and enter into non-dark hostlists of a peer that does support
this transport.
draoi 2 years ago
parent
commit
38aa8be3b3
1 changed files with 36 additions and 2 deletions
  1. 36 2
      src/net/session/refine_session.rs

+ 36 - 2
src/net/session/refine_session.rs

@@ -31,7 +31,7 @@ use futures::{
 };
 };
 use smol::Timer;
 use smol::Timer;
 use std::{
 use std::{
-    sync::{Arc, Weak},
+    sync::{Arc, Mutex, Weak},
     time::{Duration, Instant, UNIX_EPOCH},
     time::{Duration, Instant, UNIX_EPOCH},
 };
 };
 
 
@@ -60,11 +60,18 @@ pub struct RefineSession {
 
 
     /// Task that periodically checks entries in the greylist.
     /// Task that periodically checks entries in the greylist.
     pub(in crate::net) refinery: Arc<GreylistRefinery>,
     pub(in crate::net) refinery: Arc<GreylistRefinery>,
+
+    /// Task that clears the darklist once daily.
+    darklist_clear_task: Mutex<Option<smol::Task<()>>>,
 }
 }
 
 
 impl RefineSession {
 impl RefineSession {
     pub fn new(p2p: Weak<P2p>) -> RefineSessionPtr {
     pub fn new(p2p: Weak<P2p>) -> RefineSessionPtr {
-        Arc::new_cyclic(|session| Self { p2p, refinery: GreylistRefinery::new(session.clone()) })
+        Arc::new_cyclic(|session| Self {
+            p2p,
+            refinery: GreylistRefinery::new(session.clone()),
+            darklist_clear_task: Mutex::new(None),
+        })
     }
     }
 
 
     /// Start the refinery and self handshake processes.
     /// Start the refinery and self handshake processes.
@@ -92,6 +99,27 @@ impl RefineSession {
 
 
         debug!(target: "net::refine_session", "Starting greylist refinery process");
         debug!(target: "net::refine_session", "Starting greylist refinery process");
         self.refinery.clone().start().await;
         self.refinery.clone().start().await;
+
+        // Clear the darklist every 86400 seconds, i.e. once a day.
+        debug!(target: "net::refine_session", "Starting darklist clear task");
+        let one_day = 86400;
+        *self.darklist_clear_task.lock().unwrap() =
+            Some(self.p2p().executor().spawn(self.clone().clear_darklist(one_day)));
+    }
+
+    /// Sleep for a specified time period, then delete all entries from the darklist.
+    async fn clear_darklist(self: Arc<Self>, clear_period: u64) {
+        let hosts = &self.p2p().hosts().container;
+        loop {
+            sleep(clear_period).await;
+            debug!(target: "net::refine_session::clear_darklist",
+                "{} seconds has passed, clearing darklist...", clear_period);
+
+            let mut darklist = hosts.hostlists[HostColor::Dark as usize].write().unwrap();
+            darklist.clear();
+
+            drop(darklist);
+        }
     }
     }
 
 
     /// Stop the refinery and self handshake processes.
     /// Stop the refinery and self handshake processes.
@@ -99,6 +127,12 @@ impl RefineSession {
         debug!(target: "net::refine_session", "Stopping refinery process");
         debug!(target: "net::refine_session", "Stopping refinery process");
         self.refinery.clone().stop().await;
         self.refinery.clone().stop().await;
 
 
+        debug!(target: "net::refine_session", "Stopping darklist clear process");
+        let task = std::mem::take(&mut *self.darklist_clear_task.lock().unwrap());
+        if task.is_some() {
+            task.unwrap().cancel().await;
+        }
+
         if let Some(ref hostlist) = self.p2p().settings().read().await.hostlist {
         if let Some(ref hostlist) = self.p2p().settings().read().await.hostlist {
             match self.p2p().hosts().container.save_all(hostlist) {
             match self.p2p().hosts().container.save_all(hostlist) {
                 Ok(()) => {
                 Ok(()) => {