12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
+ import BaggageContext
15
16
import Dispatch
16
17
import Logging
17
18
import NIO
18
- import BaggageContext
19
19
20
20
// MARK: - InitializationContext
21
21
@@ -50,52 +50,118 @@ extension Lambda {
50
50
extension Lambda {
51
51
/// Lambda runtime context.
52
52
/// 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
+ }
54
89
55
90
/// Contains contextual metadata such as request and trace identifiers, along with other information which may
56
91
/// 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
+ }
58
113
59
114
/// The request ID, which identifies the request that triggered the function invocation.
60
115
public var requestID : String {
61
- self . baggage. lambdaRequestID
116
+ self . storage . baggage. lambdaRequestID
62
117
}
63
118
64
119
/// The AWS X-Ray tracing header.
65
120
public var traceID : String {
66
- self . baggage. lambdaTraceID
121
+ self . storage . baggage. lambdaTraceID
67
122
}
68
123
69
124
/// 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
+ }
71
128
72
129
/// The timestamp that the function times out
73
- public let deadline : DispatchWallTime
130
+ public var deadline : DispatchWallTime {
131
+ self . storage. deadline
132
+ }
74
133
75
134
/// 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
+ }
77
138
78
139
/// 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
+ }
80
143
81
144
/// `Logger` to log with, it is automatically populated with `baggage` information (such as `traceID` and `requestID`).
82
145
///
83
146
/// - note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
84
147
public var logger : Logger {
85
- self . _logger. with ( self . baggage)
148
+ self . storage . _logger. with ( self . baggage)
86
149
}
87
- private var _logger : Logger
88
150
89
151
/// The `EventLoop` the Lambda is executed on. Use this to schedule work with.
90
152
/// This is useful when implementing the `EventLoopLambdaHandler` protocol.
91
153
///
92
154
/// - note: The `EventLoop` is shared with the Lambda runtime engine and should be handled with extra care.
93
155
/// Most importantly the `EventLoop` must never be blocked.
94
- public let eventLoop : EventLoop
156
+ public var eventLoop : EventLoop {
157
+ self . storage. eventLoop
158
+ }
95
159
96
160
/// `ByteBufferAllocator` to allocate `ByteBuffer`
97
161
/// This is useful when implementing `EventLoopLambdaHandler`
98
- public let allocator : ByteBufferAllocator
162
+ public var allocator : ByteBufferAllocator {
163
+ self . storage. allocator
164
+ }
99
165
100
166
internal init ( requestID: String ,
101
167
traceID: String ,
@@ -109,15 +175,17 @@ extension Lambda {
109
175
var baggage = Baggage . background
110
176
baggage. lambdaRequestID = requestID
111
177
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
+ )
121
189
}
122
190
123
191
public func getRemainingTime( ) -> TimeAmount {
@@ -161,7 +229,6 @@ extension Lambda {
161
229
// MARK: - Baggage Items
162
230
163
231
extension Baggage {
164
-
165
232
// MARK: - Baggage: RequestID
166
233
167
234
enum LambdaRequestIDKey : Key {
@@ -172,9 +239,9 @@ extension Baggage {
172
239
/// The request ID, which identifies the request that triggered the function invocation.
173
240
public internal( set) var lambdaRequestID : String {
174
241
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
176
243
}
177
- set {
244
+ set {
178
245
self [ LambdaRequestIDKey . self] = newValue
179
246
}
180
247
}
@@ -189,11 +256,10 @@ extension Baggage {
189
256
/// The AWS X-Ray tracing header.
190
257
public internal( set) var lambdaTraceID : String {
191
258
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
193
260
}
194
261
set {
195
262
self [ LambdaTraceIDKey . self] = newValue
196
263
}
197
264
}
198
-
199
265
}
0 commit comments