Skip to content

Timeout Handler #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025
Merged

Timeout Handler #4

merged 2 commits into from
Jun 3, 2025

Conversation

swhitty
Copy link
Owner

@swhitty swhitty commented Jun 2, 2025

Adds an overload that includes a Timeout object that can be used to extend, move or cancel the timeout.

For example, the timeout can be extended by 1 second for every iteration of a sequence;

try await withThrowingTimeout(seconds: 1.0) { timeout in
  for await element in sequence {
    timeout.expire(seconds: 1.0)
    print(element)
  }
}

If possible Timeout will be marked ~Escapable in Swift 6.2, but for now a runtime assertion occurs if the object escapes the closure (via a canary and withoutActuallyEscaping)

Variants

Timeout is shared across concurrency domains — the isolation of the body closure doing the work and the Task performing and processing the timeout. Therefore, a flag is returned indicating if the timeout was in fact extended or if the timeout had already expired (and was unwinding);

let didExtend = timeout.expire(seconds: 1.0)

A deadline can also be used:

let didExtend = timeout.expire(after: .now + .seconds(1.0))

The timeout can also be cancelled indefinitely:

let didCancel = timeout.cancelExpiration()

Or expired immediately preventing any more extensions:

let didExpire = timeout.expireImmediatley()

@codecov-commenter
Copy link

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@swhitty swhitty force-pushed the timeout-handler branch 2 times, most recently from 9ea07b9 to 2efd629 Compare June 2, 2025 06:36
@swhitty swhitty linked an issue Jun 2, 2025 that may be closed by this pull request
@swhitty swhitty force-pushed the timeout-handler branch from 2efd629 to bffc2ef Compare June 3, 2025 06:05
@swhitty swhitty force-pushed the timeout-handler branch from e9e9551 to 250469c Compare June 3, 2025 06:33
@swhitty swhitty merged commit 1e1f9ce into main Jun 3, 2025
16 checks passed
@swhitty swhitty deleted the timeout-handler branch June 3, 2025 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add "reset timeout" variants
2 participants