From 397896c46f1db06c5908c068b1f2afb1d33b9a3e Mon Sep 17 00:00:00 2001 From: Juan Reyes Date: Sun, 8 Aug 2021 15:26:21 -0400 Subject: [PATCH 1/2] Change Lambda.Context from class to struct --- .../AWSLambdaRuntimeCore/LambdaContext.swift | 119 ++++++++++++------ .../AWSLambdaRuntimeCore/LambdaRunner.swift | 2 +- 2 files changed, 85 insertions(+), 36 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift index 19acc469..bdb2c5b7 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift @@ -49,41 +49,95 @@ extension Lambda { extension Lambda { /// Lambda runtime context. /// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument. - public final class Context: CustomDebugStringConvertible { + public struct Context: CustomDebugStringConvertible { + final class _Storage { + var requestID: String + var traceID: String + var invokedFunctionARN: String + var deadline: DispatchWallTime + var cognitoIdentity: String? + var clientContext: String? + var logger: Logger + var eventLoop: EventLoop + var allocator: ByteBufferAllocator + + init( + requestID: String, + traceID: String, + invokedFunctionARN: String, + deadline: DispatchWallTime, + cognitoIdentity: String?, + clientContext: String?, + logger: Logger, + eventLoop: EventLoop, + allocator: ByteBufferAllocator + ) { + self.requestID = requestID + self.traceID = traceID + self.invokedFunctionARN = invokedFunctionARN + self.deadline = deadline + self.cognitoIdentity = cognitoIdentity + self.clientContext = clientContext + self.logger = logger + self.eventLoop = eventLoop + self.allocator = allocator + } + } + + private var storage: _Storage + /// The request ID, which identifies the request that triggered the function invocation. - public let requestID: String - + public var requestID: String { + self.storage.requestID + } + /// The AWS X-Ray tracing header. - public let traceID: String - + public var traceID: String { + self.storage.traceID + } + /// The ARN of the Lambda function, version, or alias that's specified in the invocation. - public let invokedFunctionARN: String - + public var invokedFunctionARN: String { + self.storage.invokedFunctionARN + } + /// The timestamp that the function times out - public let deadline: DispatchWallTime - + public var deadline: DispatchWallTime { + self.storage.deadline + } + /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider. - public let cognitoIdentity: String? - + public var cognitoIdentity: String? { + self.storage.cognitoIdentity + } + /// For invocations from the AWS Mobile SDK, data about the client application and device. - public let clientContext: String? - + public var clientContext: String? { + self.storage.clientContext + } + /// `Logger` to log with /// /// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable. - public let logger: Logger - + public var logger: Logger { + self.storage.logger + } + /// The `EventLoop` the Lambda is executed on. Use this to schedule work with. /// This is useful when implementing the `EventLoopLambdaHandler` protocol. /// /// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care. /// Most importantly the `EventLoop` must never be blocked. - public let eventLoop: EventLoop - + public var eventLoop: EventLoop { + self.storage.eventLoop + } + /// `ByteBufferAllocator` to allocate `ByteBuffer` /// This is useful when implementing `EventLoopLambdaHandler` - public let allocator: ByteBufferAllocator - + public var allocator: ByteBufferAllocator { + self.storage.allocator + } + internal init(requestID: String, traceID: String, invokedFunctionARN: String, @@ -92,22 +146,17 @@ extension Lambda { clientContext: String? = nil, logger: Logger, eventLoop: EventLoop, - allocator: ByteBufferAllocator) - { - self.requestID = requestID - self.traceID = traceID - self.invokedFunctionARN = invokedFunctionARN - self.cognitoIdentity = cognitoIdentity - self.clientContext = clientContext - self.deadline = deadline - // utility - self.eventLoop = eventLoop - self.allocator = allocator - // mutate logger with context - var logger = logger - logger[metadataKey: "awsRequestID"] = .string(requestID) - logger[metadataKey: "awsTraceID"] = .string(traceID) - self.logger = logger + allocator: ByteBufferAllocator) { + self.storage = _Storage(requestID: requestID, + traceID: traceID, + invokedFunctionARN: invokedFunctionARN, + deadline: deadline, + cognitoIdentity: cognitoIdentity, + clientContext: clientContext, + logger: logger, + eventLoop: eventLoop, + allocator: allocator + ) } public func getRemainingTime() -> TimeAmount { diff --git a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift index ec5e9898..4ddd4dfd 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRunner.swift @@ -101,7 +101,7 @@ extension Lambda { } extension Lambda.Context { - fileprivate convenience init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Lambda.Invocation) { + init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Lambda.Invocation) { self.init(requestID: invocation.requestID, traceID: invocation.traceID, invokedFunctionARN: invocation.invokedFunctionARN, From ca2f117392fd4ebd6a7fee370d8c0e8131e5e4a4 Mon Sep 17 00:00:00 2001 From: Juan Reyes Date: Tue, 10 Aug 2021 13:18:11 -0400 Subject: [PATCH 2/2] add formatting to LambdaContext --- .../AWSLambdaRuntimeCore/LambdaContext.swift | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift index bdb2c5b7..6806aeb7 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaContext.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaContext.swift @@ -60,7 +60,7 @@ extension Lambda { var logger: Logger var eventLoop: EventLoop var allocator: ByteBufferAllocator - + init( requestID: String, traceID: String, @@ -83,46 +83,46 @@ extension Lambda { self.allocator = allocator } } - + 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 } - + /// The `EventLoop` the Lambda is executed on. Use this to schedule work with. /// This is useful when implementing the `EventLoopLambdaHandler` protocol. /// @@ -131,13 +131,13 @@ extension Lambda { public var eventLoop: EventLoop { self.storage.eventLoop } - + /// `ByteBufferAllocator` to allocate `ByteBuffer` /// This is useful when implementing `EventLoopLambdaHandler` public var allocator: ByteBufferAllocator { self.storage.allocator } - + internal init(requestID: String, traceID: String, invokedFunctionARN: String, @@ -155,8 +155,7 @@ extension Lambda { clientContext: clientContext, logger: logger, eventLoop: eventLoop, - allocator: allocator - ) + allocator: allocator) } public func getRemainingTime() -> TimeAmount {