Skip to content

Commit ac29098

Browse files
authored
Reuse DispatchQueue for multiple Invocations (#49)
should fix #39
1 parent 87121ec commit ac29098

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sources/AWSLambdaRuntime/LambdaHandler.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,28 @@ 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+
var offloadQueue: 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 defaultOffloadQueue = DispatchQueue(label: "LambdaHandler.offload")
35+
}
36+
3137
public extension LambdaHandler {
38+
/// The queue on which `handle` is invoked on.
39+
var offloadQueue: DispatchQueue {
40+
Lambda.defaultOffloadQueue
41+
}
42+
3243
/// `LambdaHandler` is offloading the processing to a `DispatchQueue`
3344
/// This is slower but safer, in case the implementation blocks the `EventLoop`
3445
/// Performance sensitive Lambdas should be based on `EventLoopLambdaHandler` which does not offload.
3546
func handle(context: Lambda.Context, payload: In) -> EventLoopFuture<Out> {
3647
let promise = context.eventLoop.makePromise(of: Out.self)
3748
// FIXME: reusable DispatchQueue
38-
DispatchQueue(label: "LambdaHandler.offload").async {
49+
self.offloadQueue.async {
3950
self.handle(context: context, payload: payload, callback: promise.completeWith)
4051
}
4152
return promise.futureResult

0 commit comments

Comments
 (0)