Skip to content

Commit ac16b2d

Browse files
f - remove peers through timer_tick_occurred feedback
1 parent 4aa42ae commit ac16b2d

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,38 +3336,6 @@ where
33363336
true
33373337
}
33383338

3339-
/// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3340-
/// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
3341-
/// to that peer is later closed while still being disconnected (i.e. force closed), we
3342-
/// therefore need to remove the peer from `peer_state` separately.
3343-
/// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
3344-
/// instead remove such peers awaiting removal through this function, which is called on a
3345-
/// timer through `timer_tick_occurred`, passing the peers disconnected peers with no channels,
3346-
/// to limit the negative effects on parallelism as much as possible.
3347-
///
3348-
/// Must be called without the `per_peer_state` lock acquired.
3349-
fn remove_peers_awaiting_removal(&self, pending_peers_awaiting_removal: HashSet<PublicKey>) {
3350-
if pending_peers_awaiting_removal.len() > 0 {
3351-
let mut per_peer_state = self.per_peer_state.write().unwrap();
3352-
for counterparty_node_id in pending_peers_awaiting_removal {
3353-
match per_peer_state.entry(counterparty_node_id) {
3354-
hash_map::Entry::Occupied(entry) => {
3355-
// Remove the entry if the peer is still disconnected and we still
3356-
// have no channels to the peer.
3357-
let remove_entry = {
3358-
let peer_state = entry.get().lock().unwrap();
3359-
!peer_state.is_connected && peer_state.channel_by_id.len() == 0
3360-
};
3361-
if remove_entry {
3362-
entry.remove_entry();
3363-
}
3364-
},
3365-
hash_map::Entry::Vacant(_) => { /* The PeerState has already been removed */ }
3366-
}
3367-
}
3368-
}
3369-
}
3370-
33713339
#[cfg(any(test, feature = "_test_utils"))]
33723340
/// Process background events, for functional testing
33733341
pub fn test_process_background_events(&self) {
@@ -3441,7 +3409,7 @@ where
34413409

34423410
let mut handle_errors: Vec<(Result<(), _>, _)> = Vec::new();
34433411
let mut timed_out_mpp_htlcs = Vec::new();
3444-
let mut pending_peers_awaiting_removal = HashSet::new();
3412+
let mut pending_peers_awaiting_removal = Vec::new();
34453413
{
34463414
let per_peer_state = self.per_peer_state.read().unwrap();
34473415
for (counterparty_node_id, peer_state_mutex) in per_peer_state.iter() {
@@ -3491,11 +3459,37 @@ where
34913459
});
34923460
let peer_should_be_removed = !peer_state.is_connected && peer_state.channel_by_id.len() == 0;
34933461
if peer_should_be_removed {
3494-
pending_peers_awaiting_removal.insert(counterparty_node_id);
3462+
pending_peers_awaiting_removal.push(counterparty_node_id);
3463+
}
3464+
}
3465+
}
3466+
3467+
// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3468+
// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels
3469+
// of to that peer is later closed while still being disconnected (i.e. force closed),
3470+
// we therefore need to remove the peer from `peer_state` separately.
3471+
// To avoid having to take the `per_peer_state` `write` lock once the channels are
3472+
// closed, we instead remove such peers awaiting removal here on a timer, to limit the
3473+
// negative effects on parallelism as much as possible.
3474+
if pending_peers_awaiting_removal.len() > 0 {
3475+
let mut per_peer_state = self.per_peer_state.write().unwrap();
3476+
for counterparty_node_id in pending_peers_awaiting_removal {
3477+
match per_peer_state.entry(counterparty_node_id) {
3478+
hash_map::Entry::Occupied(entry) => {
3479+
// Remove the entry if the peer is still disconnected and we still
3480+
// have no channels to the peer.
3481+
let remove_entry = {
3482+
let peer_state = entry.get().lock().unwrap();
3483+
!peer_state.is_connected && peer_state.channel_by_id.len() == 0
3484+
};
3485+
if remove_entry {
3486+
entry.remove_entry();
3487+
}
3488+
},
3489+
hash_map::Entry::Vacant(_) => { /* The PeerState has already been removed */ }
34953490
}
34963491
}
34973492
}
3498-
self.remove_peers_awaiting_removal(pending_peers_awaiting_removal);
34993493

35003494
self.claimable_payments.lock().unwrap().claimable_htlcs.retain(|payment_hash, (_, htlcs)| {
35013495
if htlcs.is_empty() {

0 commit comments

Comments
 (0)