|
| 1 | +// |
| 2 | +// Timeout.swift |
| 3 | +// swift-timeout |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 02/06/2025. |
| 6 | +// Copyright 2025 Simon Whitty |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/swift-timeout |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +#if compiler(>=6.0) |
| 33 | + |
| 34 | +public struct Timeout: Sendable { |
| 35 | + fileprivate var canary: @Sendable () -> Void |
| 36 | + fileprivate let state: SharedState |
| 37 | + |
| 38 | + public func expireImmediatley() { |
| 39 | + enqueue { |
| 40 | + throw TimeoutError("Task timed out before completion. expireImmediatley()") |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public func cancelExpiration() { |
| 45 | + enqueue { |
| 46 | + try await Task.sleepIndefinitely() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + struct State { |
| 51 | + var running: Task<Never, any Error>? |
| 52 | + var pending: (@Sendable () async throws -> Never)? |
| 53 | + } |
| 54 | + |
| 55 | + final class SharedState: Sendable { |
| 56 | + let state: Mutex<State> |
| 57 | + |
| 58 | + init(pending: @escaping @Sendable () async throws -> Never) { |
| 59 | + state = Mutex(.init(pending: pending)) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +extension Timeout { |
| 65 | + |
| 66 | + init( |
| 67 | + canary: @escaping @Sendable () -> Void, |
| 68 | + pending closure: @escaping @Sendable () async throws -> Never |
| 69 | + ) { |
| 70 | + self.canary = canary |
| 71 | + self.state = .init(pending: closure) |
| 72 | + } |
| 73 | + |
| 74 | + func enqueue(_ closure: @escaping @Sendable () async throws -> Never) { |
| 75 | + state.state.withLock { s in |
| 76 | + s.pending = closure |
| 77 | + s.running?.cancel() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func startPendingTask() -> Task<Never, any Error>? { |
| 82 | + return state.state.withLock { s in |
| 83 | + guard let pending = s.pending else { return nil } |
| 84 | + let task = Task { try await pending() } |
| 85 | + s.pending = nil |
| 86 | + s.running = task |
| 87 | + return task |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + func waitForTimeout() async throws { |
| 92 | + while let task = startPendingTask() { |
| 93 | + do { |
| 94 | + try await withTaskCancellationHandler { |
| 95 | + try await task.value |
| 96 | + } onCancel: { |
| 97 | + task.cancel() |
| 98 | + } |
| 99 | + } catch is CancellationError { |
| 100 | + continue |
| 101 | + } catch { |
| 102 | + throw error |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func withNonEscapingTimeout<T>( |
| 109 | + _ timeout: @escaping @Sendable () async throws -> Never, |
| 110 | + isolation: isolated (any Actor)? = #isolation, |
| 111 | + body: (Timeout) async throws -> sending T |
| 112 | +) async throws -> sending T { |
| 113 | + // canary ensuring Timeout does not escape at runtime. |
| 114 | + // Swift 6.2 and later enforce at compile time with ~Escapable |
| 115 | + try await withoutActuallyEscaping({ @Sendable in }) { escaping in |
| 116 | + _ = isolation |
| 117 | + let timeout = Timeout(canary: escaping, pending: timeout) |
| 118 | + return try await Transferring(body(timeout)) |
| 119 | + }.value |
| 120 | +} |
| 121 | + |
| 122 | +#endif |
0 commit comments