Skip to content

Commit

Permalink
Refactoring re-try logic to use progressive interval
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Dec 13, 2024
1 parent 966919e commit b97aa46
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ rustls-pemfile = "2"
serde = { version = "1.0", features = ["derive", "rc"] }
serde_json = "1.0"
thiserror = "2.0"
tokio = { version = "1.1", features = ["fs", "sync"] }
tokio = { version = "1.1", features = ["fs", "sync", "time"] }
tracing = "0.1.29"
tracing-futures = "0.2.5"
url = "2"
Expand Down
8 changes: 7 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use hyper_util::rt::TokioExecutor;
use ring::rand::SystemRandom;
use ring::signature::{RsaKeyPair, RSA_PKCS1_SHA256};
use serde::{Deserialize, Deserializer};
use tokio::time::sleep;
use tracing::{debug, warn};

use crate::Error;
Expand Down Expand Up @@ -50,7 +51,11 @@ impl HttpClient {
request: &impl Fn() -> Request<Full<Bytes>>,
provider: &'static str,
) -> Result<Arc<Token>, Error> {
//We multiply it by two on every iteration to progressively slow down ourself
//At most we will perform 50 + 100 + 200 + 400 wait as we're limited by 4 re-tries
let mut sleep_interval = Duration::from_millis(50);
let mut retries = 0;

let body = loop {
let err = match self.request(request(), provider).await {
// Early return when the request succeeds
Expand All @@ -68,7 +73,8 @@ impl HttpClient {
return Err(err);
}

tokio::time::sleep(Duration::from_millis(200)).await;
sleep(sleep_interval).await;
sleep_interval *= 2;
};

serde_json::from_slice(&body)
Expand Down

0 comments on commit b97aa46

Please sign in to comment.