Skip to content

Commit 245d4ab

Browse files
committed
add syncShutdown to LambdaHandler
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 245d4ab

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Sources/AWSLambdaRuntimeCore/LambdaHandler.swift

Lines changed: 22 additions & 1 deletion
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()
71+
promise.succeed(())
72+
} catch {
73+
promise.fail(error)
74+
}
75+
}
76+
return promise.futureResult
77+
}
78+
79+
/// Clean up the `LambdaHandler` resources synchronously.
80+
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
81+
func syncShutdown() 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.
@@ -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)