Commit 5d232ad0 authored by Nathan Harris's avatar Nathan Harris

Change `Redis.makeDefaultClientBootstrap` to `ClientBootstrap.makeRedisTCPClient`

Motivation:

During proposal review, feedback was provided that the discoverability of the factory method for building a standard RESP `ChannelPipeline` was poor outside of documentation.

Modifications:

- Move `Redis.makeDefaultClientBootstrap` to `ClientBootstrap.makeRedisTCPClient`.
- Move the `channelInitializer` implementation into a new `Channel.addBaseRedisHandlers()` instance method.

Result:

Users should have an easier time discovering how to easily create baseline RESP `ChannelPipelines`.

This contributes to #47.
parent dc0b7e8b
......@@ -36,3 +36,37 @@ extension EventLoopFuture where Value == RESPValue {
}
}
}
extension Channel {
/// Adds the baseline `ChannelHandlers` needed to support sending and receiving messages in Redis Serialization Protocol (RESP) format to the pipeline.
///
/// For implementation details, see `RedisMessageEncoder`, `RedisByteDecoder`, and `RedisCommandHandler`.
/// - Returns: An `EventLoopFuture` that resolves after all handlers have been added to the pipeline.
public func addBaseRedisHandlers() -> EventLoopFuture<Void> {
let handlers: [(ChannelHandler, name: String)] = [
(MessageToByteHandler(RedisMessageEncoder()), "RedisNIO.Outgoing"),
(ByteToMessageHandler(RedisByteDecoder()), "RedisNIO.Incoming"),
(RedisCommandHandler(), "RedisNIO.CommandQueue")
]
return .andAllSucceed(
handlers.map { self.pipeline.addHandler($0, name: $1) },
on: self.eventLoop
)
}
}
extension ClientBootstrap {
/// Makes a new `ClientBootstrap` instance with a baseline Redis `Channel` pipeline
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
///
/// For implementation details, see `RedisMessageEncoder`, `RedisByteDecoder`, and `RedisCommandHandler`.
/// - Parameter group: The `EventLoopGroup` to create the `ClientBootstrap` with.
/// - Returns: A `ClientBootstrap` with the base configuration of a `Channel` pipeline for RESP messages.
public static func makeRedisTCPClient(group: EventLoopGroup) -> ClientBootstrap {
return ClientBootstrap(group: group)
.channelOption(
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
value: 1
)
.channelInitializer { $0.addBaseRedisHandlers() }
}
}
......@@ -21,35 +21,6 @@ import NIO
/// are scoped within this namespace.
public enum Redis { }
// MARK: ClientBootstrap
extension Redis {
/// Makes a new `ClientBootstrap` instance with a default Redis `Channel` pipeline
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
///
/// See `RedisMessageEncoder`, `RedisByteDecoder`, and `RedisCommandHandler`.
/// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on.
/// - Returns: A `ClientBootstrap` with the default configuration of a `Channel` pipeline for RESP messages.
public static func makeDefaultClientBootstrap(using group: EventLoopGroup) -> ClientBootstrap {
return ClientBootstrap(group: group)
.channelOption(
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
value: 1
)
.channelInitializer { channel in
let handlers: [(ChannelHandler, String)] = [
(MessageToByteHandler(RedisMessageEncoder()), "RedisNIO.Outgoing"),
(ByteToMessageHandler(RedisByteDecoder()), "RedisNIO.Incoming"),
(RedisCommandHandler(), "RedisNIO.Queue")
]
return .andAllSucceed(
handlers.map { channel.pipeline.addHandler($0, name: $1) },
on: group.next()
)
}
}
}
// MARK: Connection Factory
extension Redis {
......@@ -81,9 +52,9 @@ extension Redis {
password: String? = nil,
logger: Logger = Logger(label: "RedisNIO.RedisConnection")
) -> EventLoopFuture<RedisConnection> {
let bootstrap = makeDefaultClientBootstrap(using: group)
let client = ClientBootstrap.makeRedisTCPClient(group: group)
return bootstrap.connect(to: socket)
return client.connect(to: socket)
.map { return RedisConnection(channel: $0, logger: logger) }
.flatMap { client in
guard let pw = password else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment