From ac583cc61a45f6c93b71742b86e51456c1b73923 Mon Sep 17 00:00:00 2001 From: Aryan Shah Date: Wed, 28 Aug 2024 15:06:37 +0100 Subject: [PATCH 1/3] Add new LambdaContext --- .../NewLambdaContext.swift | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift diff --git a/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift b/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift new file mode 100644 index 00000000..87fd1ced --- /dev/null +++ b/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift @@ -0,0 +1,145 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors +// 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 Logging +import NIOCore + +#if swift(<5.9) +@preconcurrency import Dispatch +#else +import Dispatch +#endif + +// MARK: - Context + +/// Lambda runtime context. +/// The Lambda runtime generates and passes the `LambdaContext` to the Lambda handler as an argument. +public struct NewLambdaContext: CustomDebugStringConvertible, Sendable { + final class _Storage: Sendable { + let requestID: String + let traceID: String + let invokedFunctionARN: String + let deadline: DispatchWallTime + let cognitoIdentity: String? + let clientContext: String? + let logger: Logger + + init( + requestID: String, + traceID: String, + invokedFunctionARN: String, + deadline: DispatchWallTime, + cognitoIdentity: String?, + clientContext: String?, + logger: Logger + ) { + self.requestID = requestID + self.traceID = traceID + self.invokedFunctionARN = invokedFunctionARN + self.deadline = deadline + self.cognitoIdentity = cognitoIdentity + self.clientContext = clientContext + self.logger = logger + } + } + + private var storage: _Storage + + /// The request ID, which identifies the request that triggered the function invocation. + public var requestID: String { + self.storage.requestID + } + + /// The AWS X-Ray tracing header. + public var traceID: String { + self.storage.traceID + } + + /// The ARN of the Lambda function, version, or alias that's specified in the invocation. + public var invokedFunctionARN: String { + self.storage.invokedFunctionARN + } + + /// The timestamp that the function times out. + public var deadline: DispatchWallTime { + self.storage.deadline + } + + /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. + public var cognitoIdentity: String? { + self.storage.cognitoIdentity + } + + /// For invocations from the AWS Mobile SDK, data about the client application and device. + public var clientContext: String? { + self.storage.clientContext + } + + /// `Logger` to log with. + /// + /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. + public var logger: Logger { + self.storage.logger + } + + init( + requestID: String, + traceID: String, + invokedFunctionARN: String, + deadline: DispatchWallTime, + cognitoIdentity: String? = nil, + clientContext: String? = nil, + logger: Logger + ) { + self.storage = _Storage( + requestID: requestID, + traceID: traceID, + invokedFunctionARN: invokedFunctionARN, + deadline: deadline, + cognitoIdentity: cognitoIdentity, + clientContext: clientContext, + logger: logger + ) + } + + public func getRemainingTime() -> TimeAmount { + let deadline = self.deadline.millisSinceEpoch + let now = DispatchWallTime.now().millisSinceEpoch + + let remaining = deadline - now + return .milliseconds(remaining) + } + + public var debugDescription: String { + "\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))" + } + + /// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning. + public static func __forTestsOnly( + requestID: String, + traceID: String, + invokedFunctionARN: String, + timeout: DispatchTimeInterval, + logger: Logger, + eventLoop: EventLoop + ) -> NewLambdaContext { + NewLambdaContext( + requestID: requestID, + traceID: traceID, + invokedFunctionARN: invokedFunctionARN, + deadline: .now() + timeout, + logger: logger + ) + } +} From 7957715f535dad712e8704e85e3737d07a3b6b3c Mon Sep 17 00:00:00 2001 From: Aryan Shah Date: Wed, 28 Aug 2024 15:34:37 +0100 Subject: [PATCH 2/3] Remove unnecessary if check --- Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift b/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift index 87fd1ced..389351d9 100644 --- a/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift @@ -12,15 +12,10 @@ // //===----------------------------------------------------------------------===// +import Dispatch import Logging import NIOCore -#if swift(<5.9) -@preconcurrency import Dispatch -#else -import Dispatch -#endif - // MARK: - Context /// Lambda runtime context. From f4bd7b1ca62898e88aeccad9d3322465105f4079 Mon Sep 17 00:00:00 2001 From: Aryan Shah Date: Wed, 28 Aug 2024 16:16:21 +0100 Subject: [PATCH 3/3] Replace public with package --- .../NewLambdaContext.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift b/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift index 389351d9..89414728 100644 --- a/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift @@ -20,7 +20,7 @@ import NIOCore /// Lambda runtime context. /// The Lambda runtime generates and passes the `LambdaContext` to the Lambda handler as an argument. -public struct NewLambdaContext: CustomDebugStringConvertible, Sendable { +package struct NewLambdaContext: CustomDebugStringConvertible, Sendable { final class _Storage: Sendable { let requestID: String let traceID: String @@ -52,39 +52,39 @@ public struct NewLambdaContext: CustomDebugStringConvertible, Sendable { private var storage: _Storage /// The request ID, which identifies the request that triggered the function invocation. - public var requestID: String { + package var requestID: String { self.storage.requestID } /// The AWS X-Ray tracing header. - public var traceID: String { + package var traceID: String { self.storage.traceID } /// The ARN of the Lambda function, version, or alias that's specified in the invocation. - public var invokedFunctionARN: String { + package var invokedFunctionARN: String { self.storage.invokedFunctionARN } /// The timestamp that the function times out. - public var deadline: DispatchWallTime { + package var deadline: DispatchWallTime { self.storage.deadline } /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. - public var cognitoIdentity: String? { + package var cognitoIdentity: String? { self.storage.cognitoIdentity } /// For invocations from the AWS Mobile SDK, data about the client application and device. - public var clientContext: String? { + package var clientContext: String? { self.storage.clientContext } /// `Logger` to log with. /// /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. - public var logger: Logger { + package var logger: Logger { self.storage.logger } @@ -108,7 +108,7 @@ public struct NewLambdaContext: CustomDebugStringConvertible, Sendable { ) } - public func getRemainingTime() -> TimeAmount { + package func getRemainingTime() -> TimeAmount { let deadline = self.deadline.millisSinceEpoch let now = DispatchWallTime.now().millisSinceEpoch @@ -116,12 +116,12 @@ public struct NewLambdaContext: CustomDebugStringConvertible, Sendable { return .milliseconds(remaining) } - public var debugDescription: String { + package var debugDescription: String { "\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))" } /// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning. - public static func __forTestsOnly( + package static func __forTestsOnly( requestID: String, traceID: String, invokedFunctionARN: String,