summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/ExponentialBackoff.swift
blob: 5c94735eaa56a38c7e58a2192b3048aaf2757596 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
//  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
    }
}

struct Jittered<InnerIterator: IteratorProtocol>: IteratorProtocol
    where InnerIterator.Element == Duration {
    private var inner: InnerIterator

    init(_ inner: InnerIterator) {
        self.inner = inner
    }

    mutating func next() -> Duration? {
        guard let interval = inner.next() else { return nil }

        let jitter = Double.random(in: 0.0 ... 1.0)
        let millis = interval.milliseconds
        let millisWithJitter = millis.saturatingAddition(Int(Double(millis) * jitter))

        return .milliseconds(millisWithJitter)
    }
}