p2p-swift / Sources / IrdP2PSignaling / Backoff.swift
103 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import Foundation

/// Reconnect and retry schedules. Pure functions of the attempt number plus an
/// injectable jitter source in `[0, 1)`.
public enum Backoff {
    /// Relay reconnect: `min(15000, 500·2^n) × (0.5 + jitter)` milliseconds.
    public static func relay(attempt: Int, jitter: Double) -> Duration {
        let base = min(15_000.0, 500.0 * pow(2.0, Double(max(0, attempt))))
        return .milliseconds(Int(base * (0.5 + jitter)))
    }

    /// Mailbox retry: `min(300000, 1000·2^n)` milliseconds.
    public static func mailbox(attempt: Int) -> Duration {
        .milliseconds(Int(min(300_000.0, 1000.0 * pow(2.0, Double(max(0, attempt))))))
    }

    /// The ICE-restart ladder after a data channel is lost.
    public static let iceRestartLadder: [Duration] = [.seconds(2), .seconds(5), .seconds(8)]
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/p2p-swift.git