Skip to content

Commit e25db7d

Browse files
committed
Make public factory method for "standard" NIORedis ClientBootstrap
1 parent 1e160b0 commit e25db7d

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
import NIO
3+
4+
extension ClientBootstrap {
5+
/// Makes a new `ClientBootstrap` instance with a standard Redis `Channel` pipeline for sending and receiving
6+
/// messages in Redis Serialization Protocol (RESP) format.
7+
///
8+
/// See `RESPEncoder`, `RESPDecoder`, and `RedisCommadHandler`.
9+
/// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on.
10+
/// - Returns: A `ClientBootstrap` with the standard configuration of a `Channel` pipeline for RESP messages.
11+
public static func makeForRedis(using group: EventLoopGroup) -> ClientBootstrap {
12+
return ClientBootstrap(group: group)
13+
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
14+
.channelInitializer { channel in
15+
let handlers: [ChannelHandler] = [
16+
RESPEncoder(),
17+
ByteToMessageHandler(RESPDecoder()),
18+
RedisCommandHandler()
19+
]
20+
return EventLoopFuture<Void>.andAll(
21+
handlers.map { channel.pipeline.add(handler: $0) },
22+
eventLoop: group.next()
23+
)
24+
}
25+
}
26+
}

Sources/NIORedis/RedisDriver.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,7 @@ public final class RedisDriver {
5252
port: Int = 6379,
5353
password: String? = nil
5454
) -> EventLoopFuture<RedisConnection> {
55-
let bootstrap = ClientBootstrap(group: eventLoopGroup)
56-
.channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
57-
.channelInitializer { channel in
58-
return channel.pipeline.addHandlers(
59-
RESPEncoder(),
60-
ByteToMessageHandler(RESPDecoder()),
61-
RedisCommandHandler()
62-
)
63-
}
55+
let bootstrap = ClientBootstrap.makeForRedis(using: eventLoopGroup)
6456

6557
return bootstrap.connect(host: hostname, port: port)
6658
.map { return RedisConnection(channel: $0) }

0 commit comments

Comments
 (0)