-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeoutError.swift
More file actions
78 lines (75 loc) · 3.07 KB
/
Copy pathTimeoutError.swift
File metadata and controls
78 lines (75 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// TimeoutError.swift
// Principle
//
// Created by Kamil Strzelecki on 01/02/2025.
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
//
/// An error that indicates an operation did not complete within the time limit.
///
/// This error is thrown by ``withTimeout(_:tolerance:priority:isolation:operation:)`` function.
///
public struct TimeoutError: Error, Equatable {}
/// A function which ensures execution is resumed by the caller within a time limit or immediately after it has passed.
///
/// - Parameters:
/// - duration: A maximum amount of time `operation` can execute for, after which it will be cancelled.
/// - clock: A clock used to measure the time.
/// - priority: The priority of the task.
/// - operation: The operation to perform.
/// - Throws: Error thrown by the `operation` or ``TimeoutError``.
/// - Returns: Value returned by the `operation`.
///
/// If `operation` is running for longer than `duration`, the child `Task` running it will be cancelled.
/// It’s the responsibility of the code running as part of the `operation` to check for cancellation to stop processing.
///
/// Any value or error produced by the `operation` after cancellation will be ignored.
///
public func withTimeout<C: Clock, Success: Sendable>(
_ duration: C.Instant.Duration,
tolerance: C.Instant.Duration? = nil,
clock: C,
priority: TaskPriority? = nil,
isolation: isolated (any Actor)? = #isolation,
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping () async throws -> Success
) async throws -> Success {
try await withTimeLimit(
throwing: TimeoutError(),
after: clock.now.advanced(by: duration),
tolerance: tolerance,
clock: clock,
priority: priority,
isolation: isolation,
operation: operation
)
}
/// A function which ensures execution is resumed by the caller within a time limit or immediately after it has passed.
///
/// - Parameters:
/// - duration: A maximum amount of time `operation` can execute for, after which it will be cancelled.
/// - priority: The priority of the task.
/// - operation: The operation to perform.
/// - Throws: Error thrown by the `operation` or ``TimeoutError``.
/// - Returns: Value returned by the `operation`.
///
/// If `operation` is running for longer than `duration`, the child `Task` running it will be cancelled.
/// It’s the responsibility of the code running as part of the `operation` to check for cancellation to stop processing.
///
/// Any value or error produced by the `operation` after cancellation will be ignored.
///
public func withTimeout<Success: Sendable>(
_ duration: Duration,
tolerance: Duration? = nil,
priority: TaskPriority? = nil,
isolation: isolated (any Actor)? = #isolation,
@_inheritActorContext @_implicitSelfCapture operation: sending @escaping () async throws -> Success
) async throws -> Success {
try await withTimeout(
duration,
tolerance: tolerance,
clock: .continuous,
priority: priority,
isolation: isolation,
operation: operation
)
}