From 06dab4e04bbd8c9063ec74e87501c6fc01b78035 Mon Sep 17 00:00:00 2001 From: Joe Smith Date: Wed, 11 Dec 2019 04:06:11 -0800 Subject: [PATCH] Create a ClientBootstrap protocol (#1253) This allows other EventLoopGroup implementations to swap in other bootstrap implementations. By default, it is unimplemented and will throw an error once `connect` is called on the returned Bootstrap. Support is implemented for `MultiThreadedEventLoopGroup`, `SelectableEventLoop` and `EmbeddedEventLoop`. --- Sources/NIO/Bootstrap.swift | 36 +++++++++++++++- Sources/NIO/Embedded.swift | 5 +++ Sources/NIO/EventLoop.swift | 51 +++++++++++++++++++++++ Tests/NIOTests/BootstrapTest+XCTest.swift | 1 + Tests/NIOTests/BootstrapTest.swift | 51 ++++++++++++++++++++--- 5 files changed, 138 insertions(+), 6 deletions(-) diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index bbfd02ce2..0f4639201 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -330,6 +330,40 @@ private extension Channel { } } +/// `NIOTCPClientBootstrap` is implemented by various underlying transport mechanisms. Typically, +/// this will be the BSD Sockets API implemented by `ClientBootstrap`. +internal protocol NIOTCPClientBootstrap { + /// Initialize the connected `Channel` with `initializer`. The most common task in initializer is to add + /// `ChannelHandler`s to the `ChannelPipeline`. + /// + /// - parameters: + /// - handler: A closure that initializes the provided `Channel`. + func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture) -> Self + + /// Specifies a `ChannelOption` to be applied to the `Channel`. + /// + /// - parameters: + /// - option: The option to be applied. + /// - value: The value for the option. + @inlinable + func channelOption(_ option: Option, value: Option.Value) -> Self + + /// Specifies a timeout to apply to a connection attempt. + // + /// - parameters: + /// - timeout: The timeout that will apply to the connection attempt. + func connectTimeout(_ timeout: TimeAmount) -> Self + + /// Specify the `host` and `port` to connect to for the `Channel` that will be established. + /// + /// - parameters: + /// - host: The host to connect to. + /// - port: The port to connect to. + /// - returns: An `EventLoopFuture` to deliver the `Channel` when connected. + func connect(host: String, port: Int) -> EventLoopFuture +} + + /// A `ClientBootstrap` is an easy way to bootstrap a `SocketChannel` when creating network clients. /// /// Usually you re-use a `ClientBootstrap` once you set it up and called `connect` multiple times on it. @@ -356,7 +390,7 @@ private extension Channel { /// ``` /// /// The connected `SocketChannel` will operate on `ByteBuffer` as inbound and on `IOData` as outbound messages. -public final class ClientBootstrap { +public final class ClientBootstrap: NIOTCPClientBootstrap { private let group: EventLoopGroup private var channelInitializer: Optional diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index d2de645d2..8cfd66843 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -60,6 +60,11 @@ public final class EmbeddedEventLoop: EventLoop { /// Initialize a new `EmbeddedEventLoop`. public init() { } + /// Provide a valid `ClientBootstrap` to setup an `EmbeddedEventLoop`. + internal func makeTCPClientBootstrap() -> NIOTCPClientBootstrap { + return ClientBootstrap(group: self) + } + /// - see: `EventLoop.scheduleTask(deadline:_:)` @discardableResult public func scheduleTask(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled { diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index b379b6dc5..e463f1e6b 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -744,6 +744,11 @@ internal final class SelectableEventLoop: EventLoop { _addresses.deallocate() } + /// Provide a valid `ClientBootstrap` to setup this `SelectableEventLoop` with a `SocketChannel`. + internal func makeTCPClientBootstrap() -> NIOTCPClientBootstrap { + return ClientBootstrap(group: self) + } + /// Is this `SelectableEventLoop` still open (ie. not shutting down or shut down) internal var isOpen: Bool { self.assertInEventLoop() @@ -1128,6 +1133,47 @@ extension EventLoopGroup { } } +/// The default, non-functional implementation used to encourage EventLoopGroup implementations to conform to `NIOTCPClientBootstrap`. +internal struct CannotBootstrap: NIOTCPClientBootstrap { + /// Thrown when an EventLoopGroup has not implemented `NIOTCPClientBootstrap` properly. + public struct CannotBootstrapError: Error { + let message: String + + public init(_ message: String) { + self.message = message + } + } + + let group: EventLoopGroup + + init(group: EventLoopGroup) { + self.group = group + } + + func channelInitializer(_ handler: @escaping (Channel) -> EventLoopFuture) -> CannotBootstrap { + return self + } + + func channelOption