|
1 |
| -# swift-concurrency-deadline |
| 1 | +# Deadline |
| 2 | +A deadline algorithm for Swift Concurrency. |
| 3 | + |
| 4 | +## Rationale |
| 5 | +As I've previously stated on the [Swift forums](https://forums.swift.org/t/my-experience-with-concurrency/73197): in my opinion deadlines or timeouts are a missing piece in Swift's Concurrency system. |
| 6 | + |
| 7 | +Since this algorithm is not easy to get right and the implementation in [swift-async-algorithms](https://github.com/apple/swift-async-algorithms/pull/215) has been laying around without getting merged for quite some time now, I decided to open-source my implementation. |
| 8 | + |
| 9 | +## Details |
| 10 | +It will start a `TaskGroup` with two child tasks: your operation and a `Task.sleep(until:tolerance:clock:)`. There are three possible outcomes: |
| 11 | +1. If your operation finishes first, it will simply return the result and cancel the sleeping. |
| 12 | +2. If the sleeping finishes first, it will throw a `DeadlineExceededError` and cancel your operation. |
| 13 | +3. If the parent task was cancelled, it will automatically cancel your operation and the sleeping. The cancellation handling will be inferred from your operation. `CancellationError`s from `Task.sleep(until:tolerance:clock:)` will be ignored. |
| 14 | + |
| 15 | +> [!CAUTION] |
| 16 | +> The operation closure must support cooperative cancellation. |
| 17 | +> Otherwise, `withDeadline(until:tolerance:clock:operation:)` will suspend execution until the operation completes, making the deadline ineffective. |
| 18 | +
|
| 19 | +The library comes with two free functions, one with a generic clock. And another one which uses the `ContinuousClock` as default. |
| 20 | +```swift |
| 21 | +public func withDeadline<C, T>( |
| 22 | + until instant: C.Instant, |
| 23 | + tolerance: C.Instant.Duration? = nil, |
| 24 | + clock: C, |
| 25 | + operation: @escaping @Sendable () async throws -> T |
| 26 | +) async throws -> T where C: Clock, T: Sendable { ... } |
| 27 | + |
| 28 | +public func withDeadline<T>( |
| 29 | + until instant: ContinuousClock.Instant, |
| 30 | + tolerance: ContinuousClock.Instant.Duration? = nil, |
| 31 | + operation: @escaping @Sendable () async throws -> T |
| 32 | +) async throws -> T where T: Sendable { ... } |
| 33 | +``` |
| 34 | + |
| 35 | +## Example |
| 36 | +This is just a demonstrative usage of this function. `CBCentralManager.connect(_:)` is a good example, in my opinion, since it does not support timeouts natively. |
| 37 | + |
| 38 | +Again, if you try to make something like `CBCentralManager.connect(_:)` asynchronous and use it with `withDeadline(until:tolerance:clock:operation:)` be sure to use `withTaskCancellationHandler(operation:onCancel:)` at some point to opt into cooperative cancellation. |
| 39 | +```swift |
| 40 | +try await withDeadline(until: .now + seconds(5), clock: .continous) { |
| 41 | + try await cbCentralManager.connect(peripheral) |
| 42 | +} |
| 43 | +``` |
0 commit comments