Skip to content

Commit ce8b567

Browse files
committed
introducing a new connection state "lostConnection"
1 parent 025a0e5 commit ce8b567

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

Sources/AWSLambdaRuntime/Lambda.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public enum Lambda {
4343
while !Task.isCancelled {
4444

4545
guard let runtimeClient = runtimeClient as? LambdaRuntimeClient,
46-
await !runtimeClient.isConnectionStateDisconnected else {
47-
logger.trace("Runtime client not connected, exiting run loop")
46+
await !runtimeClient.didLooseConnection else {
47+
logger.trace("Runtime client disconnected, exiting run loop")
4848
throw LambdaRuntimeError.init(code: .connectionToControlPlaneLost)
4949
}
5050

Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
6969

7070
private enum ConnectionState: Equatable {
7171
case disconnected
72+
case lostConnection
7273
case connecting([ConnectionContinuation])
7374
case connected(Channel, LambdaChannelHandler<LambdaRuntimeClient>)
7475

@@ -80,6 +81,8 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
8081
return true
8182
case (.connected, .connected):
8283
return true
84+
case (.lostConnection, .lostConnection):
85+
return true
8386
default:
8487
return false
8588
}
@@ -115,9 +118,9 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
115118
// because it is private, depending on multiple private and non-Sendable types
116119
// the only thing we need to know outside of this class is if the connection state is disconnected
117120
@usableFromInline
118-
var isConnectionStateDisconnected: Bool {
121+
var didLooseConnection: Bool {
119122
get {
120-
self.connectionState == .disconnected
123+
self.connectionState == .lostConnection
121124
}
122125
}
123126

@@ -179,12 +182,16 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
179182

180183
case .connected(let channel, _):
181184
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")
182188
}
183189
}
184190
}
185191

186192
@usableFromInline
187193
func nextInvocation() async throws -> (Invocation, Writer) {
194+
188195
try await withTaskCancellationHandler {
189196
switch self.lambdaState {
190197
case .idle:
@@ -284,7 +291,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
284291

285292
private func channelClosed(_ channel: any Channel) {
286293
switch (self.connectionState, self.closingState) {
287-
case (_, .closed):
294+
case (_, .closed), (.lostConnection, _):
288295
fatalError("Invalid state: \(self.connectionState), \(self.closingState)")
289296

290297
case (.disconnected, .notClosing):
@@ -344,6 +351,10 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
344351
return loopBound.value
345352
case .connected(_, let handler):
346353
return handler
354+
355+
case .lostConnection:
356+
// this should never happen
357+
fatalError("Lost connection to Lambda service")
347358
}
348359

349360
let bootstrap = ClientBootstrap(group: self.eventLoop)
@@ -392,7 +403,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
392403
}
393404

394405
switch self.connectionState {
395-
case .disconnected, .connected:
406+
case .disconnected, .connected, .lostConnection:
396407
fatalError("Unexpected state: \(self.connectionState)")
397408

398409
case .connecting(let array):
@@ -408,7 +419,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
408419
} catch {
409420

410421
switch self.connectionState {
411-
case .disconnected, .connected:
422+
case .disconnected, .connected, .lostConnection:
412423
fatalError("Unexpected state: \(self.connectionState)")
413424

414425
case .connecting(let array):
@@ -456,6 +467,9 @@ extension LambdaRuntimeClient: LambdaChannelHandlerDelegate {
456467

457468
isolated.connectionState = .disconnected
458469

470+
case .lostConnection:
471+
// this should never happen
472+
fatalError("Lost connection to Lambda service")
459473
}
460474
}
461475
}

0 commit comments

Comments
 (0)