From 8305715471315d7ef22a7e28350e7602b9fe0259 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 22 Dec 2024 15:35:26 +0000 Subject: [PATCH] fix: improve exponential backup detection of 429 error --- src/macros/backoff.rs | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/macros/backoff.rs b/src/macros/backoff.rs index a9e886a..778ebce 100644 --- a/src/macros/backoff.rs +++ b/src/macros/backoff.rs @@ -1,21 +1,34 @@ +// +#[macro_export] +macro_rules! backoff { + ($backoff_secs:expr, $ctx:expr) => {{ + $backoff_secs *= 2; + let jitter = rand::random::() % 10; + let backoff_secs = 60.min($backoff_secs + jitter); + $crate::p!( + $ctx.prt, + ">> Too many requests, backing off for {}s", + backoff_secs + ); + tokio::time::sleep(tokio::time::Duration::from_secs(backoff_secs)).await; + }}; +} + #[macro_export] macro_rules! with_exponential_backoff { ($ctx:expr, $operation:expr) => {{ let mut backoff_secs = 1; loop { match $operation { - Err(kxio::net::Error::Reqwest(e)) - if e.status() == Some(kxio::net::StatusCode::TOO_MANY_REQUESTS) => + Err(kxio::net::Error::Reqwest(err)) + if err.status() == Some(kxio::net::StatusCode::TOO_MANY_REQUESTS) => { - backoff_secs *= 2; - let jitter = rand::random::() % 10; - let backoff_secs = 60.min(backoff_secs + jitter); - $crate::p!( - $ctx.prt, - ">> Too many requests, backing off for {}s", - backoff_secs - ); - tokio::time::sleep(tokio::time::Duration::from_secs(backoff_secs)).await; + $crate::backoff!(backoff_secs, $ctx) + } + Err(kxio::net::Error::ResponseError { response }) + if response.status() == kxio::net::StatusCode::TOO_MANY_REQUESTS => + { + $crate::backoff!(backoff_secs, $ctx) } result => break result, }