From a9b1eea2942f4c212ad5b48d05daec4bda2338d9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 8 Jan 2025 21:05:51 +0000 Subject: [PATCH] feat: add module experimental::kxio with backoff macro --- src/experimental/kxio/backoff.rs | 36 ++++++++++++++++++++++++++++++++ src/experimental/kxio/mod.rs | 1 + src/experimental/mod.rs | 3 +++ 3 files changed, 40 insertions(+) create mode 100644 src/experimental/kxio/backoff.rs create mode 100644 src/experimental/kxio/mod.rs diff --git a/src/experimental/kxio/backoff.rs b/src/experimental/kxio/backoff.rs new file mode 100644 index 0000000..1ee95c0 --- /dev/null +++ b/src/experimental/kxio/backoff.rs @@ -0,0 +1,36 @@ +// +#[macro_export] +macro_rules! backoff { + ($backoff_secs:expr) => {{ + $backoff_secs *= 2; + let jitter = rand::random::() % 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, + } + } + }}; +} diff --git a/src/experimental/kxio/mod.rs b/src/experimental/kxio/mod.rs new file mode 100644 index 0000000..ed38d3e --- /dev/null +++ b/src/experimental/kxio/mod.rs @@ -0,0 +1 @@ +mod backoff; diff --git a/src/experimental/mod.rs b/src/experimental/mod.rs index fdd51a9..3571261 100644 --- a/src/experimental/mod.rs +++ b/src/experimental/mod.rs @@ -1,3 +1,6 @@ // +#[cfg(feature = "use-kxio")] +mod kxio; + mod to_string;