@@ -63,18 +63,55 @@ extension SCF {
63
63
extension SCF {
64
64
/// SCF runtime context.
65
65
/// The SCF runtime generates and passes the `Context` to the SCF handler as an argument.
66
- public final class Context : CustomDebugStringConvertible {
66
+ public struct Context : CustomDebugStringConvertible {
67
+ final class _Storage {
68
+ var requestID : String
69
+ var memoryLimit : UInt
70
+ var timeLimit : DispatchTimeInterval
71
+ var deadline : DispatchWallTime
72
+ var logger : Logger
73
+ var eventLoop : EventLoop
74
+ var allocator : ByteBufferAllocator
75
+
76
+ init (
77
+ requestID: String ,
78
+ memoryLimit: UInt ,
79
+ timeLimit: DispatchTimeInterval ,
80
+ logger: Logger ,
81
+ eventLoop: EventLoop ,
82
+ allocator: ByteBufferAllocator
83
+ ) {
84
+ self . requestID = requestID
85
+ self . memoryLimit = memoryLimit
86
+ self . timeLimit = timeLimit
87
+ self . deadline = . now( ) + timeLimit
88
+ self . logger = logger
89
+ self . eventLoop = eventLoop
90
+ self . allocator = allocator
91
+ }
92
+ }
93
+
94
+ private var storage : _Storage
95
+
67
96
/// The request ID, which identifies the request that triggered the function invocation.
68
- public let requestID : String
97
+ public var requestID : String {
98
+ self . storage. requestID
99
+ }
69
100
70
101
/// The memory limit of the cloud function in MB.
71
- public let memoryLimit : UInt
102
+ public var memoryLimit : UInt {
103
+ self . storage. memoryLimit
104
+ }
72
105
73
106
/// The time limit of the cloud function event in ms.
74
- public let timeLimit : DispatchTimeInterval
107
+ public var timeLimit : DispatchTimeInterval {
108
+ self . storage. timeLimit
109
+ }
75
110
76
111
/// The timestamp that the function times out.
77
- public let deadline : DispatchWallTime
112
+ public var deadline : DispatchWallTime {
113
+ self . storage. deadline
114
+ }
78
115
79
116
/// The UIN of cloud function actor.
80
117
public var uin : String {
@@ -121,18 +158,24 @@ extension SCF {
121
158
/// `Logger` to log with.
122
159
///
123
160
/// - Note: The `LogLevel` can be configured using the `LOG_LEVEL` environment variable.
124
- public let logger : Logger
161
+ public var logger : Logger {
162
+ self . storage. logger
163
+ }
125
164
126
165
/// The `EventLoop` the SCF function is executed on. Use this to schedule work with.
127
166
/// This is useful when implementing the `EventLoopSCFHandler` protocol.
128
167
///
129
168
/// - Note: The `EventLoop` is shared with the SCF Runtime Engine and should be handled with extra care.
130
169
/// Most importantly the `EventLoop` must never be blocked.
131
- public let eventLoop : EventLoop
170
+ public var eventLoop : EventLoop {
171
+ self . storage. eventLoop
172
+ }
132
173
133
174
/// `ByteBufferAllocator` to allocate `ByteBuffer`.
134
175
/// This is useful when implementing `EventLoopSCFHandler`.
135
- public let allocator : ByteBufferAllocator
176
+ public var allocator : ByteBufferAllocator {
177
+ self . storage. allocator
178
+ }
136
179
137
180
internal init ( requestID: String ,
138
181
memoryLimit: UInt ,
@@ -141,17 +184,8 @@ extension SCF {
141
184
eventLoop: EventLoop ,
142
185
allocator: ByteBufferAllocator )
143
186
{
144
- self . requestID = requestID
145
- self . memoryLimit = memoryLimit
146
- self . deadline = DispatchWallTime . now ( ) + timeLimit
147
- self . timeLimit = timeLimit
148
- // utilities
149
- self . eventLoop = eventLoop
150
- self . allocator = allocator
151
- // Mutate logger with context.
152
- var logger = logger
153
- logger [ metadataKey: " scfRequestID " ] = . string( requestID)
154
- self . logger = logger
187
+ self . storage = _Storage ( requestID: requestID, memoryLimit: memoryLimit, timeLimit: timeLimit, logger: logger, eventLoop: eventLoop, allocator: allocator)
188
+ self . storage. logger [ metadataKey: " scfRequestID " ] = . string( requestID)
155
189
}
156
190
157
191
public func getRemainingTime( ) -> TimeAmount {
0 commit comments