summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/RetryStrategy/ExponentialBackoff.swift
blob: 8505e092b20fde31b11807eab37499089a2a78be (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 © 2025 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) {
        _next = initial
        self.multiplier = multiplier
        self.maxDelay = maxDelay
    }

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

        if next > maxDelay {
            return maxDelay
        }

        _next = next * Int(multiplier)

        return next
    }
}