Skip to content

Add async LambdaHandler.shutdown #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/AWSLambdaRuntimeCore/LambdaHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *)
Expand All @@ -63,6 +70,16 @@ extension LambdaHandler {
}
return promise.futureResult
}

public func shutdown(context: Lambda.ShutdownContext) -> EventLoopFuture<Void> {
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
Expand Down