|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Dispatch |
| 16 | +import Logging |
| 17 | +import NIOCore |
| 18 | + |
| 19 | +package protocol StreamingLambdaHandler { |
| 20 | + mutating func handle( |
| 21 | + _ event: ByteBuffer, |
| 22 | + responseWriter: some LambdaResponseStreamWriter, |
| 23 | + context: NewLambdaContext |
| 24 | + ) async throws |
| 25 | +} |
| 26 | + |
| 27 | +package enum NewLambda { |
| 28 | + package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>( |
| 29 | + runtimeClient: RuntimeClient, |
| 30 | + handler: Handler, |
| 31 | + logger: Logger |
| 32 | + ) async throws |
| 33 | + where Handler: StreamingLambdaHandler { |
| 34 | + while !Task.isCancelled { |
| 35 | + let (invocation, writer) = try await runtimeClient.nextInvocation() |
| 36 | + |
| 37 | + var handler = handler |
| 38 | + do { |
| 39 | + try await handler.handle( |
| 40 | + invocation.event, |
| 41 | + responseWriter: writer, |
| 42 | + context: NewLambdaContext( |
| 43 | + requestID: invocation.metadata.requestID, |
| 44 | + traceID: invocation.metadata.traceID, |
| 45 | + invokedFunctionARN: invocation.metadata.invokedFunctionARN, |
| 46 | + deadline: DispatchWallTime(millisSinceEpoch: invocation.metadata.deadlineInMillisSinceEpoch), |
| 47 | + logger: logger |
| 48 | + ) |
| 49 | + ) |
| 50 | + } catch { |
| 51 | + try await writer.reportError(error) |
| 52 | + continue |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments