SPB Git

spb/zyquo-router Public MIT

One local endpoint, every AI provider — a private OpenAI-compatible LLM gateway for your Mac (170 models, 12 providers).

Swift 95.7% Python 2.3% Shell 1.2% Makefile 0.9%
1.1 KB · 35 lines swift
Raw Blame History
1//2//  RetryPolicy.swift3//  Zyquo Router4//5//  Author: Simon-Pierre Boucher6//  Mail: contact@spboucher.ai7//8//  Exponential backoff with jitter for transient upstream failures (429/5xx,9//  timeouts). Never retries once the first streamed byte has been forwarded10//  to the client — enforced by the caller in Phase 3's UpstreamCall path.11//1213import Foundation1415struct RetryPolicy: Sendable {16    var maxAttempts = 317    var baseDelay: TimeInterval = 0.518    var maxDelay: TimeInterval = 8.01920    /// Whether a failed attempt with this upstream status is worth retrying.21    func shouldRetry(status: Int, attempt: Int) -> Bool {22        guard attempt < maxAttempts else { return false }23        return status == 429 || (500...599).contains(status)24    }2526    /// Full-jitter exponential backoff; honors an upstream Retry-After when given.27    func delay(attempt: Int, retryAfter: TimeInterval? = nil) -> TimeInterval {28        if let retryAfter, retryAfter > 0 {29            return min(retryAfter, maxDelay)30        }31        let exponential = min(baseDelay * pow(2, Double(attempt - 1)), maxDelay)32        return Double.random(in: 0...exponential)33    }34}35