Skip to content

Commit 025a0e5

Browse files
committed
simplify by checking connection state in the nextInvocation() call
1 parent 5822e0a commit 025a0e5

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

Sources/AWSLambdaRuntime/Lambda.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ public enum Lambda {
4141
var logger = logger
4242
do {
4343
while !Task.isCancelled {
44-
45-
if let runtimeClient = runtimeClient as? LambdaRuntimeClient,
46-
let futureConnectionClosed = await runtimeClient.futureConnectionClosed
47-
{
48-
// Wait for the futureConnectionClosed to complete,
49-
// which will happen when the Lambda HTTP Server (or MockServer) closes the connection
50-
// This allows us to exit the run loop gracefully.
51-
// The futureConnectionClosed is always an error, let it throw to terminate the Lambda run loop.
52-
let _ = try await futureConnectionClosed.get()
44+
45+
guard let runtimeClient = runtimeClient as? LambdaRuntimeClient,
46+
await !runtimeClient.isConnectionStateDisconnected else {
47+
logger.trace("Runtime client not connected, exiting run loop")
48+
throw LambdaRuntimeError.init(code: .connectionToControlPlaneLost)
5349
}
54-
50+
5551
logger.trace("Waiting for next invocation")
5652
let (invocation, writer) = try await runtimeClient.nextInvocation()
5753
logger[metadataKey: "aws-request-id"] = "\(invocation.metadata.requestID)"

Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,23 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
6767
NIOLoopBound<LambdaChannelHandler<LambdaRuntimeClient>>, any Error
6868
>
6969

70-
private enum ConnectionState {
70+
private enum ConnectionState: Equatable {
7171
case disconnected
7272
case connecting([ConnectionContinuation])
7373
case connected(Channel, LambdaChannelHandler<LambdaRuntimeClient>)
74+
75+
static func == (lhs: ConnectionState, rhs: ConnectionState) -> Bool {
76+
switch (lhs, rhs) {
77+
case (.disconnected, .disconnected):
78+
return true
79+
case (.connecting, .connecting):
80+
return true
81+
case (.connected, .connected):
82+
return true
83+
default:
84+
return false
85+
}
86+
}
7487
}
7588

7689
enum LambdaState {
@@ -92,14 +105,22 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
92105
case closed
93106
}
94107

95-
@usableFromInline
96-
var futureConnectionClosed: EventLoopFuture<LambdaRuntimeError>? = nil
97-
98108
private let eventLoop: any EventLoop
99109
private let logger: Logger
100110
private let configuration: Configuration
101111

102112
private var connectionState: ConnectionState = .disconnected
113+
114+
// adding this dynamic property because I can not give access to `connectionState` directly
115+
// because it is private, depending on multiple private and non-Sendable types
116+
// the only thing we need to know outside of this class is if the connection state is disconnected
117+
@usableFromInline
118+
var isConnectionStateDisconnected: Bool {
119+
get {
120+
self.connectionState == .disconnected
121+
}
122+
}
123+
103124
private var lambdaState: LambdaState = .idle(previousRequestID: nil)
104125
private var closingState: ClosingState = .notClosing
105126

@@ -367,16 +388,6 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
367388
channel.closeFuture.whenComplete { result in
368389
self.assumeIsolated { runtimeClient in
369390
runtimeClient.channelClosed(channel)
370-
371-
// at this stage, we lost the connection to the Lambda Service,
372-
// this is very unlikely to happen when running in a lambda function deployed in the cloud
373-
// however, this happens when performance testing against the MockServer
374-
// shutdown this runtime.
375-
// The Lambda service will create a new runtime environment anyway
376-
runtimeClient.logger.trace("Connection to Lambda Service HTTP Server lost, exiting")
377-
runtimeClient.futureConnectionClosed = runtimeClient.eventLoop.makeFailedFuture(
378-
LambdaRuntimeError(code: .connectionToControlPlaneLost)
379-
)
380391
}
381392
}
382393

0 commit comments

Comments
 (0)