mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-05-03 07:22:27 +00:00
190eb81876
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>
70 lines
2.4 KiB
Swift
70 lines
2.4 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import AWSLambdaEvents
|
|
import AWSLambdaRuntime
|
|
import NIOCore
|
|
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
|
|
// for a simple struct as this one, the compiler automatically infers Sendable
|
|
// With Lambda Managed Instances, your handler struct MUST be Sendable
|
|
struct SendNumbersWithPause: StreamingLambdaHandler, Sendable {
|
|
func handle(
|
|
_ event: ByteBuffer,
|
|
responseWriter: some LambdaResponseStreamWriter,
|
|
context: LambdaContext
|
|
) async throws {
|
|
|
|
// The payload here is a Lambda Function URL request
|
|
// Check the body of the Function URL request to extract the business event
|
|
let payload = try JSONDecoder().decode(FunctionURLRequest.self, from: Data(event.readableBytesView))
|
|
let _ = payload.body
|
|
|
|
// Send HTTP status code and headers before streaming the response body
|
|
try await responseWriter.writeStatusAndHeaders(
|
|
StreamingLambdaStatusAndHeadersResponse(
|
|
statusCode: 418, // I'm a tea pot
|
|
headers: [
|
|
"Content-Type": "text/plain",
|
|
"x-my-custom-header": "streaming-example",
|
|
]
|
|
)
|
|
)
|
|
|
|
// Stream numbers with pauses to demonstrate streaming functionality
|
|
for i in 1...3 {
|
|
// Send partial data
|
|
try await responseWriter.write(ByteBuffer(string: "Number: \(i)\n"))
|
|
|
|
// Perform some long asynchronous work to simulate processing
|
|
try await Task.sleep(for: .milliseconds(1000))
|
|
}
|
|
|
|
// Send final message
|
|
try await responseWriter.write(ByteBuffer(string: "Streaming complete!\n"))
|
|
|
|
// All data has been sent. Close off the response stream.
|
|
try await responseWriter.finish()
|
|
}
|
|
}
|
|
|
|
let runtime = LambdaManagedRuntime(handler: SendNumbersWithPause())
|
|
try await runtime.run()
|