Sfoglia il codice sorgente

net: use sync Mutex for last_connection

RwLock is overkill in this case since there is only ever one reader and
one writer.

For more info why a sync Mutex is appropiate in this case, see commit 65a8e9a44fa3c835158550e7eb5b5e1946e3028f
draoi 2 anni fa
parent
commit
531e4b2f92
2 ha cambiato i file con 6 aggiunte e 4 eliminazioni
  1. 3 3
      src/net/hosts.rs
  2. 3 1
      src/net/session/refine_session.rs

+ 3 - 3
src/net/hosts.rs

@@ -795,7 +795,7 @@ pub struct Hosts {
     pub(in crate::net) channel_publisher: PublisherPtr<Result<ChannelPtr>>,
 
     /// Keeps track of the last time a connection was made.
-    pub(in crate::net) last_connection: RwLock<Instant>,
+    pub(in crate::net) last_connection: Mutex<Instant>,
 
     /// Marker for IPv6 availability
     pub(in crate::net) ipv6_available: Mutex<bool>,
@@ -812,7 +812,7 @@ impl Hosts {
             container: HostContainer::new(),
             store_publisher: Publisher::new(),
             channel_publisher: Publisher::new(),
-            last_connection: RwLock::new(Instant::now()),
+            last_connection: Mutex::new(Instant::now()),
             ipv6_available: Mutex::new(true),
             settings,
         })
@@ -987,7 +987,7 @@ impl Hosts {
         // Notify that channel processing was successful
         self.channel_publisher.notify(Ok(channel.clone())).await;
 
-        let mut last_online = self.last_connection.write().await;
+        let mut last_online = self.last_connection.lock().unwrap();
         *last_online = Instant::now();
     }
 

+ 3 - 1
src/net/session/refine_session.rs

@@ -234,7 +234,9 @@ impl GreylistRefinery {
             // Pause the refinery if we've had zero connections for longer than the configured
             // limit.
             let offline_limit = Duration::from_secs(settings.time_with_no_connections);
-            let offline_timer = Instant::now().duration_since(*hosts.last_connection.read().await);
+
+            let offline_timer =
+                { Instant::now().duration_since(*hosts.last_connection.lock().unwrap()) };
 
             if hosts.channels().await.is_empty() && offline_timer >= offline_limit {
                 warn!(target: "net::refinery", "No connections for {}s. GreylistRefinery paused.",