Skip to content

Commit a8a1fa5

Browse files
authored
Merge pull request #38 from Mordil/namespacing
Namespace global static factory methods under `Redis`
2 parents d03823f + e446a83 commit a8a1fa5

12 files changed

+84
-65
lines changed

Sources/NIORedis/Extensions/NIO/ClientBootstrap.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

Sources/NIORedis/Redis.swift

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import struct Logging.Logger
2+
import NIO
3+
4+
/// Top-level namespace for the `NIORedis` package.
5+
///
6+
/// To avoid a cluttered global namespace, named definitions that do not start with a `Redis` prefix
7+
/// are scoped within this namespace.
8+
public enum Redis { }
9+
10+
// MARK: ClientBootstrap
11+
12+
extension Redis {
13+
/// Makes a new `ClientBootstrap` instance with a default Redis `Channel` pipeline
14+
/// for sending and receiving messages in Redis Serialization Protocol (RESP) format.
15+
///
16+
/// See `RESPEncoder`, `RESPDecoder`, and `CommandHandler`.
17+
/// - Parameter using: The `EventLoopGroup` to build the `ClientBootstrap` on.
18+
/// - Returns: A `ClientBootstrap` with the default configuration of a `Channel` pipeline for RESP messages.
19+
public static func makeDefaultClientBootstrap(using group: EventLoopGroup) -> ClientBootstrap {
20+
return ClientBootstrap(group: group)
21+
.channelOption(
22+
ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR),
23+
value: 1
24+
)
25+
.channelInitializer { $0.pipeline.addHandlers([
26+
MessageToByteHandler(RESPEncoder()),
27+
ByteToMessageHandler(RESPDecoder()),
28+
RedisCommandHandler()
29+
])}
30+
}
31+
}
32+
33+
// MARK: Connection Factory
34+
35+
extension Redis {
36+
/// Makes a new connection to a Redis instance.
37+
///
38+
/// Example:
39+
///
40+
/// let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
41+
/// let connection = Redis.makeConnection(
42+
/// to: .init(ipAddress: "127.0.0.1", port: 6379),
43+
/// using: elg
44+
/// )
45+
///
46+
/// - Parameters:
47+
/// - socket: The `SocketAddress` information of the Redis instance to connect to.
48+
/// - password: The optional password to authorize the client with.
49+
/// - eventLoopGroup: The `EventLoopGroup` to build the connection on.
50+
/// - logger: The `Logger` instance to log with.
51+
/// - Returns: A `RedisConnection` instance representing this new connection.
52+
public static func makeConnection(
53+
to socket: SocketAddress,
54+
using group: EventLoopGroup,
55+
with password: String? = nil,
56+
logger: Logger = Logger(label: "NIORedis.RedisConnection")
57+
) -> EventLoopFuture<RedisConnection> {
58+
let bootstrap = makeDefaultClientBootstrap(using: group)
59+
60+
return bootstrap.connect(to: socket)
61+
.map { return RedisConnection(channel: $0, logger: logger) }
62+
.flatMap { client in
63+
guard let pw = password else {
64+
return group.next().makeSucceededFuture(client)
65+
}
66+
67+
return client.send(command: "AUTH", with: [pw])
68+
.map { _ in return client }
69+
}
70+
}
71+
}

Sources/NIORedis/RedisClient.swift

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -153,32 +153,3 @@ public final class RedisConnection: RedisClient {
153153
return promise.futureResult
154154
}
155155
}
156-
157-
extension RedisConnection {
158-
/// Makes a client connection to a Redis instance.
159-
/// - Parameters:
160-
/// - socket: The `SocketAddress` information of the Redis instance to connect to.
161-
/// - password: The optional password to authorize the client with.
162-
/// - eventLoopGroup: The `EventLoopGroup` to build the connection on.
163-
/// - logger: The `Logger` instance to log with.
164-
/// - Returns: A `RedisClient` instance representing this new connection.
165-
public static func connect(
166-
to socket: SocketAddress,
167-
with password: String? = nil,
168-
on eventLoopGroup: EventLoopGroup,
169-
logger: Logger = Logger(label: "NIORedis.RedisConnection")
170-
) -> EventLoopFuture<RedisConnection> {
171-
let bootstrap = ClientBootstrap.makeRedisDefault(using: eventLoopGroup)
172-
173-
return bootstrap.connect(to: socket)
174-
.map { return RedisConnection(channel: $0, logger: logger) }
175-
.flatMap { client in
176-
guard let pw = password else {
177-
return eventLoopGroup.next().makeSucceededFuture(client)
178-
}
179-
180-
return client.send(command: "AUTH", with: [pw])
181-
.map { _ in return client }
182-
}
183-
}
184-
}

Tests/NIORedisTests/Commands/BasicCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class BasicCommandsTests: XCTestCase {
66

77
override func setUp() {
88
do {
9-
connection = try RedisConnection.connect().wait()
9+
connection = try Redis.makeConnection().wait()
1010
} catch {
1111
XCTFail("Failed to create RedisConnection! \(error)")
1212
}

Tests/NIORedisTests/Commands/HashCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class HashCommandsTests: XCTestCase {
66

77
override func setUp() {
88
do {
9-
connection = try RedisConnection.connect().wait()
9+
connection = try Redis.makeConnection().wait()
1010
} catch {
1111
XCTFail("Failed to create RedisConnection! \(error)")
1212
}

Tests/NIORedisTests/Commands/ListCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class ListCommandsTests: XCTestCase {
66

77
override func setUp() {
88
do {
9-
connection = try RedisConnection.connect().wait()
9+
connection = try Redis.makeConnection().wait()
1010
} catch {
1111
XCTFail("Failed to create RedisConnection! \(error)")
1212
}

Tests/NIORedisTests/Commands/SetCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ final class SetCommandsTests: XCTestCase {
66

77
override func setUp() {
88
do {
9-
connection = try RedisConnection.connect().wait()
9+
connection = try Redis.makeConnection().wait()
1010
} catch {
1111
XCTFail("Failed to create RedisConnection! \(error)")
1212
}

Tests/NIORedisTests/Commands/SortedSetCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class SortedSetCommandsTests: XCTestCase {
1010

1111
override func setUp() {
1212
do {
13-
connection = try RedisConnection.connect().wait()
13+
connection = try Redis.makeConnection().wait()
1414

1515
var dataset: [(RESPValueConvertible, Double)] = []
1616
for index in 1...10 {

Tests/NIORedisTests/Commands/StringCommandsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class StringCommandsTests: XCTestCase {
88

99
override func setUp() {
1010
do {
11-
connection = try RedisConnection.connect().wait()
11+
connection = try Redis.makeConnection().wait()
1212
} catch {
1313
XCTFail("Failed to create RedisConnection! \(error)")
1414
}

0 commit comments

Comments
 (0)