|
| 1 | +// |
| 2 | +// TaskTimeout.swift |
| 3 | +// TaskTimeout |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 31/08/2024. |
| 6 | +// Copyright 2024 Simon Whitty |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/TaskTimeout |
| 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 | +import Foundation |
| 33 | + |
| 34 | +public struct TimeoutError: LocalizedError { |
| 35 | + public var errorDescription: String? |
| 36 | + |
| 37 | + public init(timeout: TimeInterval) { |
| 38 | + self.errorDescription = "Task timed out before completion. Timeout: \(timeout) seconds." |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#if compiler(>=6.0) |
| 43 | +public func withThrowingTimeout<T>( |
| 44 | + isolation: isolated (any Actor)? = #isolation, |
| 45 | + seconds: TimeInterval, |
| 46 | + body: () async throws -> sending T |
| 47 | +) async throws -> sending T { |
| 48 | + let transferringBody = { try await Transferring(body()) } |
| 49 | + typealias NonSendableClosure = () async throws -> Transferring<T> |
| 50 | + typealias SendableClosure = @Sendable () async throws -> Transferring<T> |
| 51 | + return try await withoutActuallyEscaping(transferringBody) { |
| 52 | + (_ fn: @escaping NonSendableClosure) async throws -> Transferring<T> in |
| 53 | + let sendableFn = unsafeBitCast(fn, to: SendableClosure.self) |
| 54 | + return try await _withThrowingTimeout(isolation: isolation, seconds: seconds, body: sendableFn) |
| 55 | + }.value |
| 56 | +} |
| 57 | + |
| 58 | +// Sendable |
| 59 | +private func _withThrowingTimeout<T: Sendable>( |
| 60 | + isolation: isolated (any Actor)? = #isolation, |
| 61 | + seconds: TimeInterval, |
| 62 | + body: @Sendable @escaping () async throws -> T |
| 63 | +) async throws -> T { |
| 64 | + try await withThrowingTaskGroup(of: T.self) { group in |
| 65 | + group.addTask { |
| 66 | + try await body() |
| 67 | + } |
| 68 | + group.addTask { |
| 69 | + try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000)) |
| 70 | + throw TimeoutError(timeout: seconds) |
| 71 | + } |
| 72 | + let success = try await group.next()! |
| 73 | + group.cancelAll() |
| 74 | + return success |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +private struct Transferring<Value>: Sendable { |
| 79 | + |
| 80 | + nonisolated(unsafe) public var value: Value |
| 81 | + |
| 82 | + init(_ value: Value) { |
| 83 | + self.value = value |
| 84 | + } |
| 85 | +} |
| 86 | +#else |
| 87 | +public func withThrowingTimeout<T>( |
| 88 | + seconds: TimeInterval, |
| 89 | + body: () async throws -> T |
| 90 | +) async throws -> T { |
| 91 | + let transferringBody = { try await Transferring(body()) } |
| 92 | + typealias NonSendableClosure = () async throws -> Transferring<T> |
| 93 | + typealias SendableClosure = @Sendable () async throws -> Transferring<T> |
| 94 | + return try await withoutActuallyEscaping(transferringBody) { |
| 95 | + (_ fn: @escaping NonSendableClosure) async throws -> Transferring<T> in |
| 96 | + let sendableFn = unsafeBitCast(fn, to: SendableClosure.self) |
| 97 | + return try await _withThrowingTimeout(seconds: seconds, body: sendableFn) |
| 98 | + }.value |
| 99 | +} |
| 100 | + |
| 101 | +// Sendable |
| 102 | +private func _withThrowingTimeout<T: Sendable>( |
| 103 | + seconds: TimeInterval, |
| 104 | + body: @Sendable @escaping () async throws -> T |
| 105 | +) async throws -> T { |
| 106 | + try await withThrowingTaskGroup(of: T.self) { group in |
| 107 | + group.addTask { |
| 108 | + try await body() |
| 109 | + } |
| 110 | + group.addTask { |
| 111 | + try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000)) |
| 112 | + throw TimeoutError(timeout: seconds) |
| 113 | + } |
| 114 | + let success = try await group.next()! |
| 115 | + group.cancelAll() |
| 116 | + return success |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +private struct Transferring<Value>: @unchecked Sendable { |
| 121 | + |
| 122 | + var value: Value |
| 123 | + |
| 124 | + init(_ value: Value) { |
| 125 | + self.value = value |
| 126 | + } |
| 127 | +} |
| 128 | +#endif |
0 commit comments