@@ -65,11 +65,11 @@ extension ValkeyChannelHandler {
65
65
@usableFromInline
66
66
struct ConnectedState {
67
67
let context : Context
68
- var pendingHelloCommand : PendingCommand
68
+ var pendingCommands : Deque < PendingCommand >
69
69
70
- func cancel( requestID: Int ) -> PendingCommand ? {
71
- if pendingHelloCommand . requestID == requestID {
72
- return pendingHelloCommand
70
+ func cancel( requestID: Int ) -> Deque < PendingCommand > ? {
71
+ if pendingCommands . first ? . requestID == requestID {
72
+ return pendingCommands
73
73
}
74
74
return nil
75
75
}
@@ -85,11 +85,11 @@ extension ValkeyChannelHandler {
85
85
86
86
/// handler has become active
87
87
@usableFromInline
88
- mutating func setConnected( context: Context , pendingHelloCommand : PendingCommand ) {
88
+ mutating func setConnected( context: Context , pendingCommands : Deque < PendingCommand > ) {
89
89
switch consume self. state {
90
90
case . initialized:
91
91
self = . connected(
92
- . init( context: context, pendingHelloCommand : pendingHelloCommand )
92
+ . init( context: context, pendingCommands : pendingCommands )
93
93
)
94
94
case . connected:
95
95
preconditionFailure ( " Cannot set connected state when state is connected " )
@@ -155,15 +155,18 @@ extension ValkeyChannelHandler {
155
155
switch consume self. state {
156
156
case . initialized:
157
157
preconditionFailure ( " Cannot send command when initialized " )
158
- case . connected( let state) :
158
+ case . connected( var state) :
159
+ guard let helloCommand = state. pendingCommands. popFirst ( ) else {
160
+ preconditionFailure ( " Cannot be in connected state with no pending commands " )
161
+ }
159
162
switch token. value {
160
163
case . bulkError( let message) , . simpleError( let message) :
161
164
let error = ValkeyClientError ( . commandError, message: String ( buffer: message) )
162
165
self = . closed( error)
163
- return . respondAndClose( state . pendingHelloCommand , error)
166
+ return . respondAndClose( helloCommand , error)
164
167
default :
165
- self = . active( . init( context: state. context, pendingCommands: . init ( ) ) )
166
- return . respond( state . pendingHelloCommand , . cancel)
168
+ self = . active( . init( context: state. context, pendingCommands: state . pendingCommands ) )
169
+ return . respond( helloCommand , . cancel)
167
170
}
168
171
case . active( var state) :
169
172
guard let command = state. pendingCommands. popFirst ( ) else {
@@ -204,8 +207,12 @@ extension ValkeyChannelHandler {
204
207
self = . closed( nil )
205
208
return . respondAndClose( command, nil )
206
209
}
207
- case . closed:
208
- preconditionFailure ( " Cannot receive command on closed connection " )
210
+ case . closed( let error) :
211
+ guard let error else {
212
+ preconditionFailure ( " Cannot receive command on closed connection with no error " )
213
+ }
214
+ self = . closed( error)
215
+ return . closeWithError( error)
209
216
}
210
217
}
211
218
@@ -221,19 +228,22 @@ extension ValkeyChannelHandler {
221
228
case . initialized:
222
229
preconditionFailure ( " Cannot wait until connection has succeeded " )
223
230
case . connected( let state) :
224
- switch state. pendingHelloCommand. promise {
231
+ guard let helloCommand = state. pendingCommands. first else {
232
+ preconditionFailure ( " Cannot be in connected state with no pending commands " )
233
+ }
234
+ switch helloCommand. promise {
225
235
case . nio( let promise) :
226
236
self = . connected( state)
227
237
return . waitForPromise( promise)
228
- case . swift:
238
+ case . swift, . ignore :
229
239
preconditionFailure ( " Connected state cannot be setup with a Swift continuation " )
230
240
}
231
241
case . active( let state) :
232
242
self = . active( state)
233
243
return . done
234
244
case . closing( let state) :
235
245
self = . closing( state)
236
- return . done
246
+ return . reportedClosed ( nil )
237
247
case . closed( let error) :
238
248
self = . closed( error)
239
249
return . reportedClosed( error)
@@ -253,12 +263,15 @@ extension ValkeyChannelHandler {
253
263
case . initialized:
254
264
preconditionFailure ( " Cannot cancel when initialized " )
255
265
case . connected( let state) :
256
- if state. pendingHelloCommand. deadline <= now {
266
+ guard let helloCommand = state. pendingCommands. first else {
267
+ preconditionFailure ( " Cannot be in connected state with no pending commands " )
268
+ }
269
+ if helloCommand. deadline <= now {
257
270
self = . closed( ValkeyClientError ( . timeout) )
258
- return . failPendingCommandsAndClose( state. context, [ state. pendingHelloCommand ] )
271
+ return . failPendingCommandsAndClose( state. context, state. pendingCommands )
259
272
} else {
260
273
self = . connected( state)
261
- return . reschedule( state . pendingHelloCommand . deadline)
274
+ return . reschedule( helloCommand . deadline)
262
275
}
263
276
case . active( let state) :
264
277
if let firstCommand = state. pendingCommands. first {
@@ -303,11 +316,11 @@ extension ValkeyChannelHandler {
303
316
case . initialized:
304
317
preconditionFailure ( " Cannot cancel when initialized " )
305
318
case . connected( let state) :
306
- if let command = state. cancel ( requestID: requestID) {
319
+ if let commands = state. cancel ( requestID: requestID) {
307
320
self = . closed( CancellationError ( ) )
308
321
return . failPendingCommandsAndClose(
309
322
state. context,
310
- cancel: [ command ] ,
323
+ cancel: . init ( commands ) ,
311
324
closeConnectionDueToCancel: [ ]
312
325
)
313
326
} else {
@@ -360,7 +373,7 @@ extension ValkeyChannelHandler {
360
373
self = . closed( nil )
361
374
return . doNothing
362
375
case . connected( let state) :
363
- self = . closing( . init( context: state. context, pendingCommands: [ state. pendingHelloCommand ] ) )
376
+ self = . closing( . init( context: state. context, pendingCommands: state. pendingCommands ) )
364
377
return . waitForPendingCommands( state. context)
365
378
case . active( let state) :
366
379
if state. pendingCommands. count > 0 {
@@ -393,7 +406,7 @@ extension ValkeyChannelHandler {
393
406
return . doNothing
394
407
case . connected( let state) :
395
408
self = . closed( nil )
396
- return . failPendingCommandsAndClose( state. context, [ state. pendingHelloCommand ] )
409
+ return . failPendingCommandsAndClose( state. context, state. pendingCommands )
397
410
case . active( let state) :
398
411
self = . closed( nil )
399
412
return . failPendingCommandsAndClose( state. context, state. pendingCommands)
@@ -421,7 +434,7 @@ extension ValkeyChannelHandler {
421
434
return . doNothing
422
435
case . connected( let state) :
423
436
self = . closed( nil )
424
- return . failPendingCommandsAndSubscriptions( [ state. pendingHelloCommand ] )
437
+ return . failPendingCommandsAndSubscriptions( state. pendingCommands )
425
438
case . active( let state) :
426
439
self = . closed( nil )
427
440
return . failPendingCommandsAndSubscriptions( state. pendingCommands)
0 commit comments