@@ -25,17 +25,28 @@ import NIO
25
25
/// The `EventLoopLambdaHandler` will execute the Lambda on the same `EventLoop` as the core runtime engine, making the processing faster but requires
26
26
/// more care from the implementation to never block the `EventLoop`.
27
27
public protocol LambdaHandler : EventLoopLambdaHandler {
28
+ var offloadQueue : DispatchQueue { get }
29
+
28
30
func handle( context: Lambda . Context , payload: In , callback: @escaping ( Result < Out , Error > ) -> Void )
29
31
}
30
32
33
+ private extension Lambda {
34
+ static let defaultOffloadQueue = DispatchQueue ( label: " LambdaHandler.offload " )
35
+ }
36
+
31
37
public extension LambdaHandler {
38
+ /// The queue on which `handle` is invoked on.
39
+ var offloadQueue : DispatchQueue {
40
+ Lambda . defaultOffloadQueue
41
+ }
42
+
32
43
/// `LambdaHandler` is offloading the processing to a `DispatchQueue`
33
44
/// This is slower but safer, in case the implementation blocks the `EventLoop`
34
45
/// Performance sensitive Lambdas should be based on `EventLoopLambdaHandler` which does not offload.
35
46
func handle( context: Lambda . Context , payload: In ) -> EventLoopFuture < Out > {
36
47
let promise = context. eventLoop. makePromise ( of: Out . self)
37
48
// FIXME: reusable DispatchQueue
38
- DispatchQueue ( label : " LambdaHandler.offload " ) . async {
49
+ self . offloadQueue . async {
39
50
self . handle ( context: context, payload: payload, callback: promise. completeWith)
40
51
}
41
52
return promise. futureResult
0 commit comments