@@ -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
298315impl < 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