Files
190eb81876 Add support for Lambda Managed Instances without changing the public API [Convenience + Example] (#623)
This PR builds on
https://github.com/awslabs/swift-aws-lambda-runtime/pull/629 to add
convenience structs (Handlers and Adapters) that are `Sendable`

**Changes**

- **Added Sendable adapter types**: Implemented `ClosureHandlerSendable`
- a thread-safe version of existing closure handler that enforces
`Sendable` conformance for concurrent execution environments - and added
conditional conformance to `Sendable` for other Adapters when the
Handler is `Sendable`

- **Enhanced handler protocols for concurrency**: Extended handler
protocols to support `Sendable` constraints and concurrent response
writing through `LambdaResponseStreamWriter & Sendable`, enabling safe
multi-threaded invocation processing

- **Created comprehensive Lambda Managed Instances examples**: Built
three demonstration functions showcasing concurrent execution
capabilities, streaming responses, and background processing patterns
specific to the new managed instances deployment model

**Context**
Lambda Managed Instances support multi-concurrent invocations where
multiple invocations execute simultaneously within the same execution
environment. The runtime now detects the configured concurrency level
and launches the appropriate number of RICs to handle concurrent
requests efficiently.

When `AWS_LAMBDA_MAX_CONCURRENCY` is 1 or unset, the runtime maintains
the existing single-threaded behaviour for optimal performance on
traditional Lambda deployments.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Sebastien Stormacq <stormacq@amazon.lu>
2026-02-12 00:32:31 +01:00

153 lines
6.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright SwiftAWSLambdaRuntime project authors
// Copyright (c) Amazon.com, Inc. or its affiliates.
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if ManagedRuntimeSupport
#if FoundationJSONSupport
import NIOCore
#if canImport(FoundationEssentials)
import FoundationEssentials
#else
import struct Foundation.Data
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
#endif
import Logging
@available(LambdaSwift 2.0, *)
extension LambdaManagedRuntime {
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a non-`Void` return type**.
/// - Parameters:
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - body: The handler in the form of a closure.
public convenience init<Event: Decodable, Output>(
decoder: JSONDecoder = JSONDecoder(),
encoder: JSONEncoder = JSONEncoder(),
logger: Logger = Logger(label: "LambdaManagedRuntime"),
body: @Sendable @escaping (Event, LambdaContext) async throws -> Output
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Output, ClosureHandlerSendable<Event, Output>>,
Event,
Output,
LambdaJSONEventDecoder,
LambdaJSONOutputEncoder<Output>
>
{
let handler = LambdaCodableAdapter(
encoder: encoder,
decoder: decoder,
handler: LambdaHandlerAdapter(handler: ClosureHandlerSendable(body: body))
)
self.init(handler: handler, logger: logger)
}
/// Initialize an instance with a `LambdaHandler` defined in the form of a closure **with a `Void` return type**.
/// - Parameter body: The handler in the form of a closure.
/// - Parameter decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
/// - Parameter logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
public convenience init<Event: Decodable>(
decoder: JSONDecoder = JSONDecoder(),
logger: Logger = Logger(label: "LambdaRuntime"),
body: @Sendable @escaping (Event, LambdaContext) async throws -> Void
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Void, ClosureHandlerSendable<Event, Void>>,
Event,
Void,
LambdaJSONEventDecoder,
VoidEncoder
>
{
let handler = LambdaCodableAdapter(
decoder: LambdaJSONEventDecoder(decoder),
handler: LambdaHandlerAdapter(handler: ClosureHandlerSendable(body: body))
)
self.init(handler: handler, logger: logger)
}
/// Initialize an instance directly with a `LambdaHandler`, when `Event` is `Decodable` and `Output` is `Void`.
/// - Parameters:
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - lambdaHandler: A type that conforms to the `LambdaHandler` and `Sendable` protocols, whose `Event` is `Decodable` and `Output` is `Void`
public convenience init<Event: Decodable, LHandler: LambdaHandler & Sendable>(
decoder: JSONDecoder = JSONDecoder(),
logger: Logger = Logger(label: "LambdaRuntime"),
lambdaHandler: LHandler
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Void, LHandler>,
Event,
Void,
LambdaJSONEventDecoder,
VoidEncoder
>,
LHandler.Event == Event,
LHandler.Output == Void
{
let handler = LambdaCodableAdapter(
decoder: LambdaJSONEventDecoder(decoder),
handler: LambdaHandlerAdapter(handler: lambdaHandler)
)
self.init(handler: handler, logger: logger)
}
/// Initialize an instance directly with a `LambdaHandler`, when `Event` is `Decodable` and `Output` is `Encodable`.
/// - Parameters:
/// - decoder: The decoder object that will be used to decode the incoming `ByteBuffer` event into the generic `Event` type. `JSONDecoder()` used as default.
/// - encoder: The encoder object that will be used to encode the generic `Output` into a `ByteBuffer`. `JSONEncoder()` used as default.
/// - logger: The logger to use for the runtime. Defaults to a logger with label "LambdaRuntime".
/// - lambdaHandler: A type that conforms to the `LambdaHandler` and `Sendable` protocols, whose `Event` is `Decodable` and `Output` is `Encodable`
public convenience init<Event: Decodable, Output, LHandler: LambdaHandler & Sendable>(
decoder: JSONDecoder = JSONDecoder(),
encoder: JSONEncoder = JSONEncoder(),
logger: Logger = Logger(label: "LambdaRuntime"),
lambdaHandler: LHandler
)
where
Handler == LambdaCodableAdapter<
LambdaHandlerAdapter<Event, Output, LHandler>,
Event,
Output,
LambdaJSONEventDecoder,
LambdaJSONOutputEncoder<Output>
>,
LHandler.Event == Event,
LHandler.Output == Output
{
let handler = LambdaCodableAdapter(
encoder: encoder,
decoder: decoder,
handler: LambdaHandlerAdapter(handler: lambdaHandler)
)
self.init(handler: handler, logger: logger)
}
}
#endif // trait: FoundationJSONSupport
#endif // trait: ManagedRuntimeSupport