Skip to content

Commit

Permalink
add syncShutdown to LambdaHandler (#132)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tomerd committed Jun 18, 2020
1 parent 0535cb7 commit 2bac896
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ public extension LambdaHandler {
}
}

public extension LambdaHandler {
func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture<Void> {
let promise = context.eventLoop.makePromise(of: Void.self)
self.offloadQueue.async {
do {
try self.syncShutdown(context: context)
promise.succeed(())
} catch {
promise.fail(error)
}
}
return promise.futureResult
}

/// Clean up the Lambda resources synchronously.
/// Concrete Lambda handlers implement this method to shutdown resources like `HTTPClient`s and database connections.
func syncShutdown(context: Lambda.ShutdownContext) 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.
Expand Down Expand Up @@ -165,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<ByteBuffer?>

/// 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`.
Expand All @@ -175,7 +196,7 @@ public protocol ByteBufferLambdaHandler {

public extension ByteBufferLambdaHandler {
func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture<Void> {
context.eventLoop.makeSucceededFuture(Void())
context.eventLoop.makeSucceededFuture(())
}
}

Expand Down

0 comments on commit 2bac896

Please sign in to comment.