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,9 +238,22 @@ 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 {
// 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;
}
}
// 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 {
@ -248,16 +261,6 @@ impl NetApp {
h(pk, false);
}
});
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();
}
}
/// 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