Skip to content

Removing the handler field from FromSwarm::ConnectionClosed in favor of ConnectionHandler::poll_close #3046

@thomaseizinger

Description

@thomaseizinger

Currently, FromSwarm::ConnectionClosed contains a handler field which makes handling the event klunky in many ways:

  • It cannot implement Clone
  • It adds a type-parameter
  • We cannot unify SwarmEvent and FromSwarm despite them containing the same data

The usecase for returning the handler is to allow the behaviour to extract data from a closed connection which would other get lost as connections can close at any point. Nothing is currently using this functionality but it makes sense in general so we want to keep it.

We can solve this by adding a poll_close function as described by @mxinden below: #3046 (comment)

modified   swarm/src/handler.rs
@@ -158,6 +158,15 @@ pub trait ConnectionHandler: Send + 'static {
         >,
     >;
 
+    /// Gracefully close the [`ConnectionHandler`].
+    ///
+    /// Implementations may transfer one or more events to their [`NetworkBehaviour`] implementation
+    /// by emitting [`Poll::Ready`] with [`Self::ToBehaviour`]. Implementations should eventually
+    /// return [`Poll::Ready(None)`] to signal successful closing.
+    fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::ToBehaviour>> {
+        Poll::Ready(None)
+    }
+
     /// Adds a closure that turns the input event into something else.
     fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap>
     where

Upon closing a connection, poll_close would be called until it returns Ready(None) and all returned events would be delivered to the behaviour.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions