Skip to content

Commit d55af4a

Browse files
committed
Reuse DispatchQueue for multiple Invocations
should fix #39
1 parent 87121ec commit d55af4a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Sources/AWSLambdaRuntime/LambdaHandler.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,27 @@ import NIO
2525
/// The `EventLoopLambdaHandler` will execute the Lambda on the same `EventLoop` as the core runtime engine, making the processing faster but requires
2626
/// more care from the implementation to never block the `EventLoop`.
2727
public protocol LambdaHandler: EventLoopLambdaHandler {
28+
@inlinable var dispatchQueue: DispatchQueue { get }
29+
2830
func handle(context: Lambda.Context, payload: In, callback: @escaping (Result<Out, Error>) -> Void)
2931
}
3032

33+
private extension Lambda {
34+
static let handlerQueue = DispatchQueue(label: "LambdaHandler.offload")
35+
}
36+
3137
public extension LambdaHandler {
38+
var dispatchQueue: DispatchQueue {
39+
Lambda.handlerQueue
40+
}
41+
3242
/// `LambdaHandler` is offloading the processing to a `DispatchQueue`
3343
/// This is slower but safer, in case the implementation blocks the `EventLoop`
3444
/// Performance sensitive Lambdas should be based on `EventLoopLambdaHandler` which does not offload.
3545
func handle(context: Lambda.Context, payload: In) -> EventLoopFuture<Out> {
3646
let promise = context.eventLoop.makePromise(of: Out.self)
3747
// FIXME: reusable DispatchQueue
38-
DispatchQueue(label: "LambdaHandler.offload").async {
48+
self.dispatchQueue.async {
3949
self.handle(context: context, payload: payload, callback: promise.completeWith)
4050
}
4151
return promise.futureResult

0 commit comments

Comments
 (0)