refactor: unify 3 backoff implementations into shared helper

This commit is contained in:
asepharyana
2026-07-18 03:18:27 +07:00
parent e32501ee59
commit 431c8d3b89
6 changed files with 47 additions and 56 deletions
+5 -27
View File
@@ -26,9 +26,10 @@
use anyhow::Result;
use std::sync::atomic::AtomicBool;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::time::Duration;
use crate::app::runtime::stream::turn::StreamedTurn;
use crate::app::util::backoff::backoff_seconds;
use crate::app::runtime::stream::{SseParser, StreamEvent};
use crate::dto::chat::message::ChatMessage;
use crate::dto::provider::request::{ChatRequest, StreamOptions, ToolDef};
@@ -43,30 +44,11 @@ const REQUEST_TIMEOUT: Duration = Duration::from_mins(1);
// Retry helpers
// ---------------------------------------------------------------------------
/// Return a pseudo-random jitter offset in the range [0, range_ns).
///
/// Uses the full epoch nanoseconds (wrapped to u64) instead of the
/// sub-second component so the jitter range scales with `range_ns`
/// rather than being capped at ~1s.
fn jitter_ns(range_ns: u64) -> u64 {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_nanos() as u64;
nanos % range_ns
}
/// Exponential backoff with ±25% jitter, capped at 30 seconds.
///
/// `attempt` is 1-based (first retry → attempt=1).
fn backoff_duration(attempt: u32) -> Duration {
let base_secs = (2u64).pow(attempt).min(30);
let half_range = (base_secs * 250_000_000).max(100_000_000); // 25% of base, min 100ms
let offset = jitter_ns(half_range * 2); // [0, 50% of base)
// ±25% jitter: subtract half_range so the result varies
// between base-25% and base+25%.
let ns = base_secs * 1_000_000_000 + offset - half_range;
Duration::from_nanos(ns)
backoff_seconds(attempt, 30)
}
/// Is the error an auth / billing failure that retrying won't fix?
@@ -95,12 +77,8 @@ fn is_rate_limit(err_str: &str) -> bool {
/// Return a rate-appropriate backoff (longer for 429).
fn backoff_for_error(attempt: u32, err_str: &str) -> Duration {
if is_rate_limit(err_str) {
// Rate limits need more time to drain — start at 5s instead of 2s.
let base_secs = (5u64 * (2u64).pow(attempt.saturating_sub(1))).min(60);
let half_range = (base_secs * 250_000_000).max(100_000_000);
let offset = jitter_ns(half_range * 2);
let ns = base_secs * 1_000_000_000 + offset - half_range;
Duration::from_nanos(ns)
// Rate limits need more time to drain — backoff capped at 60s.
backoff_seconds(attempt, 60)
} else {
backoff_duration(attempt)
}