Move retry backoff into codex-async-utils (#46330)

Move the exponential backoff helper into `codex-async-utils` so
`codex-cloud-config` can use it without a runtime dependency on `codex-core`.
Keep `codex-core` as a development dependency for cloud-config tests.

Preserve the existing retry delays and jitter, and re-export `backoff` from
`codex_core::util` for existing callers.

GitOrigin-RevId: 338f3194e166e77003da532310ff5be78a0eac9e
This commit is contained in:
Michael Bolin
2026-09-18 00:25:37 +00:00
committed by copyberry
parent 608e4cc9a1
commit ff73d63e64
7 changed files with 28 additions and 15 deletions
+1
View File
@@ -8,6 +8,7 @@ license.workspace = true
workspace = true
[dependencies]
rand = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "time"] }
tokio-util.workspace = true
+17
View File
@@ -0,0 +1,17 @@
//! Shared exponential retry delays with jitter.
use std::time::Duration;
use rand::Rng;
const INITIAL_DELAY_MS: u64 = 200;
const BACKOFF_FACTOR: f64 = 2.0;
/// Return a retry delay starting at 200 ms and doubling on each subsequent attempt,
/// with up to 10% jitter. Attempts zero and one both use the initial delay.
pub fn backoff(attempt: u64) -> Duration {
let exp = BACKOFF_FACTOR.powi(attempt.saturating_sub(1) as i32);
let base = (INITIAL_DELAY_MS as f64 * exp) as u64;
let jitter = rand::rng().random_range(0.9..1.1);
Duration::from_millis((base as f64 * jitter) as u64)
}
+4
View File
@@ -1,3 +1,7 @@
mod backoff;
pub use backoff::backoff;
use std::future::Future;
use tokio_util::sync::CancellationToken;