Skip to content

Commit ac583cc

Browse files
committed
Add new LambdaContext
1 parent 0f68ed5 commit ac583cc

File tree

1 file changed

+145
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)