From 245d4ab27aa3d13b8269184ff2884339dd5ac073 Mon Sep 17 00:00:00 2001 From: tom doron Date: Thu, 18 Jun 2020 12:33:39 -0700 Subject: [PATCH 1/2] 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 --- .../AWSLambdaRuntimeCore/LambdaHandler.swift | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index f7559218..9d1d194a 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -62,6 +62,27 @@ public extension LambdaHandler { } } +public extension LambdaHandler { + func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture { + let promise = context.eventLoop.makePromise(of: Void.self) + self.offloadQueue.async { + do { + try self.syncShutdown() + promise.succeed(()) + } catch { + promise.fail(error) + } + } + return promise.futureResult + } + + /// Clean up the `LambdaHandler` resources synchronously. + /// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections. + func syncShutdown() throws { + // noop + } +} + // MARK: - EventLoopLambdaHandler /// 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 { public extension ByteBufferLambdaHandler { func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture { - context.eventLoop.makeSucceededFuture(Void()) + context.eventLoop.makeSucceededFuture(()) } } From e5804408ba53fbfb3b85e7097bf20c83c232f383 Mon Sep 17 00:00:00 2001 From: tom doron Date: Thu, 18 Jun 2020 12:42:48 -0700 Subject: [PATCH 2/2] fixup --- Sources/AWSLambdaRuntimeCore/LambdaHandler.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift index 9d1d194a..16eba1cb 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaHandler.swift @@ -67,7 +67,7 @@ public extension LambdaHandler { let promise = context.eventLoop.makePromise(of: Void.self) self.offloadQueue.async { do { - try self.syncShutdown() + try self.syncShutdown(context: context) promise.succeed(()) } catch { promise.fail(error) @@ -76,9 +76,9 @@ public extension LambdaHandler { return promise.futureResult } - /// Clean up the `LambdaHandler` resources synchronously. + /// Clean up the Lambda resources synchronously. /// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections. - func syncShutdown() throws { + func syncShutdown(context: Lambda.ShutdownContext) throws { // noop } } @@ -186,8 +186,8 @@ public protocol ByteBufferLambdaHandler { /// The `EventLoopFuture` should be completed with either a response encoded as `ByteBuffer` or an `Error` func handle(context: Lambda.Context, event: ByteBuffer) -> EventLoopFuture - /// The method to clean up your resources. - /// Concrete Lambda handlers implement this method to shutdown their `HTTPClient`s and database connections. + /// 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`.