fix: improve exponential backup detection of 429 error
This commit is contained in:
parent
5d62b7edd0
commit
8305715471
1 changed files with 24 additions and 11 deletions
|
@ -1,21 +1,34 @@
|
||||||
|
//
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! backoff {
|
||||||
|
($backoff_secs:expr, $ctx:expr) => {{
|
||||||
|
$backoff_secs *= 2;
|
||||||
|
let jitter = rand::random::<u64>() % 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_export]
|
||||||
macro_rules! with_exponential_backoff {
|
macro_rules! with_exponential_backoff {
|
||||||
($ctx:expr, $operation:expr) => {{
|
($ctx:expr, $operation:expr) => {{
|
||||||
let mut backoff_secs = 1;
|
let mut backoff_secs = 1;
|
||||||
loop {
|
loop {
|
||||||
match $operation {
|
match $operation {
|
||||||
Err(kxio::net::Error::Reqwest(e))
|
Err(kxio::net::Error::Reqwest(err))
|
||||||
if e.status() == Some(kxio::net::StatusCode::TOO_MANY_REQUESTS) =>
|
if err.status() == Some(kxio::net::StatusCode::TOO_MANY_REQUESTS) =>
|
||||||
{
|
{
|
||||||
backoff_secs *= 2;
|
$crate::backoff!(backoff_secs, $ctx)
|
||||||
let jitter = rand::random::<u64>() % 10;
|
}
|
||||||
let backoff_secs = 60.min(backoff_secs + jitter);
|
Err(kxio::net::Error::ResponseError { response })
|
||||||
$crate::p!(
|
if response.status() == kxio::net::StatusCode::TOO_MANY_REQUESTS =>
|
||||||
$ctx.prt,
|
{
|
||||||
">> Too many requests, backing off for {}s",
|
$crate::backoff!(backoff_secs, $ctx)
|
||||||
backoff_secs
|
|
||||||
);
|
|
||||||
tokio::time::sleep(tokio::time::Duration::from_secs(backoff_secs)).await;
|
|
||||||
}
|
}
|
||||||
result => break result,
|
result => break result,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue