Update existing peer on InboundConnection
When handling a new InboundConnection, check to see
if there is already a promoted peer for the identity.
If there is replace the connection_id and active
endpoint for the connection. Also notify subscribers
status Connected.
@@ -615,6 +615,26 @@ fn handle_notifications(
"Received peer connection from {} (remote endpoint: {})",
identity, endpoint
);
+
+ // If we got an inbound counnection for an existing peer, replace old connection with
+ // this new one.
+ if let Some(mut peer_metadata) = peers.get_by_peer_id(&identity).cloned() {
+ peer_metadata.status = PeerStatus::Connected;
+ peer_metadata.connection_id = connection_id;
+
+ if let Err(err) = connector.remove_connection(&peer_metadata.active_endpoint) {
+ error!("Unable to clean up old connection: {}", err);
+ }
+ let notification = PeerManagerNotification::Connected {
+ peer: peer_metadata.id.to_string(),
+ };
+ subscribers.retain(|sender| sender.send(notification.clone()).is_ok());
+
+ peer_metadata.active_endpoint = endpoint;
+ if let Err(err) = peers.update_peer(peer_metadata) {
+ error!("Unable to update peer: {}", err);
+ }
+ } else {
unreferenced_peers.insert(
identity,
UnreferencedPeer {
@@ -623,6 +643,7 @@ fn handle_notifications(
},
);
}
+ }
ConnectionManagerNotification::Connected {
endpoint,
identity,
