Skip to content

Commit 4121b35

Browse files
authored
Update LambdaContext (#345)
1 parent 0f68ed5 commit 4121b35

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

Diff for: Sources/AWSLambdaRuntimeCore/NewLambdaContext.swift

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2017-2022 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+
// MARK: - Context
20+
21+
/// Lambda runtime context.
22+
/// The Lambda runtime generates and passes the `LambdaContext` to the Lambda handler as an argument.
23+
package struct NewLambdaContext: CustomDebugStringConvertible, Sendable {
24+
final class _Storage: Sendable {
25+
let requestID: String
26+
let traceID: String
27+
let invokedFunctionARN: String
28+
let deadline: DispatchWallTime
29+
let cognitoIdentity: String?
30+
let clientContext: String?
31+
let logger: Logger
32+
33+
init(
34+
requestID: String,
35+
traceID: String,
36+
invokedFunctionARN: String,
37+
deadline: DispatchWallTime,
38+
cognitoIdentity: String?,
39+
clientContext: String?,
40+
logger: Logger
41+
) {
42+
self.requestID = requestID
43+
self.traceID = traceID
44+
self.invokedFunctionARN = invokedFunctionARN
45+
self.deadline = deadline
46+
self.cognitoIdentity = cognitoIdentity
47+
self.clientContext = clientContext
48+
self.logger = logger
49+
}
50+
}
51+
52+
private var storage: _Storage
53+
54+
/// The request ID, which identifies the request that triggered the function invocation.
55+
package var requestID: String {
56+
self.storage.requestID
57+
}
58+
59+
/// The AWS X-Ray tracing header.
60+
package var traceID: String {
61+
self.storage.traceID
62+
}
63+
64+
/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
65+
package var invokedFunctionARN: String {
66+
self.storage.invokedFunctionARN
67+
}
68+
69+
/// The timestamp that the function times out.
70+
package var deadline: DispatchWallTime {
71+
self.storage.deadline
72+
}
73+
74+
/// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
75+
package var cognitoIdentity: String? {
76+
self.storage.cognitoIdentity
77+
}
78+
79+
/// For invocations from the AWS Mobile SDK, data about the client application and device.
80+
package var clientContext: String? {
81+
self.storage.clientContext
82+
}
83+
84+
/// `Logger` to log with.
85+
///
86+
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
87+
package var logger: Logger {
88+
self.storage.logger
89+
}
90+
91+
init(
92+
requestID: String,
93+
traceID: String,
94+
invokedFunctionARN: String,
95+
deadline: DispatchWallTime,
96+
cognitoIdentity: String? = nil,
97+
clientContext: String? = nil,
98+
logger: Logger
99+
) {
100+
self.storage = _Storage(
101+
requestID: requestID,
102+
traceID: traceID,
103+
invokedFunctionARN: invokedFunctionARN,
104+
deadline: deadline,
105+
cognitoIdentity: cognitoIdentity,
106+
clientContext: clientContext,
107+
logger: logger
108+
)
109+
}
110+
111+
package func getRemainingTime() -> TimeAmount {
112+
let deadline = self.deadline.millisSinceEpoch
113+
let now = DispatchWallTime.now().millisSinceEpoch
114+
115+
let remaining = deadline - now
116+
return .milliseconds(remaining)
117+
}
118+
119+
package var debugDescription: String {
120+
"\(Self.self)(requestID: \(self.requestID), traceID: \(self.traceID), invokedFunctionARN: \(self.invokedFunctionARN), cognitoIdentity: \(self.cognitoIdentity ?? "nil"), clientContext: \(self.clientContext ?? "nil"), deadline: \(self.deadline))"
121+
}
122+
123+
/// This interface is not part of the public API and must not be used by adopters. This API is not part of semver versioning.
124+
package static func __forTestsOnly(
125+
requestID: String,
126+
traceID: String,
127+
invokedFunctionARN: String,
128+
timeout: DispatchTimeInterval,
129+
logger: Logger,
130+
eventLoop: EventLoop
131+
) -> NewLambdaContext {
132+
NewLambdaContext(
133+
requestID: requestID,
134+
traceID: traceID,
135+
invokedFunctionARN: invokedFunctionARN,
136+
deadline: .now() + timeout,
137+
logger: logger
138+
)
139+
}
140+
}

0 commit comments

Comments
 (0)