@@ -69,6 +69,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
69
69
70
70
private enum ConnectionState : Equatable {
71
71
case disconnected
72
+ case lostConnection
72
73
case connecting( [ ConnectionContinuation ] )
73
74
case connected( Channel , LambdaChannelHandler < LambdaRuntimeClient > )
74
75
@@ -80,6 +81,8 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
80
81
return true
81
82
case ( . connected, . connected) :
82
83
return true
84
+ case ( . lostConnection, . lostConnection) :
85
+ return true
83
86
default :
84
87
return false
85
88
}
@@ -115,9 +118,9 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
115
118
// because it is private, depending on multiple private and non-Sendable types
116
119
// the only thing we need to know outside of this class is if the connection state is disconnected
117
120
@usableFromInline
118
- var isConnectionStateDisconnected : Bool {
121
+ var didLooseConnection : Bool {
119
122
get {
120
- self . connectionState == . disconnected
123
+ self . connectionState == . lostConnection
121
124
}
122
125
}
123
126
@@ -179,12 +182,16 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
179
182
180
183
case . connected( let channel, _) :
181
184
channel. close ( mode: . all, promise: nil )
185
+ case . lostConnection:
186
+ // this should never happen.
187
+ fatalError ( " Lost connection to Lambda service while closing the runtime client " )
182
188
}
183
189
}
184
190
}
185
191
186
192
@usableFromInline
187
193
func nextInvocation( ) async throws -> ( Invocation , Writer ) {
194
+
188
195
try await withTaskCancellationHandler {
189
196
switch self . lambdaState {
190
197
case . idle:
@@ -284,7 +291,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
284
291
285
292
private func channelClosed( _ channel: any Channel ) {
286
293
switch ( self . connectionState, self . closingState) {
287
- case ( _, . closed) :
294
+ case ( _, . closed) , ( . lostConnection , _ ) :
288
295
fatalError ( " Invalid state: \( self . connectionState) , \( self . closingState) " )
289
296
290
297
case ( . disconnected, . notClosing) :
@@ -344,6 +351,10 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
344
351
return loopBound. value
345
352
case . connected( _, let handler) :
346
353
return handler
354
+
355
+ case . lostConnection:
356
+ // this should never happen
357
+ fatalError ( " Lost connection to Lambda service " )
347
358
}
348
359
349
360
let bootstrap = ClientBootstrap ( group: self . eventLoop)
@@ -392,7 +403,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
392
403
}
393
404
394
405
switch self . connectionState {
395
- case . disconnected, . connected:
406
+ case . disconnected, . connected, . lostConnection :
396
407
fatalError ( " Unexpected state: \( self . connectionState) " )
397
408
398
409
case . connecting( let array) :
@@ -408,7 +419,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
408
419
} catch {
409
420
410
421
switch self . connectionState {
411
- case . disconnected, . connected:
422
+ case . disconnected, . connected, . lostConnection :
412
423
fatalError ( " Unexpected state: \( self . connectionState) " )
413
424
414
425
case . connecting( let array) :
@@ -456,6 +467,9 @@ extension LambdaRuntimeClient: LambdaChannelHandlerDelegate {
456
467
457
468
isolated. connectionState = . disconnected
458
469
470
+ case . lostConnection:
471
+ // this should never happen
472
+ fatalError ( " Lost connection to Lambda service " )
459
473
}
460
474
}
461
475
}
0 commit comments