diff options
author | Alex Auvolat <alex@adnab.me> | 2020-12-07 12:39:19 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-12-07 12:39:34 +0100 |
commit | 83789a3076e986782af60ba32b0398414c1c82d7 (patch) | |
tree | 15f3939483129c4d5e6523b476134a92e5ab55c2 | |
parent | b247f02c2907def0b569c958a589754dddc31012 (diff) | |
download | netapp-83789a3076e986782af60ba32b0398414c1c82d7.tar.gz netapp-83789a3076e986782af60ba32b0398414c1c82d7.zip |
Call on_disconnected_handler earlier (as soon as .disconnect() is called)
-rw-r--r-- | src/netapp.rs | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/src/netapp.rs b/src/netapp.rs index e903f44..bf9a3f0 100644 --- a/src/netapp.rs +++ b/src/netapp.rs @@ -238,26 +238,29 @@ impl NetApp { /// Close the outgoing connection we have to a node specified by its public key, /// if such a connection is currently open. pub fn disconnect(self: &Arc<Self>, pk: &ed25519::PublicKey) { - // Don't disconnect from ourself (we aren't connected anyways) - // but pretend we did - if *pk == self.pubkey { - let pk = *pk; - let self2 = self.clone(); - tokio::spawn(async move { - if let Some(h) = self2.on_disconnected_handler.load().as_ref() { - h(pk, false); - } - }); - return; + // If pk is ourself, we're not supposed to have a connection open + if *pk != self.pubkey { + let conn = self.client_conns.read().unwrap().remove(pk); + if let Some(c) = conn { + debug!("Closing connection to {} ({})", + hex::encode(c.peer_pk), + c.remote_addr); + c.close(); + } else { + return; + } } - let conn = self.client_conns.read().unwrap().get(pk).cloned(); - if let Some(c) = conn { - debug!("Closing connection to {} ({})", - hex::encode(c.peer_pk), - c.remote_addr); - c.close(); - } + // call on_disconnected_handler immediately, since the connection + // was removed + // (if pk == self.pubkey, we pretend we disconnected) + let pk = *pk; + let self2 = self.clone(); + tokio::spawn(async move { + if let Some(h) = self2.on_disconnected_handler.load().as_ref() { + h(pk, false); + } + }); } /// Close the incoming connection from a certain client to us, @@ -360,6 +363,8 @@ impl NetApp { } } } + // else case: happens if connection was removed in .disconnect() + // in which case on_disconnected_handler was already called } /// Send a message to a remote host to which a client connection is already |