|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import Foundation |
| 15 | +import NIOConcurrencyHelpers |
| 16 | +import NIOCore |
| 17 | +import Logging |
| 18 | + |
| 19 | +/// A container that allows tasks to finish after a synchronous invocation |
| 20 | +/// has produced its response. |
| 21 | +actor DetachedTasksContainer: Sendable { |
| 22 | + |
| 23 | + struct Context: Sendable { |
| 24 | + let eventLoop: EventLoop |
| 25 | + let logger: Logger |
| 26 | + } |
| 27 | + |
| 28 | + private var context: Context |
| 29 | + private var storage: [RegistrationKey: EventLoopFuture<Void>] = [:] |
| 30 | + |
| 31 | + init(context: Context) { |
| 32 | + self.context = context |
| 33 | + } |
| 34 | + |
| 35 | + /// Adds a detached async task. |
| 36 | + /// |
| 37 | + /// - Parameters: |
| 38 | + /// - name: The name of the task. |
| 39 | + /// - task: The async task to execute. |
| 40 | + /// - Returns: A `RegistrationKey` for the registered task. |
| 41 | + func detached(task: @Sendable @escaping () async -> Void) { |
| 42 | + let key = RegistrationKey() |
| 43 | + let promise = context.eventLoop.makePromise(of: Void.self) |
| 44 | + promise.completeWithTask(task) |
| 45 | + let task = promise.futureResult.always { [weak self] result in |
| 46 | + guard let self else { return } |
| 47 | + Task { |
| 48 | + await self.removeTask(forKey: key) |
| 49 | + } |
| 50 | + } |
| 51 | + self.storage[key] = task |
| 52 | + } |
| 53 | + |
| 54 | + func removeTask(forKey key: RegistrationKey) { |
| 55 | + self.storage.removeValue(forKey: key) |
| 56 | + } |
| 57 | + |
| 58 | + /// Awaits all registered tasks to complete. |
| 59 | + /// |
| 60 | + /// - Returns: An `EventLoopFuture<Void>` that completes when all tasks have finished. |
| 61 | + func awaitAll() -> EventLoopFuture<Void> { |
| 62 | + let tasks = storage.values |
| 63 | + if tasks.isEmpty { |
| 64 | + return context.eventLoop.makeSucceededVoidFuture() |
| 65 | + } else { |
| 66 | + let context = context |
| 67 | + return EventLoopFuture.andAllComplete(Array(tasks), on: context.eventLoop).flatMap { [weak self] in |
| 68 | + guard let self else { |
| 69 | + return context.eventLoop.makeSucceededFuture(()) |
| 70 | + } |
| 71 | + let promise = context.eventLoop.makePromise(of: Void.self) |
| 72 | + promise.completeWithTask { |
| 73 | + try await self.awaitAll().get() |
| 74 | + } |
| 75 | + return promise.futureResult |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +extension DetachedTasksContainer { |
| 82 | + /// Lambda detached task registration key. |
| 83 | + struct RegistrationKey: Hashable, CustomStringConvertible, Sendable { |
| 84 | + var value: String |
| 85 | + |
| 86 | + init() { |
| 87 | + // UUID basically |
| 88 | + self.value = UUID().uuidString |
| 89 | + } |
| 90 | + |
| 91 | + var description: String { |
| 92 | + self.value |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments