Skip to content

Commit 0f68ed5

Browse files
authoredAug 28, 2024··
Introduce LambdaRuntimeClientProtocol (#344)
1 parent f329874 commit 0f68ed5

File tree

6 files changed

+57
-19
lines changed

6 files changed

+57
-19
lines changed
 

‎Sources/AWSLambdaRuntimeCore/ControlPlaneRequest.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ enum ControlPlaneRequest: Hashable {
2323
}
2424

2525
enum ControlPlaneResponse: Hashable {
26-
case next(Invocation, ByteBuffer)
26+
case next(InvocationMetadata, ByteBuffer)
2727
case accepted
2828
case error(ErrorResponse)
2929
}
3030

31-
struct Invocation: Hashable {
32-
let requestID: String
33-
let deadlineInMillisSinceEpoch: Int64
34-
let invokedFunctionARN: String
35-
let traceID: String
36-
let clientContext: String?
37-
let cognitoIdentity: String?
31+
package struct InvocationMetadata: Hashable {
32+
package let requestID: String
33+
package let deadlineInMillisSinceEpoch: Int64
34+
package let invokedFunctionARN: String
35+
package let traceID: String
36+
package let clientContext: String?
37+
package let cognitoIdentity: String?
3838

39-
init(headers: HTTPHeaders) throws {
39+
package init(headers: HTTPHeaders) throws {
4040
guard let requestID = headers.first(name: AmazonHeaders.requestID), !requestID.isEmpty else {
4141
throw LambdaRuntimeError.invocationMissingHeader(AmazonHeaders.requestID)
4242
}

‎Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ final class LambdaRunner {
132132
}
133133

134134
extension LambdaContext {
135-
init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Invocation) {
135+
init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: InvocationMetadata) {
136136
self.init(
137137
requestID: invocation.requestID,
138138
traceID: invocation.traceID,

‎Sources/AWSLambdaRuntimeCore/LambdaRuntimeClient.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ struct LambdaRuntimeClient {
3232
}
3333

3434
/// Requests invocation from the control plane.
35-
func getNextInvocation(logger: Logger) -> EventLoopFuture<(Invocation, ByteBuffer)> {
35+
func getNextInvocation(logger: Logger) -> EventLoopFuture<(InvocationMetadata, ByteBuffer)> {
3636
let url = Consts.invocationURLPrefix + Consts.getNextInvocationURLSuffix
3737
logger.debug("requesting work from lambda runtime engine using \(url)")
3838
return self.httpClient.get(url: url, headers: LambdaRuntimeClient.defaultHeaders).flatMapThrowing { response in
3939
guard response.status == .ok else {
4040
throw LambdaRuntimeError.badStatusCode(response.status)
4141
}
42-
let invocation = try Invocation(headers: response.headers)
42+
let invocation = try InvocationMetadata(headers: response.headers)
4343
guard let event = response.body else {
4444
throw LambdaRuntimeError.noBody
4545
}
@@ -59,7 +59,7 @@ struct LambdaRuntimeClient {
5959
/// Reports a result to the Runtime Engine.
6060
func reportResults(
6161
logger: Logger,
62-
invocation: Invocation,
62+
invocation: InvocationMetadata,
6363
result: Result<ByteBuffer?, Error>
6464
) -> EventLoopFuture<Void> {
6565
var url = Consts.invocationURLPrefix + "/" + invocation.requestID
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 NIOCore
16+
17+
package protocol LambdaResponseStreamWriter {
18+
mutating func write(_ buffer: ByteBuffer) async throws
19+
func finish() async throws
20+
func writeAndFinish(_ buffer: ByteBuffer) async throws
21+
func reportError(_ error: any Error) async throws
22+
}
23+
24+
package protocol LambdaRuntimeClientProtocol {
25+
associatedtype Writer: LambdaResponseStreamWriter
26+
27+
func nextInvocation() async throws -> (Invocation, Writer)
28+
}
29+
30+
package struct Invocation {
31+
package var metadata: InvocationMetadata
32+
package var event: ByteBuffer
33+
34+
package init(metadata: InvocationMetadata, event: ByteBuffer) {
35+
self.metadata = metadata
36+
self.event = event
37+
}
38+
}

‎Tests/AWSLambdaRuntimeCoreTests/ControlPlaneRequestTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class InvocationTest: XCTestCase {
2525
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
2626
])
2727

28-
var invocation: Invocation?
28+
var invocation: InvocationMetadata?
2929

30-
XCTAssertNoThrow(invocation = try Invocation(headers: headers))
30+
XCTAssertNoThrow(invocation = try InvocationMetadata(headers: headers))
3131
XCTAssertNotNil(invocation)
3232

3333
guard !invocation!.traceID.isEmpty else {

‎Tests/AWSLambdaRuntimeCoreTests/LambdaRuntimeClientTest.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ class LambdaRuntimeClientTest: XCTestCase {
279279
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
280280
(AmazonHeaders.traceID, "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=1"),
281281
])
282-
var inv: Invocation?
283-
XCTAssertNoThrow(inv = try Invocation(headers: header))
282+
var inv: InvocationMetadata?
283+
XCTAssertNoThrow(inv = try InvocationMetadata(headers: header))
284284
guard let invocation = inv else { return }
285285

286286
let result = client.reportResults(
@@ -332,8 +332,8 @@ class LambdaRuntimeClientTest: XCTestCase {
332332
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),
333333
(AmazonHeaders.traceID, "Root=\(AmazonHeaders.generateXRayTraceID());Sampled=1"),
334334
])
335-
var inv: Invocation?
336-
XCTAssertNoThrow(inv = try Invocation(headers: header))
335+
var inv: InvocationMetadata?
336+
XCTAssertNoThrow(inv = try InvocationMetadata(headers: header))
337337
guard let invocation = inv else { return }
338338

339339
let result = client.reportResults(logger: logger, invocation: invocation, result: Result.success(nil))

0 commit comments

Comments
 (0)
Please sign in to comment.