forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuture.swift
112 lines (88 loc) · 3.8 KB
/
Future.swift
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Atomics
@testable import _ConnectionPoolModule
/// This is a `Future` type that shall make writing tests a bit simpler. I'm well aware, that this is a pattern
/// that should not be embraced with structured concurrency. However writing all tests in full structured
/// concurrency is an effort, that isn't worth the endgoals in my view.
final class Future<Success: Sendable>: Sendable {
struct State: Sendable {
var result: Swift.Result<Success, any Error>? = nil
var continuations: [(Int, CheckedContinuation<Success, any Error>)] = []
}
let waiterID = ManagedAtomic(0)
let stateBox: NIOLockedValueBox<State> = NIOLockedValueBox(State())
init(of: Success.Type) {}
enum GetAction {
case fail(any Error)
case succeed(Success)
case none
}
var success: Success {
get async throws {
let waiterID = self.waiterID.loadThenWrappingIncrement(ordering: .relaxed)
return try await withTaskCancellationHandler {
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Success, any Error>) in
let action = self.stateBox.withLockedValue { state -> GetAction in
if Task.isCancelled {
return .fail(CancellationError())
}
switch state.result {
case .none:
state.continuations.append((waiterID, continuation))
return .none
case .success(let result):
return .succeed(result)
case .failure(let error):
return .fail(error)
}
}
switch action {
case .fail(let error):
continuation.resume(throwing: error)
case .succeed(let result):
continuation.resume(returning: result)
case .none:
break
}
}
} onCancel: {
let cont = self.stateBox.withLockedValue { state -> CheckedContinuation<Success, any Error>? in
guard state.result == nil else { return nil }
guard let contIndex = state.continuations.firstIndex(where: { $0.0 == waiterID }) else {
return nil
}
let (_, continuation) = state.continuations.remove(at: contIndex)
return continuation
}
cont?.resume(throwing: CancellationError())
}
}
}
func yield(value: Success) {
let continuations = self.stateBox.withLockedValue { state in
guard state.result == nil else {
return [(Int, CheckedContinuation<Success, any Error>)]().lazy.map(\.1)
}
state.result = .success(value)
let continuations = state.continuations
state.continuations = []
return continuations.lazy.map(\.1)
}
for continuation in continuations {
continuation.resume(returning: value)
}
}
func yield(error: any Error) {
let continuations = self.stateBox.withLockedValue { state in
guard state.result == nil else {
return [(Int, CheckedContinuation<Success, any Error>)]().lazy.map(\.1)
}
state.result = .failure(error)
let continuations = state.continuations
state.continuations = []
return continuations.lazy.map(\.1)
}
for continuation in continuations {
continuation.resume(throwing: error)
}
}
}