Make public factory method for "standard" NIORedis ClientBootstrap

This commit is contained in:
Nathan Harris
2019-01-17 16:10:59 -08:00
parent 1e160b0680
commit e25db7dcfd
2 changed files with 27 additions and 9 deletions
@@ -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<Void>.andAll(
handlers.map { channel.pipeline.add(handler: $0) },
eventLoop: group.next()
)
}
}
}
+1 -9
View File
@@ -52,15 +52,7 @@ public final class RedisDriver {
port: Int = 6379,
password: String? = nil
) -> EventLoopFuture<RedisConnection> {
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) }