|
14 | 14 |
|
15 | 15 | import NIO
|
16 | 16 |
|
17 |
| -/// The `NIO.ChannelOutboundHandler.OutboundIn` type for `RedisCommandHandler`. |
18 |
| -/// |
19 |
| -/// This holds the full command message to be sent to Redis, and an `NIO.EventLoopPromise` to be fulfilled when a response has been received. |
20 |
| -/// - Important: This struct has _reference semantics_ due to the retention of the `NIO.EventLoopPromise`. |
21 |
| -public struct RedisCommand { |
22 |
| - /// A message waiting to be sent to Redis. A full message contains a command keyword and its arguments stored as a single `RESPValue.array`. |
23 |
| - public let message: RESPValue |
24 |
| - /// A promise to be fulfilled with the sent message's response from Redis. |
25 |
| - public let responsePromise: EventLoopPromise<RESPValue> |
26 |
| - |
27 |
| - public init(message: RESPValue, responsePromise promise: EventLoopPromise<RESPValue>) { |
28 |
| - self.message = message |
29 |
| - self.responsePromise = promise |
30 |
| - } |
31 |
| -} |
32 |
| - |
33 | 17 | /// An object that operates in a First In, First Out (FIFO) request-response cycle.
|
34 | 18 | ///
|
35 | 19 | /// `RedisCommandHandler` is a `NIO.ChannelDuplexHandler` that sends `RedisCommand` instances to Redis,
|
36 | 20 | /// and fulfills the command's `NIO.EventLoopPromise` as soon as a `RESPValue` response has been received from Redis.
|
37 | 21 | public final class RedisCommandHandler {
|
| 22 | + /// The data payload that the command handler is expecting to receive in the channel to process sending to Redis. |
| 23 | + /// ## message |
| 24 | + /// This value is expected to be a fully serialized command with it's keyword and arguments in a bulk string array ready to be sent to Redis as-is. |
| 25 | + /// ## responsePromise |
| 26 | + /// This is a `NIO.EventLoopPromise` that will be resolved once a response from Redis has been received. |
| 27 | + public typealias OutboundCommandPayload = (message: RESPValue, responsePromise: EventLoopPromise<RESPValue>) |
| 28 | + |
38 | 29 | /// FIFO queue of promises waiting to receive a response value from a sent command.
|
39 | 30 | private var commandResponseQueue: CircularBuffer<EventLoopPromise<RESPValue>>
|
40 | 31 | private var state: State = .default
|
@@ -115,27 +106,25 @@ extension RedisCommandHandler: ChannelInboundHandler {
|
115 | 106 | // MARK: ChannelOutboundHandler
|
116 | 107 |
|
117 | 108 | extension RedisCommandHandler: ChannelOutboundHandler {
|
118 |
| - /// See `NIO.ChannelOutboundHandler.OutboundIn` |
119 |
| - public typealias OutboundIn = RedisCommand |
120 |
| - /// See `NIO.ChannelOutboundHandler.OutboundOut` |
| 109 | + public typealias OutboundIn = OutboundCommandPayload |
121 | 110 | public typealias OutboundOut = RESPValue
|
122 | 111 |
|
123 | 112 | /// Invoked by SwiftNIO when a `write` has been requested on the `Channel`.
|
124 | 113 | ///
|
125 |
| - /// This unwraps a `RedisCommand`, storing the `NIO.EventLoopPromise` in a command queue, |
| 114 | + /// This unwraps a `OutboundCommandPayload` tuple, storing the `NIO.EventLoopPromise` in a command queue |
126 | 115 | /// to fulfill later with the response to the command that is about to be sent through the `NIO.Channel`.
|
127 | 116 | ///
|
128 | 117 | /// See `NIO.ChannelOutboundHandler.write(context:data:promise:)`
|
129 | 118 | public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
130 |
| - let commandContext = self.unwrapOutboundIn(data) |
| 119 | + let commandPayload = self.unwrapOutboundIn(data) |
131 | 120 |
|
132 | 121 | switch self.state {
|
133 |
| - case let .error(e): commandContext.responsePromise.fail(e) |
| 122 | + case let .error(e): commandPayload.responsePromise.fail(e) |
134 | 123 |
|
135 | 124 | case .default:
|
136 |
| - self.commandResponseQueue.append(commandContext.responsePromise) |
| 125 | + self.commandResponseQueue.append(commandPayload.responsePromise) |
137 | 126 | context.write(
|
138 |
| - self.wrapOutboundOut(commandContext.message), |
| 127 | + self.wrapOutboundOut(commandPayload.message), |
139 | 128 | promise: promise
|
140 | 129 | )
|
141 | 130 | }
|
|
0 commit comments