SPB Git

Repository Public

VQClock.swift 1.3 KB 33 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/VQuantKit/Core/VQClock.swift3// Description: Time as a dependency, so reconnect back-off and job polling are deterministic in tests.4import Foundation56public protocol VQClock: Sendable {7    func now() -> Date8    /// Suspends for the duration; throws `CancellationError` when the task is cancelled.9    func sleep(seconds: TimeInterval) async throws10}1112public struct VQSystemClock: VQClock {13    public init() {}14    public func now() -> Date { Date() }15    public func sleep(seconds: TimeInterval) async throws {16        try await Task.sleep(nanoseconds: UInt64(max(0, seconds) * 1_000_000_000))17    }18}1920/// Exponential back-off: `initial × multiplier^attempt`, capped.21public struct VQBackoff: Sendable, Equatable {22    public var initial: TimeInterval23    public var multiplier: Double24    public var maximum: TimeInterval25    public init(initial: TimeInterval = 1, multiplier: Double = 2, maximum: TimeInterval = 60) {26        self.initial = initial; self.multiplier = multiplier; self.maximum = maximum27    }28    /// Delay before retry number `attempt` (0 = first retry).29    public func delay(attempt: Int) -> TimeInterval {30        min(maximum, initial * pow(multiplier, Double(max(0, min(attempt, 30)))))31    }32}33
Files main
Menu

Theme