Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/global-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ protobuf-src = ["types/protobuf-src"]
chirp = ["types"]

[dependencies]
async-trait = "0.1"
formatted-error = { path = "../formatted-error" }
types = { path = "../types/core", optional = true }
http = "0.2"
reqwest = "0.11"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
23 changes: 22 additions & 1 deletion lib/global-error/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::Location;
use crate::{bail, GlobalResult, Location};
use async_trait::async_trait;

#[derive(Debug, thiserror::Error)]
pub enum AssertionError {
Expand Down Expand Up @@ -122,3 +123,23 @@ impl<'a, T> UnwrapOrAssertError for &'a &'a Option<T> {
}
}
}

#[async_trait]
pub trait ToGlobalError: Sized {
async fn to_global_error(self) -> GlobalResult<Self>;
}

#[async_trait]
impl ToGlobalError for reqwest::Response {
async fn to_global_error(self) -> GlobalResult<Self> {
if self.status().is_success() {
Ok(self)
} else {
let url = self.url().clone();
let status = self.status();
let body = self.text().await?;

bail!(format!("{url} ({status}):\n{body}"));
}
}
}
2 changes: 2 additions & 0 deletions lib/util/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ macros = []
serde = []

[dependencies]
async-trait = "0.1"
bcrypt = "0.13.0"
chrono = "0.4"
formatted-error = { path = "../../formatted-error", optional = true }
Expand All @@ -20,6 +21,7 @@ ipnet = { version = "2.7", features = ["serde"] }
lazy_static = "1.4"
rand = "0.8"
regex = "1.4"
reqwest = "0.11"
rivet-util-env = { path = "../env" }
rivet-util-macros = { path = "../macros" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions lib/util/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod geo;
pub mod glob;
pub mod math;
pub mod net;
pub mod req;
pub mod route;
pub mod sort;
pub mod timestamp;
Expand Down
41 changes: 41 additions & 0 deletions lib/util/core/src/req.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::time::Duration;

use async_trait::async_trait;
use global_error::prelude::*;

#[async_trait]
pub trait SendRetry {
/// Retries the request upon receiving a 429 response.
async fn send_retry(self, mut retries: usize) -> GlobalResult<reqwest::Response>;
}

#[async_trait]
impl SendRetry for reqwest::RequestBuilder {
async fn send_retry(self, mut retries: usize) -> GlobalResult<reqwest::Response> {
loop {
let req = unwrap!(self.try_clone());
let res = req.send().await?;

if let reqwest::StatusCode::TOO_MANY_REQUESTS = res.status() {
if retries != 0 {
retries -= 1;

// TODO: Parse all valid Retry-After formats. Currently only parses duration
let retry_time = res
.headers()
.get("Retry-After")
.map(|x| x.to_str())
.transpose()?
.map(|x| x.parse::<u64>())
.transpose()?
.unwrap_or(5);
tokio::time::sleep(Duration::from_secs(retry_time)).await;

continue;
}
}

break Ok(res);
}
}
}
Loading