summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/ExponentialBackoff.swift
blob: a765d5301106802385b0358bffcc980b0992ac84 (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
54
//
//  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: REST.Duration
    private let multiplier: UInt64
    private let maxDelay: REST.Duration?

    init(initial: REST.Duration, multiplier: UInt64, maxDelay: REST.Duration? = nil) {
        _next = initial
        self.multiplier = multiplier
        self.maxDelay = maxDelay
    }

    mutating func next() -> REST.Duration? {
        let next = _next

        if let maxDelay = maxDelay, next > maxDelay {
            return maxDelay
        }

        _next = next * multiplier

        return next
    }
}

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

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

    mutating func next() -> REST.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(UInt64(Double(millis) * jitter))

        return .milliseconds(millisWithJitter)
    }
}