ChannelPipeline: addHandler and removeHandler instead of add/remove (#817)

Motivation:

- `ChannelPipeline.add(name:handler:...)` had a strange order of arguments
- `remove(handler:)` and `remove(ctx:)` both remove `ChannelHandler`s
  but they read like they remove different things

So let's just fix the argument order and name them `addHandler` and
`removeHandler` making clear what they do.

Modifications:

- rename all `ChannelPipeline.add(name:handler:...)`s to `ChannelPipeline.addHandler(_:name:...)`
- rename all `ChannelPipeline.remove(...)`s to `ChannelPipeline.removeHandler(...)`

Result:

more readable and consistent code
This commit is contained in:
Johannes Weiss authored and GitHub committed 2019-02-21 11:46:54 +00:00
1 parent 5d27e59bd9
commit 46ffd630de
50 files changed
+539 -425

No files matched your search

+7 -8
View File
@@ -24,12 +24,12 @@ extension ChannelPipeline {
/// Configure a `ChannelPipeline` for use as a HTTP client.
///
/// - parameters:
/// - first: Whether to add the HTTP client at the head of the channel pipeline,
/// or at the tail.
/// - position: The position in the `ChannelPipeline` where to add the HTTP client handlers. Defaults to `.last`.
/// - returns: An `EventLoopFuture` that will fire when the pipeline is configured.
public func addHTTPClientHandlers(first: Bool = false,
public func addHTTPClientHandlers(position: ChannelPipeline.Position = .last,
leftOverBytesStrategy: RemoveAfterUpgradeStrategy = .dropBytes) -> EventLoopFuture<Void> {
return addHandlers(HTTPRequestEncoder(), HTTPResponseDecoder(leftOverBytesStrategy: leftOverBytesStrategy), first: first)
return addHandlers(HTTPRequestEncoder(), HTTPResponseDecoder(leftOverBytesStrategy: leftOverBytesStrategy),
position: position)
}
/// Configure a `ChannelPipeline` for use as a HTTP server.
@@ -45,8 +45,7 @@ extension ChannelPipeline {
/// features.
///
/// - parameters:
/// - first: Whether to add the HTTP server at the head of the channel pipeline,
/// or at the tail.
/// - position: Where in the pipeline to add the HTTP server handlers, defaults to `.last`.
/// - pipelining: Whether to provide assistance handling HTTP clients that pipeline
/// their requests. Defaults to `true`. If `false`, users will need to handle
/// clients that pipeline themselves.
@@ -58,7 +57,7 @@ extension ChannelPipeline {
/// - errorHandling: Whether to provide assistance handling protocol errors (e.g.
/// failure to parse the HTTP request) by sending 400 errors. Defaults to `true`.
/// - returns: An `EventLoopFuture` that will fire when the pipeline is configured.
public func configureHTTPServerPipeline(first: Bool = false,
public func configureHTTPServerPipeline(position: ChannelPipeline.Position = .last,
withPipeliningAssistance pipelining: Bool = true,
withServerUpgrade upgrade: HTTPUpgradeConfiguration? = nil,
withErrorHandling errorHandling: Bool = true) -> EventLoopFuture<Void> {
@@ -83,7 +82,7 @@ extension ChannelPipeline {
handlers.append(upgrader)
}
return self.addHandlers(handlers, first: first)
return self.addHandlers(handlers, position: position)
}
}
+4 -4
View File
@@ -207,7 +207,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler, RemovableChannelHa
ctx.fireChannelReadComplete()
}
}.whenComplete { (_: Result<Void, Error>) in
ctx.pipeline.remove(ctx: ctx, promise: nil)
ctx.pipeline.removeHandler(ctx: ctx, promise: nil)
}
}
}
@@ -226,7 +226,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler, RemovableChannelHa
private func notUpgrading(ctx: ChannelHandlerContext, data: NIOAny) {
assert(self.receivedMessages.count == 0)
ctx.fireChannelRead(data)
ctx.pipeline.remove(ctx: ctx, promise: nil)
ctx.pipeline.removeHandler(ctx: ctx, promise: nil)
}
/// Builds the initial mandatory HTTP headers for HTTP ugprade responses.
@@ -237,7 +237,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler, RemovableChannelHa
/// Removes the given channel handler from the channel pipeline.
private func removeHandler(ctx: ChannelHandlerContext, handler: RemovableChannelHandler?) -> EventLoopFuture<Void> {
if let handler = handler {
return ctx.pipeline.remove(handler: handler)
return ctx.pipeline.removeHandler(handler)
} else {
return ctx.eventLoop.makeSucceededFuture(())
}
@@ -249,7 +249,7 @@ public class HTTPServerUpgradeHandler: ChannelInboundHandler, RemovableChannelHa
return ctx.eventLoop.makeSucceededFuture(())
}
return .andAllSucceed(self.extraHTTPHandlers.map { ctx.pipeline.remove(handler: $0) },
return .andAllSucceed(self.extraHTTPHandlers.map { ctx.pipeline.removeHandler($0) },
on: ctx.eventLoop)
}
}