diff --git a/Sources/NIORedis/Extensions/NIO/ClientBootstrap.swift b/Sources/NIORedis/Extensions/NIO/ClientBootstrap.swift new file mode 100644 index 0000000..4097156 --- /dev/null +++ b/Sources/NIORedis/Extensions/NIO/ClientBootstrap.swift @@ -0,0 +1,26 @@ +import Foundation +import NIO + +extension ClientBootstrap { + /// Makes a new `ClientBootstrap` instance with a standard Redis `Channel` pipeline for sending and receiving + /// messages in Redis Serialization Protocol (RESP) format. + /// + /// See `RESPEncoder`, `RESPDecoder`, and `RedisCommadHandler`. + /// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on. + /// - Returns: A `ClientBootstrap` with the standard configuration of a `Channel` pipeline for RESP messages. + public static func makeForRedis(using group: EventLoopGroup) -> ClientBootstrap { + return ClientBootstrap(group: group) + .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) + .channelInitializer { channel in + let handlers: [ChannelHandler] = [ + RESPEncoder(), + ByteToMessageHandler(RESPDecoder()), + RedisCommandHandler() + ] + return EventLoopFuture.andAll( + handlers.map { channel.pipeline.add(handler: $0) }, + eventLoop: group.next() + ) + } + } +} diff --git a/Sources/NIORedis/RedisDriver.swift b/Sources/NIORedis/RedisDriver.swift index 86cb9f0..cf8bd24 100644 --- a/Sources/NIORedis/RedisDriver.swift +++ b/Sources/NIORedis/RedisDriver.swift @@ -52,15 +52,7 @@ public final class RedisDriver { port: Int = 6379, password: String? = nil ) -> EventLoopFuture { - let bootstrap = ClientBootstrap(group: eventLoopGroup) - .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) - .channelInitializer { channel in - return channel.pipeline.addHandlers( - RESPEncoder(), - ByteToMessageHandler(RESPDecoder()), - RedisCommandHandler() - ) - } + let bootstrap = ClientBootstrap.makeForRedis(using: eventLoopGroup) return bootstrap.connect(host: hostname, port: port) .map { return RedisConnection(channel: $0) }