Call on_disconnected_handler earlier (as soon as .disconnect() is called)

This commit is contained in:
Alex 2020-12-07 12:39:19 +01:00
parent b247f02c29
commit 83789a3076

View file

@ -238,26 +238,29 @@ impl NetApp {
/// Close the outgoing connection we have to a node specified by its public key, /// Close the outgoing connection we have to a node specified by its public key,
/// if such a connection is currently open. /// if such a connection is currently open.
pub fn disconnect(self: &Arc<Self>, pk: &ed25519::PublicKey) { pub fn disconnect(self: &Arc<Self>, pk: &ed25519::PublicKey) {
// Don't disconnect from ourself (we aren't connected anyways) // If pk is ourself, we're not supposed to have a connection open
// but pretend we did if *pk != self.pubkey {
if *pk == self.pubkey { let conn = self.client_conns.read().unwrap().remove(pk);
let pk = *pk; if let Some(c) = conn {
let self2 = self.clone(); debug!("Closing connection to {} ({})",
tokio::spawn(async move { hex::encode(c.peer_pk),
if let Some(h) = self2.on_disconnected_handler.load().as_ref() { c.remote_addr);
h(pk, false); c.close();
} } else {
}); return;
return; }
} }
let conn = self.client_conns.read().unwrap().get(pk).cloned(); // call on_disconnected_handler immediately, since the connection
if let Some(c) = conn { // was removed
debug!("Closing connection to {} ({})", // (if pk == self.pubkey, we pretend we disconnected)
hex::encode(c.peer_pk), let pk = *pk;
c.remote_addr); let self2 = self.clone();
c.close(); 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, /// 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 /// Send a message to a remote host to which a client connection is already