Skip to content

Commit 77ad9dc

Browse files
committed
chore: rename withDeadline to deadline
1 parent 8cd10f5 commit 77ad9dc

File tree

7 files changed

+324
-117
lines changed

7 files changed

+324
-117
lines changed

Package.resolved

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ let package = Package(
2323
.product(name: "Clocks", package: "swift-clocks")
2424
],
2525
swiftSettings: [.enableExperimentalFeature("StrictConcurrency")]
26-
),
26+
)
2727
]
2828
)

Package@swift-5.8.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ let package = Package(
2121
"Deadline",
2222
.product(name: "Clocks", package: "swift-clocks")
2323
]
24-
),
24+
)
2525
]
2626
)

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,29 @@ Since this algorithm is not easy to get right and the implementation in [swift-a
1313

1414
The library comes with two free functions, one with a generic clock. And another one which uses the `ContinuousClock` as default.
1515
```swift
16-
public func withDeadline<C, R>(
16+
public func deadline<C, R>(
1717
until instant: C.Instant,
1818
tolerance: C.Instant.Duration? = nil,
1919
clock: C,
20+
isolation: isolated (any Actor)? = #isolation,
2021
operation: @Sendable () async throws -> R
2122
) async throws -> R where C: Clock, R: Sendable { ... }
2223

23-
public func withDeadline<R>(
24+
public func deadline<R>(
2425
until instant: ContinuousClock.Instant,
2526
tolerance: ContinuousClock.Instant.Duration? = nil,
27+
isolation: isolated (any Actor)? = #isolation,
2628
operation: @Sendable () async throws -> R
2729
) async throws -> R where R: Sendable { ... }
2830
```
2931

3032
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.
3133

3234
- Parameters:
33-
- `until`: The absolute deadline for the operation to complete.
35+
- `instant`: The absolute deadline for the operation to complete.
3436
- `tolerance`: The allowed tolerance for the deadline.
3537
- `clock`: The clock used for timing the operation.
38+
- `isolation`: The isolation passed on to the task group.
3639
- `operation`: The asynchronous operation to be executed.
3740

3841
- Returns: The result of the operation if it completes before the deadline.
@@ -47,15 +50,15 @@ To fully understand this, let's illustrate the 3 outcomes of this function:
4750
#### Outcome 1
4851
The operation finishes in time:
4952
```swift
50-
let result = try await withDeadline(until: .now + .seconds(5)) {
53+
let result = try await deadline(until: .now + .seconds(5)) {
5154
// Simulate long running task
5255
try await Task.sleep(for: .seconds(1))
5356
return "success"
5457
}
5558
```
5659
As you'd expect, result will be "success". The same applies when your operation fails in time:
5760
```swift
58-
let result = try await withDeadline(until: .now + .seconds(5)) {
61+
let result = try await deadline(until: .now + .seconds(5)) {
5962
// Simulate long running task
6063
try await Task.sleep(for: .seconds(1))
6164
throw CustomError()
@@ -66,7 +69,7 @@ This will throw `CustomError`.
6669
#### Outcome 2
6770
The operation does not finish in time:
6871
```swift
69-
let result = try await withDeadline(until: .now + .seconds(1)) {
72+
let result = try await deadline(until: .now + .seconds(1)) {
7073
// Simulate even longer running task
7174
try await Task.sleep(for: .seconds(5))
7275
return "success"
@@ -79,7 +82,7 @@ The parent task was cancelled:
7982
```swift
8083
let task = Task {
8184
do {
82-
try await withDeadline(until: .now + .seconds(5)) {
85+
try await deadline(until: .now + .seconds(5)) {
8386
try await URLSession.shared.data(from: url)
8487
}
8588
} catch {

0 commit comments

Comments
 (0)