From 9f9d42456ded56e57560238c5fac8116d49afc90 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 31 Mar 2022 13:56:22 +0100 Subject: [PATCH 1/2] Add async LambdaHandler.shutdown --- .../AWSLambdaRuntimeCore/LambdaHandler.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 3c2697ff..a8c3d0f7 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -44,6 +44,13 @@ public protocol LambdaHandler: EventLoopLambdaHandler { /// /// - Returns: A Lambda result ot type `Output`. func handle(_ event: Event, context: LambdaContext) async throws -> Output + + /// Clean up the Lambda resources asynchronously. + /// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections. + /// + /// - Note: In case your Lambda fails while creating your LambdaHandler in the `HandlerFactory`, this method + /// **is not invoked**. In this case you must cleanup the created resources immediately in the `HandlerFactory`. + func shutdown(context: Lambda.ShutdownContext) async throws } @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) @@ -63,6 +70,17 @@ extension LambdaHandler { } return promise.futureResult } + + public func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture { + let promise = context.eventLoop.makePromise(of: Void.self) + promise.completeWithTask { + try await self.shutdown(context: context) + } + return promise.futureResult + } + + public func shutdown(context: Lambda.ShutdownContext) async throws { + } } #endif From 5d427165718b5ae4c6c2b6e0398da56131af9c8a Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 31 Mar 2022 14:09:11 +0100 Subject: [PATCH 2/2] soundness --- Sources/AWSLambdaRuntimeCore/LambdaHandler.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index a8c3d0f7..e24d3e19 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -79,8 +79,7 @@ extension LambdaHandler { return promise.futureResult } - public func shutdown(context: Lambda.ShutdownContext) async throws { - } + public func shutdown(context: Lambda.ShutdownContext) async throws {} } #endif