From dc9edbb9769c52ac0081afddd5cb99bfc2d15b3f Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Mon, 26 Jul 2021 11:03:43 +0100 Subject: [PATCH] Create ChannelCore.removeHandlers(pipeline:) and use it (#1916) Motivation: A while back we added a ChannelCore extension method that would allow removing handlers from a channel pipeline, in order to implement Channels outside of NIO. This extension worked well, but it did assume that you had a Channel to give it. There's no reason it needed a Channel instead of just a ChannelPipeline, but it was based on the assumption that Self would conform to Channel. Of course, this is usually true, but it turns out not to always be true, and EmbeddedChannel is one example where the Channel and ChannelCore are separate. To that end, we shouldn't require using the Channel here, just the ChannelPipeline. This will also let us force our NIO channels onto the public API, which will make migrating to NIOCore easier. Modifications: - Deprecate ChannelCore.removeHandlers(channel:) - Implement ChannelCore.removeHandlers(pipeline:) - Swap existing Channel implementations to use the new method. Result: More consistent usage of our APIs. Co-authored-by: George Barnett --- Sources/NIO/BaseSocketChannel.swift | 2 +- Sources/NIO/Channel.swift | 16 +++++++++++++++- Sources/NIO/Embedded.swift | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Sources/NIO/BaseSocketChannel.swift b/Sources/NIO/BaseSocketChannel.swift index d44d631f8..af1bdac07 100644 --- a/Sources/NIO/BaseSocketChannel.swift +++ b/Sources/NIO/BaseSocketChannel.swift @@ -842,7 +842,7 @@ class BaseSocketChannel: SelectableChannel, Chan eventLoop.execute { // ensure this is executed in a delayed fashion as the users code may still traverse the pipeline - self.pipeline.removeHandlers() + self.removeHandlers(pipeline: self.pipeline) self.closePromise.succeed(()) diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index 4708c84d0..076bb20d8 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -310,8 +310,22 @@ extension ChannelCore { /// /// - parameters: /// - channel: The `Channel` whose `ChannelPipeline` will be closed. + @available(*, deprecated, renamed: "removeHandlers(pipeline:)") public func removeHandlers(channel: Channel) { - channel.pipeline.removeHandlers() + self.removeHandlers(pipeline: channel.pipeline) + } + + /// Removes the `ChannelHandler`s from the `ChannelPipeline` `pipeline`, and + /// closes that `ChannelPipeline`. + /// + /// This method is intended for use when writing custom `ChannelCore` implementations. + /// This can be called from `close0` to tear down the `ChannelPipeline` when closure is + /// complete. + /// + /// - parameters: + /// - pipeline: The `ChannelPipline` to be closed. + public func removeHandlers(pipeline: ChannelPipeline) { + pipeline.removeHandlers() } } diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 335db3739..359b551c5 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -281,7 +281,7 @@ class EmbeddedChannelCore: ChannelCore { eventLoop.execute { // ensure this is executed in a delayed fashion as the users code may still traverse the pipeline - self.pipeline.removeHandlers() + self.removeHandlers(pipeline: self.pipeline) self.closePromise.succeed(()) } }