Skip to content

Commit c5241a4

Browse files
committed
Merge branch 'client-bootstrap' into '47-proposal-feedback'
Change `Redis.makeDefaultClientBootstrap` to `ClientBootstrap.makeRedisTCPClient` See merge request Mordil/swift-redis-nio-client!64
2 parents dc0b7e8 + 5d232ad commit c5241a4

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

Sources/RedisNIO/Extensions/SwiftNIO.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,37 @@ extension EventLoopFuture where Value == RESPValue {
3636
}
3737
}
3838
}
39+
40+
extension Channel {
41+
/// Adds the baseline `ChannelHandlers` needed to support sending and receiving messages in Redis Serialization Protocol (RESP) format to the pipeline.
42+
///
43+
/// For implementation details, see `RedisMessageEncoder`, `RedisByteDecoder`, and `RedisCommandHandler`.
44+
/// - Returns: An `EventLoopFuture` that resolves after all handlers have been added to the pipeline.
45+
public func addBaseRedisHandlers() -> EventLoopFuture<Void> {
46+
let handlers: [(ChannelHandler, name: String)] = [
47+
(MessageToByteHandler(RedisMessageEncoder()), "RedisNIO.Outgoing"),
48+
(ByteToMessageHandler(RedisByteDecoder()), "RedisNIO.Incoming"),
49+
(RedisCommandHandler(), "RedisNIO.CommandQueue")
50+
]
51+
return .andAllSucceed(
52+
handlers.map { self.pipeline.addHandler($0, name: $1) },
53+
on: self.eventLoop
54+
)
55+
}
56+
}
57+
extension ClientBootstrap {
58+
/// Makes a new `ClientBootstrap` instance with a baseline Redis `Channel` pipeline
59+
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
60+
///
61+
/// For implementation details, see `RedisMessageEncoder`, `RedisByteDecoder`, and `RedisCommandHandler`.
62+
/// - Parameter group: The `EventLoopGroup` to create the `ClientBootstrap` with.
63+
/// - Returns: A `ClientBootstrap` with the base configuration of a `Channel` pipeline for RESP messages.
64+
public static func makeRedisTCPClient(group: EventLoopGroup) -> ClientBootstrap {
65+
return ClientBootstrap(group: group)
66+
.channelOption(
67+
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
68+
value: 1
69+
)
70+
.channelInitializer { $0.addBaseRedisHandlers() }
71+
}
72+
}

Sources/RedisNIO/Redis.swift

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,6 @@ import NIO
2121
/// are scoped within this namespace.
2222
public enum Redis { }
2323

24-
// MARK: ClientBootstrap
25-
26-
extension Redis {
27-
/// Makes a new `ClientBootstrap` instance with a default Redis `Channel` pipeline
28-
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
29-
///
30-
/// See `RedisMessageEncoder`, `RedisByteDecoder`, and `RedisCommandHandler`.
31-
/// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on.
32-
/// - Returns: A `ClientBootstrap` with the default configuration of a `Channel` pipeline for RESP messages.
33-
public static func makeDefaultClientBootstrap(using group: EventLoopGroup) -> ClientBootstrap {
34-
return ClientBootstrap(group: group)
35-
.channelOption(
36-
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
37-
value: 1
38-
)
39-
.channelInitializer { channel in
40-
let handlers: [(ChannelHandler, String)] = [
41-
(MessageToByteHandler(RedisMessageEncoder()), "RedisNIO.Outgoing"),
42-
(ByteToMessageHandler(RedisByteDecoder()), "RedisNIO.Incoming"),
43-
(RedisCommandHandler(), "RedisNIO.Queue")
44-
]
45-
return .andAllSucceed(
46-
handlers.map { channel.pipeline.addHandler($0, name: $1) },
47-
on: group.next()
48-
)
49-
}
50-
}
51-
}
52-
5324
// MARK: Connection Factory
5425

5526
extension Redis {
@@ -81,9 +52,9 @@ extension Redis {
8152
password: String? = nil,
8253
logger: Logger = Logger(label: "RedisNIO.RedisConnection")
8354
) -> EventLoopFuture<RedisConnection> {
84-
let bootstrap = makeDefaultClientBootstrap(using: group)
55+
let client = ClientBootstrap.makeRedisTCPClient(group: group)
8556

86-
return bootstrap.connect(to: socket)
57+
return client.connect(to: socket)
8758
.map { return RedisConnection(channel: $0, logger: logger) }
8859
.flatMap { client in
8960
guard let pw = password else {

0 commit comments

Comments
 (0)