@@ -25,16 +25,12 @@ import Foundation
25
25
26
26
// This is our guardian to ensure only one LambdaRuntime is running at the time
27
27
// We use an Atomic here to ensure thread safety
28
- private let _isRunning = Atomic < Bool > ( false )
28
+ fileprivate let _isRunning = Atomic < Bool > ( false )
29
29
30
- // We need `@unchecked` Sendable here, as `NIOLockedValueBox` does not understand `sending` today.
31
- // We don't want to use `NIOLockedValueBox` here anyway. We would love to use Mutex here, but this
32
- // sadly crashes the compiler today (on Linux).
33
- // See https://github.com/swiftlang/swift/issues/80036
30
+ // we use unchecked here because Handler are not Sendable
34
31
public final class LambdaRuntime < Handler> : @unchecked Sendable where Handler: StreamingLambdaHandler {
35
- // TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
36
32
@usableFromInline
37
- let handlerMutex : NIOLockedValueBox < Handler ? >
33
+ let handler : Handler
38
34
@usableFromInline
39
35
let logger : Logger
40
36
@usableFromInline
@@ -45,7 +41,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
45
41
eventLoop: EventLoop = Lambda . defaultEventLoop,
46
42
logger: Logger = Logger ( label: " LambdaRuntime " )
47
43
) {
48
- self . handlerMutex = NIOLockedValueBox ( handler)
44
+ self . handler = handler
49
45
self . eventLoop = eventLoop
50
46
51
47
// by setting the log level here, we understand it can not be changed dynamically at runtime
@@ -77,23 +73,13 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
77
73
78
74
// if the original value was already true, run() is already running
79
75
if original {
80
- throw LambdaRuntimeError ( code: . moreThanOneLambdaRuntimeInstance )
76
+ throw LambdaRuntimeError ( code: . runtimeCanOnlyBeStartedOnce )
81
77
}
82
78
83
79
defer {
84
80
_isRunning. store ( false , ordering: . releasing)
85
81
}
86
82
87
- let handler = self . handlerMutex. withLockedValue { handler in
88
- let result = handler
89
- handler = nil
90
- return result
91
- }
92
-
93
- guard let handler else {
94
- throw LambdaRuntimeError ( code: . runtimeCanOnlyBeStartedOnce)
95
- }
96
-
97
83
// are we running inside an AWS Lambda runtime environment ?
98
84
// AWS_LAMBDA_RUNTIME_API is set when running on Lambda
99
85
// https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
@@ -132,7 +118,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
132
118
) { runtimeClient in
133
119
try await Lambda . runLoop (
134
120
runtimeClient: runtimeClient,
135
- handler: handler,
121
+ handler: self . handler,
136
122
logger: self . logger
137
123
)
138
124
}
0 commit comments