Skip to content

Commit a64f204

Browse files
authored
Update README.md
1 parent 1e34c68 commit a64f204

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

README.md

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ As I've previously stated on the [Swift forums](https://forums.swift.org/t/my-ex
77
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.
88

99
## 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.
1810

1911
The library comes with two free functions, one with a generic clock. And another one which uses the `ContinuousClock` as default.
2012
```swift
@@ -32,12 +24,66 @@ public func withDeadline<T>(
3224
) async throws -> T where T: Sendable { ... }
3325
```
3426

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.
27+
This function provides a mechanism for enforcing timeouts on asynchronous operations that lack native deadline support. It creates a `TaskGroup` with two concurrent tasks: the provided operation and a sleep task.
28+
29+
- Parameters:
30+
- `until`: The absolute deadline for the operation to complete.
31+
- `tolerance`: The allowed tolerance for the deadline.
32+
- `clock`: The clock used for timing the operation.
33+
- `operation`: The asynchronous operation to be executed.
34+
35+
- Returns: The result of the operation if it completes before the deadline.
36+
- Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation itself.
37+
38+
> [!CAUTION]
39+
> The operation closure must support cooperative cancellation. Otherwise, the deadline will not be respected.
40+
41+
### Examples
42+
To fully understand this, let's illustrate the 3 outcomes of this function:
3743

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.
44+
#### Outcome 1
45+
The operation finishes in time:
46+
```swift
47+
let result = try await withDeadline(until: .now + .seconds(5)) {
48+
// Simulate long running task
49+
try await Task.sleep(for: .seconds(1))
50+
return "success"
51+
}
52+
```
53+
As you'd expect, result will be "success". The same applies when your operation fails in time:
3954
```swift
40-
try await withDeadline(until: .now + seconds(5), clock: .continous) {
41-
try await cbCentralManager.connect(peripheral)
55+
let result = try await withDeadline(until: .now + .seconds(5)) {
56+
// Simulate long running task
57+
try await Task.sleep(for: .seconds(1))
58+
throw CustomError()
4259
}
4360
```
61+
This will throw `CustomError`.
62+
63+
#### Outcome 2
64+
The operation does not finish in time:
65+
```swift
66+
let result = try await withDeadline(until: .now + .seconds(1)) {
67+
// Simulate even longer running task
68+
try await Task.sleep(for: .seconds(5))
69+
return "success"
70+
}
71+
```
72+
This will throw `DeadlineExceededError` because the operation will not finish in time.
73+
74+
#### Outcome 3
75+
The parent task was cancelled:
76+
```swift
77+
let task = Task {
78+
do {
79+
try await withDeadline(until: .now + .seconds(5)) {
80+
try await URLSession.shared.data(from: url)
81+
}
82+
} catch {
83+
print(error)
84+
}
85+
}
86+
87+
task.cancel()
88+
```
89+
The print is guaranteed to print `URLError(.cancelled)`.

0 commit comments

Comments
 (0)