Skip to content

Commit 96df91d

Browse files
committed
1 parent 48525aa commit 96df91d

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

Diff for: Sources/TencentSCFRuntimeCore/SCFContext.swift

+53-19
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,55 @@ extension SCF {
6363
extension SCF {
6464
/// SCF runtime context.
6565
/// 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+
6796
/// 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+
}
69100

70101
/// The memory limit of the cloud function in MB.
71-
public let memoryLimit: UInt
102+
public var memoryLimit: UInt {
103+
self.storage.memoryLimit
104+
}
72105

73106
/// 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+
}
75110

76111
/// The timestamp that the function times out.
77-
public let deadline: DispatchWallTime
112+
public var deadline: DispatchWallTime {
113+
self.storage.deadline
114+
}
78115

79116
/// The UIN of cloud function actor.
80117
public var uin: String {
@@ -121,18 +158,24 @@ extension SCF {
121158
/// `Logger` to log with.
122159
///
123160
/// - 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+
}
125164

126165
/// The `EventLoop` the SCF function is executed on. Use this to schedule work with.
127166
/// This is useful when implementing the `EventLoopSCFHandler` protocol.
128167
///
129168
/// - Note: The `EventLoop` is shared with the SCF Runtime Engine and should be handled with extra care.
130169
/// Most importantly the `EventLoop` must never be blocked.
131-
public let eventLoop: EventLoop
170+
public var eventLoop: EventLoop {
171+
self.storage.eventLoop
172+
}
132173

133174
/// `ByteBufferAllocator` to allocate `ByteBuffer`.
134175
/// This is useful when implementing `EventLoopSCFHandler`.
135-
public let allocator: ByteBufferAllocator
176+
public var allocator: ByteBufferAllocator {
177+
self.storage.allocator
178+
}
136179

137180
internal init(requestID: String,
138181
memoryLimit: UInt,
@@ -141,17 +184,8 @@ extension SCF {
141184
eventLoop: EventLoop,
142185
allocator: ByteBufferAllocator)
143186
{
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)
155189
}
156190

157191
public func getRemainingTime() -> TimeAmount {

Diff for: Sources/TencentSCFRuntimeCore/SCFRunner.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ extension SCF {
118118
}
119119

120120
extension SCF.Context {
121-
fileprivate convenience init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: SCF.Invocation) {
121+
init(logger: Logger, eventLoop: EventLoop, allocator: ByteBufferAllocator, invocation: SCF.Invocation) {
122122
self.init(requestID: invocation.requestID,
123123
memoryLimit: invocation.memoryLimit,
124124
timeLimit: .milliseconds(Int(invocation.timeLimit)),

0 commit comments

Comments
 (0)