diff options
Diffstat (limited to 'src/netapp.rs')
-rw-r--r-- | src/netapp.rs | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/netapp.rs b/src/netapp.rs index 967105e..6f6da5b 100644 --- a/src/netapp.rs +++ b/src/netapp.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::net::SocketAddr; use std::pin::Pin; use std::sync::{Arc, RwLock}; +use std::time::Instant; use std::future::Future; @@ -75,10 +76,18 @@ where M::KIND, hex::encode(remote) ); + let begin_time = Instant::now(); let res = match rmp_serde::decode::from_read_ref::<_, M>(&bytes[..]) { Ok(msg) => Ok(handler(remote, msg).await), Err(e) => Err(e.to_string()), }; + let end_time = Instant::now(); + debug!( + "Request {:08x} from {} handled in {}msec", + M::KIND, + hex::encode(remote), + (end_time - begin_time).as_millis() + ); rmp_to_vec_all_named(&res).unwrap_or(vec![]) } @@ -291,8 +300,7 @@ impl NetApp { pub(crate) fn connected_as_server(&self, id: ed25519::PublicKey, conn: Arc<ServerConn>) { info!("Accepted connection from {}", hex::encode(id)); - let mut conn_list = self.server_conns.write().unwrap(); - conn_list.insert(id.clone(), conn); + self.server_conns.write().unwrap().insert(id, conn); } // Handle hello message from a client. This message is used for them to tell us @@ -319,10 +327,11 @@ impl NetApp { if let Some(c) = conn_list.get(id) { if Arc::ptr_eq(c, &conn) { conn_list.remove(id); - } + drop(conn_list); - if let Some(h) = self.on_disconnected_handler.load().as_ref() { - h(conn.peer_pk, true); + if let Some(h) = self.on_disconnected_handler.load().as_ref() { + h(conn.peer_pk, true); + } } } } @@ -338,8 +347,8 @@ impl NetApp { info!("Connection established to {}", hex::encode(id)); { - let mut conn_list = self.client_conns.write().unwrap(); - if let Some(old_c) = conn_list.insert(id.clone(), conn.clone()) { + let old_c_opt = self.client_conns.write().unwrap().insert(id, conn.clone()); + if let Some(old_c) = old_c_opt { tokio::spawn(async move { old_c.close() }); } } @@ -365,6 +374,7 @@ impl NetApp { if let Some(c) = conn_list.get(id) { if Arc::ptr_eq(c, &conn) { conn_list.remove(id); + drop(conn_list); if let Some(h) = self.on_disconnected_handler.load().as_ref() { h(conn.peer_pk, false); |