Skip to content

Commit dc9edbb

Browse files
Lukasaglbrntt
andauthored
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 <[email protected]>
1 parent f9b8f15 commit dc9edbb

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Sources/NIO/BaseSocketChannel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ class BaseSocketChannel<SocketType: BaseSocketProtocol>: SelectableChannel, Chan
842842

843843
eventLoop.execute {
844844
// ensure this is executed in a delayed fashion as the users code may still traverse the pipeline
845-
self.pipeline.removeHandlers()
845+
self.removeHandlers(pipeline: self.pipeline)
846846

847847
self.closePromise.succeed(())
848848

Sources/NIO/Channel.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,22 @@ extension ChannelCore {
310310
///
311311
/// - parameters:
312312
/// - channel: The `Channel` whose `ChannelPipeline` will be closed.
313+
@available(*, deprecated, renamed: "removeHandlers(pipeline:)")
313314
public func removeHandlers(channel: Channel) {
314-
channel.pipeline.removeHandlers()
315+
self.removeHandlers(pipeline: channel.pipeline)
316+
}
317+
318+
/// Removes the `ChannelHandler`s from the `ChannelPipeline` `pipeline`, and
319+
/// closes that `ChannelPipeline`.
320+
///
321+
/// This method is intended for use when writing custom `ChannelCore` implementations.
322+
/// This can be called from `close0` to tear down the `ChannelPipeline` when closure is
323+
/// complete.
324+
///
325+
/// - parameters:
326+
/// - pipeline: The `ChannelPipline` to be closed.
327+
public func removeHandlers(pipeline: ChannelPipeline) {
328+
pipeline.removeHandlers()
315329
}
316330
}
317331

Sources/NIO/Embedded.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ class EmbeddedChannelCore: ChannelCore {
281281

282282
eventLoop.execute {
283283
// ensure this is executed in a delayed fashion as the users code may still traverse the pipeline
284-
self.pipeline.removeHandlers()
284+
self.removeHandlers(pipeline: self.pipeline)
285285
self.closePromise.succeed(())
286286
}
287287
}

0 commit comments

Comments
 (0)