Просмотр исходного кода

hosts: use async mutex instead of async rwlock on HostRegistry

Mutex makes more sense here since there are orders of magnitude more call to write() than there are to read() (we counted) (trust me bro)
draoi 2 лет назад
Родитель
Сommit
5d7632d79f
1 измененных файлов с 7 добавлено и 7 удалено
  1. 7 7
      src/net/hosts.rs

+ 7 - 7
src/net/hosts.rs

@@ -26,7 +26,7 @@ use std::{
 
 
 use log::{debug, error, info, trace, warn};
 use log::{debug, error, info, trace, warn};
 use rand::{prelude::IteratorRandom, rngs::OsRng, Rng};
 use rand::{prelude::IteratorRandom, rngs::OsRng, Rng};
-use smol::lock::RwLock;
+use smol::lock::{Mutex as AsyncMutex, RwLock};
 use url::Url;
 use url::Url;
 
 
 use super::{settings::SettingsPtr, ChannelPtr};
 use super::{settings::SettingsPtr, ChannelPtr};
@@ -75,7 +75,7 @@ pub type HostsPtr = Arc<Hosts>;
 /// Keeps track of hosts and their current state. Prevents race conditions
 /// Keeps track of hosts and their current state. Prevents race conditions
 /// where multiple threads are simultaneously trying to change the state of
 /// where multiple threads are simultaneously trying to change the state of
 /// a given host.
 /// a given host.
-pub(in crate::net) type HostRegistry = RwLock<HashMap<Url, HostState>>;
+pub(in crate::net) type HostRegistry = AsyncMutex<HashMap<Url, HostState>>;
 
 
 /// HostState is a set of mutually exclusive states that can be Insert,
 /// HostState is a set of mutually exclusive states that can be Insert,
 /// Refine, Move, Connect, Suspend or Connected. The state is `None` when the
 /// Refine, Move, Connect, Suspend or Connected. The state is `None` when the
@@ -813,7 +813,7 @@ impl Hosts {
     /// Create a new hosts list
     /// Create a new hosts list
     pub(in crate::net) fn new(settings: SettingsPtr) -> HostsPtr {
     pub(in crate::net) fn new(settings: SettingsPtr) -> HostsPtr {
         Arc::new(Self {
         Arc::new(Self {
-            registry: RwLock::new(HashMap::new()),
+            registry: AsyncMutex::new(HashMap::new()),
             container: HostContainer::new(),
             container: HostContainer::new(),
             store_publisher: Publisher::new(),
             store_publisher: Publisher::new(),
             channel_publisher: Publisher::new(),
             channel_publisher: Publisher::new(),
@@ -872,7 +872,7 @@ impl Hosts {
         addr: Url,
         addr: Url,
         new_state: HostState,
         new_state: HostState,
     ) -> Result<HostState> {
     ) -> Result<HostState> {
-        let mut registry = self.registry.write().await;
+        let mut registry = self.registry.lock().await;
 
 
         trace!(target: "net::hosts::try_update_registry()", "Try register addr={}, state={}",
         trace!(target: "net::hosts::try_update_registry()", "Try register addr={}, state={}",
                addr, &new_state);
                addr, &new_state);
@@ -943,13 +943,13 @@ impl Hosts {
     /// the refinery or outbound connect loop, and may result in invalid states. It should
     /// the refinery or outbound connect loop, and may result in invalid states. It should
     /// only be called when it is completely safe to do so.
     /// only be called when it is completely safe to do so.
     pub(in crate::net) async fn unregister(&self, addr: &Url) {
     pub(in crate::net) async fn unregister(&self, addr: &Url) {
-        self.registry.write().await.remove(addr);
+        self.registry.lock().await.remove(addr);
         debug!(target: "net::hosts::unregister()", "Removed {} from HostRegistry", addr);
         debug!(target: "net::hosts::unregister()", "Removed {} from HostRegistry", addr);
     }
     }
 
 
     /// Returns the list of connected channels.
     /// Returns the list of connected channels.
     pub async fn channels(&self) -> Vec<ChannelPtr> {
     pub async fn channels(&self) -> Vec<ChannelPtr> {
-        let registry = self.registry.read().await;
+        let registry = self.registry.lock().await;
         let mut channels = Vec::new();
         let mut channels = Vec::new();
 
 
         for (_, state) in registry.iter() {
         for (_, state) in registry.iter() {
@@ -962,7 +962,7 @@ impl Hosts {
 
 
     /// Returns the list of suspended channels.
     /// Returns the list of suspended channels.
     pub(in crate::net) async fn suspended(&self) -> Vec<Url> {
     pub(in crate::net) async fn suspended(&self) -> Vec<Url> {
-        let registry = self.registry.read().await;
+        let registry = self.registry.lock().await;
         let mut addrs = Vec::new();
         let mut addrs = Vec::new();
 
 
         for (url, state) in registry.iter() {
         for (url, state) in registry.iter() {