Skip to content

Commit 4eb0659

Browse files
swarm/: Allow disconnecting from Swarm and NetworkBehaviour (#2110)
Add `ExpandedSwarm::disconnect_peer_id` and `NetworkBehaviourAction::CloseConnection` to close connections to a specific peer via an `ExpandedSwarm` or `NetworkBehaviour`. Co-authored-by: Max Inden <[email protected]>
1 parent f9491e7 commit 4eb0659

File tree

7 files changed

+376
-40
lines changed

7 files changed

+376
-40
lines changed

protocols/gossipsub/src/behaviour.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,9 @@ where
32023202
NetworkBehaviourAction::ReportObservedAddr { address, score } => {
32033203
NetworkBehaviourAction::ReportObservedAddr { address, score }
32043204
}
3205+
NetworkBehaviourAction::CloseConnection { peer_id, connection } => {
3206+
NetworkBehaviourAction::CloseConnection { peer_id, connection }
3207+
}
32053208
});
32063209
}
32073210

protocols/request-response/src/throttled.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,9 @@ where
657657
| NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
658658
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event },
659659
| NetworkBehaviourAction::ReportObservedAddr { address, score } =>
660-
NetworkBehaviourAction::ReportObservedAddr { address, score }
660+
NetworkBehaviourAction::ReportObservedAddr { address, score },
661+
| NetworkBehaviourAction::CloseConnection { peer_id, connection } =>
662+
NetworkBehaviourAction::CloseConnection { peer_id, connection }
661663
};
662664

663665
return Poll::Ready(event)

swarm-derive/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
479479
std::task::Poll::Ready(#network_behaviour_action::ReportObservedAddr { address, score }) => {
480480
return std::task::Poll::Ready(#network_behaviour_action::ReportObservedAddr { address, score });
481481
}
482+
std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection }) => {
483+
return std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection });
484+
}
482485
std::task::Poll::Pending => break,
483486
}
484487
}

swarm/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515

1616
See [PR 2100] for details.
1717

18+
- Add `ExpandedSwarm::disconnect_peer_id` and
19+
`NetworkBehaviourAction::CloseConnection` to close connections to a specific
20+
peer via an `ExpandedSwarm` or `NetworkBehaviour`. See [PR 2110] for details.
21+
1822
[PR 2100]: https://github.com/libp2p/rust-libp2p/pull/2100
23+
[PR 2110]: https://github.com/libp2p/rust-libp2p/pull/2110/
1924

2025
# 0.29.0 [2021-04-13]
2126

swarm/src/behaviour.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,23 @@ pub enum NetworkBehaviourAction<TInEvent, TOutEvent> {
293293
/// relative to other observed addresses.
294294
score: AddressScore,
295295
},
296+
297+
/// Instructs the `Swarm` to initiate a graceful close of one or all connections
298+
/// with the given peer.
299+
///
300+
/// Note: Closing a connection via
301+
/// [`NetworkBehaviourAction::CloseConnection`] does not inform the
302+
/// corresponding [`ProtocolsHandler`].
303+
/// Closing a connection via a [`ProtocolsHandler`] can be done
304+
/// either in a collaborative manner across [`ProtocolsHandler`]s
305+
/// with [`ProtocolsHandler::connection_keep_alive`] or directly with
306+
/// [`ProtocolsHandlerEvent::Close`](crate::ProtocolsHandlerEvent::Close).
307+
CloseConnection {
308+
/// The peer to disconnect.
309+
peer_id: PeerId,
310+
/// Whether to close a specific or all connections to the given peer.
311+
connection: CloseConnection,
312+
}
296313
}
297314

298315
impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
@@ -312,7 +329,9 @@ impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
312329
event: f(event)
313330
},
314331
NetworkBehaviourAction::ReportObservedAddr { address, score } =>
315-
NetworkBehaviourAction::ReportObservedAddr { address, score }
332+
NetworkBehaviourAction::ReportObservedAddr { address, score },
333+
NetworkBehaviourAction::CloseConnection { peer_id, connection } =>
334+
NetworkBehaviourAction::CloseConnection { peer_id, connection }
316335
}
317336
}
318337

@@ -328,7 +347,9 @@ impl<TInEvent, TOutEvent> NetworkBehaviourAction<TInEvent, TOutEvent> {
328347
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
329348
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event },
330349
NetworkBehaviourAction::ReportObservedAddr { address, score } =>
331-
NetworkBehaviourAction::ReportObservedAddr { address, score }
350+
NetworkBehaviourAction::ReportObservedAddr { address, score },
351+
NetworkBehaviourAction::CloseConnection { peer_id, connection } =>
352+
NetworkBehaviourAction::CloseConnection { peer_id, connection }
332353
}
333354
}
334355
}
@@ -373,3 +394,18 @@ impl Default for DialPeerCondition {
373394
DialPeerCondition::Disconnected
374395
}
375396
}
397+
398+
/// The options which connections to close.
399+
#[derive(Debug, Clone)]
400+
pub enum CloseConnection {
401+
/// Disconnect a particular connection.
402+
One(ConnectionId),
403+
/// Disconnect all connections.
404+
All,
405+
}
406+
407+
impl Default for CloseConnection {
408+
fn default() -> Self {
409+
CloseConnection::All
410+
}
411+
}

0 commit comments

Comments
 (0)