|
| 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 | +} |
0 commit comments