-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetryIf.swift
288 lines (254 loc) · 9.12 KB
/
RetryIf.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import Combine
import Foundation
import Synchronized
public extension Publisher {
func retry<Context: Scheduler>(
after interval: @escaping (Int) -> Context.SchedulerTimeType.Stride,
tolerance: Context.SchedulerTimeType.Stride? = nil,
scheduler: Context,
options: Context.SchedulerOptions? = nil
) -> Publishers.RetryIf<Self, Context> {
retryIf(
{ _ in true },
after: interval,
tolerance: tolerance,
scheduler: scheduler,
options: options
)
}
func retryIf<Context: Scheduler>(
_ predicate: @escaping (Failure) -> Bool,
after interval: Context.SchedulerTimeType.Stride,
tolerance: Context.SchedulerTimeType.Stride? = nil,
scheduler: Context,
options: Context.SchedulerOptions? = nil
) -> Publishers.RetryIf<Self, Context> {
retryIf(
predicate,
after: { _ in interval },
tolerance: tolerance,
scheduler: scheduler,
options: options
)
}
func retryIf<Context: Scheduler>(
_ predicate: @escaping (Failure) -> Bool,
after interval: @escaping (Int) -> Context.SchedulerTimeType.Stride,
tolerance: Context.SchedulerTimeType.Stride? = nil,
scheduler: Context,
options: Context.SchedulerOptions? = nil
) -> Publishers.RetryIf<Self, Context> {
Publishers.RetryIf(
upstream: self,
retryIf: predicate,
after: interval,
tolerance: tolerance ?? scheduler.minimumTolerance,
scheduler: scheduler,
options: options
)
}
}
public extension Publishers {
struct RetryIf<Upstream: Publisher, Context: Scheduler>: Publisher {
public typealias Output = Upstream.Output
public typealias Failure = Upstream.Failure
private let upstream: Upstream
private let predicate: (Failure) -> Bool
private let interval: (Int) -> Context.SchedulerTimeType.Stride
private let tolerance: Context.SchedulerTimeType.Stride
private let scheduler: Context
private let options: Context.SchedulerOptions?
public init(
upstream: Upstream,
retryIf predicate: @escaping (Failure) -> Bool,
after interval: @escaping (Int) -> Context.SchedulerTimeType.Stride,
tolerance: Context.SchedulerTimeType.Stride,
scheduler: Context,
options: Context.SchedulerOptions? = nil
) {
self.upstream = upstream
self.predicate = predicate
self.interval = interval
self.tolerance = tolerance
self.scheduler = scheduler
self.options = options
}
public func receive<S: Subscriber>(
subscriber: S
) where Output == S.Input, Failure == S.Failure {
let subscription = RetryIfSubscription(
upstream: upstream,
retryIf: predicate,
interval: interval,
tolerance: tolerance,
scheduler: scheduler,
options: options,
subscriber: subscriber
)
upstream.receive(subscriber: subscription)
}
}
}
private final class RetryIfSubscription<Upstream, Context, S>: Subscription, Subscriber
where
Upstream: Publisher,
Context: Scheduler,
S: Subscriber,
S.Input == Upstream.Output,
S.Failure == Upstream.Failure
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private enum State {
case waitingForSubscription(S)
case retrying(S)
case ready(S, Subscription)
case completed
var subscriber: S? {
switch self {
case let .waitingForSubscription(sub),
let .retrying(sub),
let .ready(sub, _):
return sub
case .completed:
return nil
}
}
var subscription: Subscription? {
switch self {
case let .ready(_, subscription):
return subscription
case .waitingForSubscription, .retrying, .completed:
return nil
}
}
}
private let upstream: Upstream
private let predicate: (Failure) -> Bool
private let interval: (Int) -> Context.SchedulerTimeType.Stride
private let tolerance: Context.SchedulerTimeType.Stride
private let scheduler: Context
private let options: Context.SchedulerOptions?
private var retries: Int = 0
private var demand: Subscribers.Demand = .none
private var state: State
private let lock = RecursiveLock()
init(
upstream: Upstream,
retryIf predicate: @escaping (Failure) -> Bool,
interval: @escaping (Int) -> Context.SchedulerTimeType.Stride,
tolerance: Context.SchedulerTimeType.Stride,
scheduler: Context,
options: Context.SchedulerOptions? = nil,
subscriber: S
) {
self.upstream = upstream
self.predicate = predicate
self.interval = interval
self.tolerance = tolerance
self.scheduler = scheduler
self.options = options
state = .waitingForSubscription(subscriber)
}
deinit {
cancel()
}
func cancel() {
let subscription: Subscription? = lock.locked {
let sub = state.subscription
state = .completed
return sub
}
subscription?.cancel()
}
func request(_ demand: Subscribers.Demand) {
guard demand > .none else { return }
let subscription: Subscription? = lock.locked {
self.demand += demand
return state.subscription
}
subscription?.request(demand)
}
func receive(subscription: Subscription) {
let subscriberAndDemand: (S?, Subscribers.Demand)? = lock.locked {
switch state {
// If this is the initial call to this method, we need to
// call `subscriber.receive(subscription:)`, which is why
// we return `subscriber` here.
case let .waitingForSubscription(subscriber):
state = .ready(subscriber, subscription)
return (subscriber, demand)
// If we are retrying after receiving a failure, we do not
// want to call `subscriber.receive(subscription:)`, which
// is why we do not return `subscriber` here.
case let .retrying(subscriber):
state = .ready(subscriber, subscription)
return (nil, demand)
case .ready, .completed:
return nil
}
}
guard let (subscriber, demand) = subscriberAndDemand
else { return subscription.cancel() }
subscriber?.receive(subscription: self)
guard demand > .none else { return }
subscription.request(demand)
}
func receive(_ input: Upstream.Output) -> Subscribers.Demand {
let subs: (S, Subscription)? = lock.locked {
guard demand > .none, case let .ready(subscriber, subscription) = state
else { return nil }
demand -= 1
retries = 0
return (subscriber, subscription)
}
guard let (subscriber, subscription) = subs else { return .none }
let newDemand = subscriber.receive(input)
guard newDemand > .none else { return .none }
lock.locked { demand += newDemand }
subscription.request(newDemand)
return .none
}
func receive(completion: Subscribers.Completion<Failure>) {
switch completion {
case .finished:
complete(with: .finished)
case let .failure(error):
let shouldRetry = predicate(error)
guard shouldRetry else {
complete(with: .failure(error))
return
}
let _count: Int? = lock.locked {
guard case let .ready(subscriber, _) = state
else { return nil }
state = .retrying(subscriber)
retries += 1
return retries
}
guard let count = _count else { return complete(with: completion) }
scheduler.schedule(
after: scheduler.now.advanced(by: interval(count)),
tolerance: tolerance,
options: options
) {
// We need to retain `self` strongly in this block
// or else we could be deallocated.
self.upstream.subscribe(self)
}
}
}
private func complete(with completion: Subscribers.Completion<Failure>) {
let subscriberAndSubscription: (S?, Subscription?) = lock.locked {
let oldState = state
state = .completed
return (oldState.subscriber, oldState.subscription)
}
if let subscription = subscriberAndSubscription.1 {
subscription.cancel()
}
if let subscriber = subscriberAndSubscription.0 {
subscriber.receive(completion: completion)
}
}
}