Skip to content

Commit

Permalink
Added retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Nov 25, 2023
1 parent 7cf3888 commit 7ce6f5c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ serde_json = { version = "1.0", default_features = false }

# perform requests to the internet
reqwest = {version="*", features = ["gzip"]}
reqwest-retry = "*"
reqwest-middleware = "*"

# create random string for cookies
rand = {version="*", default_features = false, features = ["std", "std_rng", "getrandom"]}
Expand Down
13 changes: 8 additions & 5 deletions src/icao_to_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::sync::Arc;
use rand::Rng;
use reqwest::header;
use reqwest::{self, StatusCode};
use reqwest_middleware::ClientBuilder;
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use time::PrimitiveDateTime;

use crate::fs_azure;
Expand Down Expand Up @@ -80,17 +82,18 @@ async fn globe_history(icao: &str, date: &time::Date) -> Result<Vec<u8>, std::io
headers.insert("Sec-Fetch-Site", "same-origin".parse().unwrap());
headers.insert("TE", "trailers".parse().unwrap());

let client = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
// Retry up to 3 times with increasing intervals between attempts.
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let client = ClientBuilder::new(reqwest::Client::new())
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();

let response = client
.get(url)
.headers(headers)
.send()
.await
.map_err(to_io_err)?;
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
if response.status() == StatusCode::OK {
Ok(response.bytes().await.map_err(to_io_err)?.to_vec())
} else if response.status() == StatusCode::NOT_FOUND {
Expand Down

0 comments on commit 7ce6f5c

Please sign in to comment.