feat: add module experimental::kxio with backoff macro
All checks were successful
Test / build (map[name:nightly]) (push) Successful in 2m20s
Test / build (map[name:stable]) (push) Successful in 2m12s
Release Please / Release-plz (push) Successful in 42s

This commit is contained in:
Paul Campbell 2025-01-08 21:05:51 +00:00
parent 48713e4e60
commit a9b1eea294
3 changed files with 40 additions and 0 deletions

View file

@ -0,0 +1,36 @@
//
#[macro_export]
macro_rules! backoff {
($backoff_secs:expr) => {{
$backoff_secs *= 2;
let jitter = rand::random::<u64>() % 10;
let backoff_secs = 60.min($backoff_secs + jitter);
tracing::debug(?backoff_secs, "Too many requests, backing off for {}s");
tokio::time::sleep(tokio::time::Duration::from_secs(backoff_secs)).await;
}};
}
#[macro_export]
macro_rules! with_exponential_backoff {
($operation:expr) => {{
let mut backoff_secs = 1;
loop {
match $operation {
Err(kxio::net::Error::Reqwest(err))
if err.status() == Some(kxio::net::StatusCode::TOO_MANY_REQUESTS) =>
{
$crate::backoff!(backoff_secs)
}
Err(kxio::net::Error::ResponseError { response })
if response.status() == kxio::net::StatusCode::TOO_MANY_REQUESTS =>
{
$crate::backoff!(backoff_secs)
}
Ok(response) if response.status() == kxio::net::StatusCode::TOO_MANY_REQUESTS => {
$crate::backoff!(backoff_secs)
}
result => break result,
}
}
}};
}

View file

@ -0,0 +1 @@
mod backoff;

View file

@ -1,3 +1,6 @@
// //
#[cfg(feature = "use-kxio")]
mod kxio;
mod to_string; mod to_string;