Skip to content

Commit 0005639

Browse files
committed
writable, convenient to use for users context
1 parent 7755376 commit 0005639

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed

Diff for: Sources/AWSLambdaRuntimeCore/LambdaContext.swift

+93-27
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import BaggageContext
1516
import Dispatch
1617
import Logging
1718
import NIO
18-
import BaggageContext
1919

2020
// MARK: - InitializationContext
2121

@@ -50,52 +50,118 @@ extension Lambda {
5050
extension Lambda {
5151
/// Lambda runtime context.
5252
/// The Lambda runtime generates and passes the `Context` to the Lambda handler as an argument.
53-
public final class Context: BaggageContext.Context, CustomDebugStringConvertible {
53+
public struct Context: BaggageContext.Context, CustomDebugStringConvertible {
54+
private var storage: _Storage
55+
56+
final class _Storage {
57+
var baggage: Baggage
58+
59+
let invokedFunctionARN: String
60+
let deadline: DispatchWallTime
61+
let cognitoIdentity: String?
62+
let clientContext: String?
63+
64+
var _logger: Logger
65+
66+
let eventLoop: EventLoop
67+
let allocator: ByteBufferAllocator
68+
69+
init(
70+
baggage: Baggage,
71+
invokedFunctionARN: String,
72+
deadline: DispatchWallTime,
73+
cognitoIdentity: String?,
74+
clientContext: String?,
75+
_logger: Logger,
76+
eventLoop: EventLoop,
77+
allocator: ByteBufferAllocator
78+
) {
79+
self.baggage = baggage
80+
self.invokedFunctionARN = invokedFunctionARN
81+
self.deadline = deadline
82+
self.cognitoIdentity = cognitoIdentity
83+
self.clientContext = clientContext
84+
self._logger = _logger
85+
self.eventLoop = eventLoop
86+
self.allocator = allocator
87+
}
88+
}
5489

5590
/// Contains contextual metadata such as request and trace identifiers, along with other information which may
5691
/// be carried throughout asynchronous and cross-node boundaries (e.g. through HTTPClient calls).
57-
public let baggage: Baggage
92+
public var baggage: Baggage {
93+
get {
94+
self.storage.baggage
95+
}
96+
set {
97+
if isKnownUniquelyReferenced(&self.storage) {
98+
self.storage.baggage = newValue
99+
} else {
100+
self.storage = _Storage(
101+
baggage: newValue,
102+
invokedFunctionARN: self.storage.invokedFunctionARN,
103+
deadline: self.storage.deadline,
104+
cognitoIdentity: self.storage.cognitoIdentity,
105+
clientContext: self.storage.clientContext,
106+
_logger: self.storage._logger,
107+
eventLoop: self.storage.eventLoop,
108+
allocator: self.storage.allocator
109+
)
110+
}
111+
}
112+
}
58113

59114
/// The request ID, which identifies the request that triggered the function invocation.
60115
public var requestID: String {
61-
self.baggage.lambdaRequestID
116+
self.storage.baggage.lambdaRequestID
62117
}
63118

64119
/// The AWS X-Ray tracing header.
65120
public var traceID: String {
66-
self.baggage.lambdaTraceID
121+
self.storage.baggage.lambdaTraceID
67122
}
68123

69124
/// The ARN of the Lambda function, version, or alias that's specified in the invocation.
70-
public let invokedFunctionARN: String
125+
public var invokedFunctionARN: String {
126+
self.storage.invokedFunctionARN
127+
}
71128

72129
/// The timestamp that the function times out
73-
public let deadline: DispatchWallTime
130+
public var deadline: DispatchWallTime {
131+
self.storage.deadline
132+
}
74133

75134
/// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
76-
public let cognitoIdentity: String?
135+
public var cognitoIdentity: String? {
136+
self.storage.cognitoIdentity
137+
}
77138

78139
/// For invocations from the AWS Mobile SDK, data about the client application and device.
79-
public let clientContext: String?
140+
public var clientContext: String? {
141+
self.storage.clientContext
142+
}
80143

81144
/// `Logger` to log with, it is automatically populated with `baggage` information (such as `traceID` and `requestID`).
82145
///
83146
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
84147
public var logger: Logger {
85-
self._logger.with(self.baggage)
148+
self.storage._logger.with(self.baggage)
86149
}
87-
private var _logger: Logger
88150

89151
/// The `EventLoop` the Lambda is executed on. Use this to schedule work with.
90152
/// This is useful when implementing the `EventLoopLambdaHandler` protocol.
91153
///
92154
/// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care.
93155
/// Most importantly the `EventLoop` must never be blocked.
94-
public let eventLoop: EventLoop
156+
public var eventLoop: EventLoop {
157+
self.storage.eventLoop
158+
}
95159

96160
/// `ByteBufferAllocator` to allocate `ByteBuffer`
97161
/// This is useful when implementing `EventLoopLambdaHandler`
98-
public let allocator: ByteBufferAllocator
162+
public var allocator: ByteBufferAllocator {
163+
self.storage.allocator
164+
}
99165

100166
internal init(requestID: String,
101167
traceID: String,
@@ -109,15 +175,17 @@ extension Lambda {
109175
var baggage = Baggage.background
110176
baggage.lambdaRequestID = requestID
111177
baggage.lambdaTraceID = traceID
112-
self.baggage = baggage
113-
self.invokedFunctionARN = invokedFunctionARN
114-
self.cognitoIdentity = cognitoIdentity
115-
self.clientContext = clientContext
116-
self.deadline = deadline
117-
// utility
118-
self.eventLoop = eventLoop
119-
self.allocator = allocator
120-
self._logger = logger
178+
self.storage = _Storage(
179+
baggage: baggage,
180+
invokedFunctionARN: invokedFunctionARN,
181+
deadline: deadline,
182+
cognitoIdentity: cognitoIdentity,
183+
clientContext: clientContext,
184+
// utility
185+
_logger: logger,
186+
eventLoop: eventLoop,
187+
allocator: allocator
188+
)
121189
}
122190

123191
public func getRemainingTime() -> TimeAmount {
@@ -161,7 +229,6 @@ extension Lambda {
161229
// MARK: - Baggage Items
162230

163231
extension Baggage {
164-
165232
// MARK: - Baggage: RequestID
166233

167234
enum LambdaRequestIDKey: Key {
@@ -172,9 +239,9 @@ extension Baggage {
172239
/// The request ID, which identifies the request that triggered the function invocation.
173240
public internal(set) var lambdaRequestID: String {
174241
get {
175-
return self[LambdaRequestIDKey.self]! // !-safe, the runtime guarantees to always set an identifier, even in testing
242+
self[LambdaRequestIDKey.self]! // !-safe, the runtime guarantees to always set an identifier, even in testing
176243
}
177-
set {
244+
set {
178245
self[LambdaRequestIDKey.self] = newValue
179246
}
180247
}
@@ -189,11 +256,10 @@ extension Baggage {
189256
/// The AWS X-Ray tracing header.
190257
public internal(set) var lambdaTraceID: String {
191258
get {
192-
return self[LambdaTraceIDKey.self]! // !-safe, the runtime guarantees to always set an identifier, even in testing
259+
self[LambdaTraceIDKey.self]! // !-safe, the runtime guarantees to always set an identifier, even in testing
193260
}
194261
set {
195262
self[LambdaTraceIDKey.self] = newValue
196263
}
197264
}
198-
199265
}

Diff for: Sources/AWSLambdaRuntimeCore/LambdaRunner.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ extension Lambda {
101101
}
102102

103103
private extension Lambda.Context {
104-
convenience init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Lambda.Invocation) {
104+
init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: Lambda.Invocation) {
105105
self.init(requestID: invocation.requestID,
106106
traceID: invocation.traceID,
107107
invokedFunctionARN: invocation.invokedFunctionARN,

0 commit comments

Comments
 (0)