Skip to content

Commit 2bac896

Browse files
authored
add syncShutdown to LambdaHandler (#132)
motivation: make shutdown easier to use changes: * override shutdown to use the offloadQueue to perform syncShutdown * add empty syncShutdown that can be implmented by the concrete Lambda function
1 parent 0535cb7 commit 2bac896

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Diff for: Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

+24-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ public extension LambdaHandler {
6262
}
6363
}
6464

65+
public extension LambdaHandler {
66+
func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture<Void> {
67+
let promise = context.eventLoop.makePromise(of: Void.self)
68+
self.offloadQueue.async {
69+
do {
70+
try self.syncShutdown(context: context)
71+
promise.succeed(())
72+
} catch {
73+
promise.fail(error)
74+
}
75+
}
76+
return promise.futureResult
77+
}
78+
79+
/// Clean up the Lambda resources synchronously.
80+
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
81+
func syncShutdown(context: Lambda.ShutdownContext) throws {
82+
// noop
83+
}
84+
}
85+
6586
// MARK: - EventLoopLambdaHandler
6687

6788
/// Strongly typed, `EventLoopFuture` based processing protocol for a Lambda that takes a user defined `In` and returns a user defined `Out` asynchronously.
@@ -165,8 +186,8 @@ public protocol ByteBufferLambdaHandler {
165186
/// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error`
166187
func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture<ByteBuffer?>
167188

168-
/// The method to clean up your resources.
169-
/// Concrete Lambda handlers implement this method to shutdown their `HTTPClient`s and database connections.
189+
/// Clean up the Lambda resources asynchronously.
190+
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
170191
///
171192
/// - Note: In case your Lambda fails while creating your LambdaHandler in the `HandlerFactory`, this method
172193
/// **is not invoked**. In this case you must cleanup the created resources immediately in the `HandlerFactory`.
@@ -175,7 +196,7 @@ public protocol ByteBufferLambdaHandler {
175196

176197
public extension ByteBufferLambdaHandler {
177198
func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture<Void> {
178-
context.eventLoop.makeSucceededFuture(Void())
199+
context.eventLoop.makeSucceededFuture(())
179200
}
180201
}
181202

0 commit comments

Comments
 (0)