blob: 57a54e07a905bfdfe6b1a851b538aca11db3706e (
plain)
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
|
//
// ExponentialBackoff.swift
// MullvadREST
//
// Created by pronebird on 03/11/2022.
// Copyright © 2022 Mullvad VPN AB. All rights reserved.
//
import Foundation
import MullvadTypes
struct ExponentialBackoff: IteratorProtocol {
private var _next: Duration
private let multiplier: UInt64
private let maxDelay: Duration?
init(initial: Duration, multiplier: UInt64, maxDelay: Duration? = nil) {
_next = initial
self.multiplier = multiplier
self.maxDelay = maxDelay
}
mutating func next() -> Duration? {
let next = _next
if let maxDelay, next > maxDelay {
return maxDelay
}
_next = next * Int(multiplier)
return next
}
}
|