Skip to content

Commit d865a0f

Browse files
committed
rename runtime cancellation to graceful shutdown
1 parent c271eaf commit d865a0f

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

Examples/ServiceLifeCycle/Sources/Lambda.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ struct LambdaFunction {
5252
/// of the PGClient together with the LambdaRuntime
5353
let serviceGroup = ServiceGroup(
5454
services: [pgClient, runtime],
55-
gracefulShutdownSignals: [.sigterm], // add SIGINT for CTRL+C in local testing
55+
gracefulShutdownSignals: [.sigterm, .sigint], // add SIGINT for CTRL+C in local testing
56+
// cancellationSignals: [.sigint],
5657
logger: logger
5758
)
5859
try await serviceGroup.run()

Sources/AWSLambdaRuntimeCore/Lambda.swift

+12-6
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public enum Lambda {
3434

3535
// allow to gracefully shitdown the runtime client loop
3636
// this supports gracefull shutdown of the Lambda runtime when integarted with Swift ServiceLifeCycle
37-
private static let cancelled: Mutex<Bool> = Mutex(false)
38-
public static func cancel() {
39-
Lambda.cancelled.withLock {
37+
private static let gracefulShutdown: Mutex<Bool> = Mutex(false)
38+
public static func shutdown() {
39+
Lambda.gracefulShutdown.withLock {
4040
$0 = true
4141
}
4242
}
@@ -46,11 +46,13 @@ public enum Lambda {
4646
logger: Logger
4747
) async throws where Handler: StreamingLambdaHandler {
4848
var handler = handler
49-
49+
var gracefulShutdown: Bool = Lambda.gracefulShutdown.withLock { $0 }
5050
do {
51-
while !Task.isCancelled {
51+
while !Task.isCancelled && !gracefulShutdown {
52+
logger.trace("Waiting for next invocation")
5253
let (invocation, writer) = try await runtimeClient.nextInvocation()
5354

55+
logger.trace("Received invocation : \(invocation.metadata.requestID)")
5456
do {
5557
try await handler.handle(
5658
invocation.event,
@@ -69,11 +71,15 @@ public enum Lambda {
6971
try await writer.reportError(error)
7072
continue
7173
}
74+
logger.trace("Completed invocation : \(invocation.metadata.requestID)")
75+
gracefulShutdown = Lambda.gracefulShutdown.withLock { $0 }
7276
}
77+
7378
} catch is CancellationError {
7479
// don't allow cancellation error to propagate further
80+
logger.trace("Lambda runLoop() task has been cancelled")
7581
}
76-
logger.trace("Lambda runLoop() \(cancelled ? "cancelled" : "completed")")
82+
logger.trace("Lambda runLoop() terminated \(gracefulShutdown ? "with gracefull shutdown" : "")")
7783
}
7884

7985
/// The default EventLoop the Lambda is scheduled on.

Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
102102
}
103103
}
104104

105-
/// Cancels the runtime client loop.
106-
public func cancel() {
107-
Lambda.cancel()
105+
/// Gracefully shutdown the runtime client loop.
106+
public func shutdown() {
107+
Lambda.shutdown()
108108
}
109109
}

Sources/AWSLambdaRuntimeService/LambdaRuntimeService.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class LambdaRuntimeService<Handler>: Service, @unchecked Sendable where H
3131
try await runtime.run()
3232
} onCancelOrGracefulShutdown: {
3333
self.logger.debug("LambdaRuntime will be cancelled or gracefully shutdown")
34-
self.runtime.cancel()
34+
self.runtime.shutdown()
3535
}
3636
}
3737

0 commit comments

Comments
 (0)