Skip to content

Commit 151d4ac

Browse files
authored
Merge pull request #783 from ariard/2021-01-disconnect-node-id
Add PeerManager::disconnect_node_id()
2 parents f151c02 + 5b7d7ed commit 151d4ac

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,24 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
11791179
};
11801180
}
11811181

1182+
/// Disconnect a peer given its node id.
1183+
///
1184+
/// Set no_connection_possible to true to prevent any further connection with this peer,
1185+
/// force-closing any channels we have with it.
1186+
///
1187+
/// If a peer is connected, this will call `disconnect_socket` on the descriptor for the peer,
1188+
/// so be careful about reentrancy issues.
1189+
pub fn disconnect_by_node_id(&self, node_id: PublicKey, no_connection_possible: bool) {
1190+
let mut peers_lock = self.peers.lock().unwrap();
1191+
if let Some(mut descriptor) = peers_lock.node_id_to_descriptor.remove(&node_id) {
1192+
log_trace!(self.logger, "Disconnecting peer with id {} due to client request", node_id);
1193+
peers_lock.peers.remove(&descriptor);
1194+
peers_lock.peers_needing_send.remove(&descriptor);
1195+
self.message_handler.chan_handler.peer_disconnected(&node_id, no_connection_possible);
1196+
descriptor.disconnect_socket();
1197+
}
1198+
}
1199+
11821200
/// This function should be called roughly once every 30 seconds.
11831201
/// It will send pings to each peer and disconnect those which did not respond to the last round of pings.
11841202

0 commit comments

Comments
 (0)