Skip to content

Commit

Permalink
Add retry delays
Browse files Browse the repository at this point in the history
Factors in two possible ways Github can indicate a retry delay. But then
falls back to an exponentially increasing value. But in both cases, will
cap the delay.
  • Loading branch information
goodspark committed Apr 28, 2023
1 parent 56a9694 commit 796c2af
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 25 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ serde_path_to_error = "0.1.4"
serde_urlencoded = "0.7.1"
snafu = { version = "0.7", features = ["backtraces"] }
tokio = { version = "1.17.0", default-features = false, optional = true }
tower = { version = "0.4.13", default-features = false, features = ["util", "buffer"] }
# This has on-master-but-unreleased improvements to the retry code, merged shortly after 0.4.13.
# Can be changed to a version again once https://github.com/tower-rs/tower/pull/681 lands.
tower = { git = "https://github.com/tower-rs/tower", rev = "aec7b8f417b101d57f85c7ede05275dd61a48597", default-features = false, features = ["util", "buffer"] }
tower-http = { version = "0.4.0", features = ["map-response-body", "trace"] }
tracing = { version = "0.1.37", features = ["log"], optional = true }
url = { version = "2.2.2", features = ["serde"] }
Expand Down
42 changes: 41 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub use self::{
pub type Result<T, E = error::Error> = std::result::Result<T, E>;

const GITHUB_BASE_URI: &str = "https://api.github.com";
const MAX_RETRIES: usize = 3;

static STATIC_INSTANCE: Lazy<arc_swap::ArcSwap<Octocrab>> =
Lazy::new(|| arc_swap::ArcSwap::from_pointee(Octocrab::default()));
Expand Down Expand Up @@ -695,7 +696,7 @@ impl Default for DefaultOctocrabBuilderConfig {
write_timeout: None,
base_uri: None,
#[cfg(feature = "retry")]
retry_config: RetryConfig::Simple(3),
retry_config: RetryConfig::Simple(MAX_RETRIES),
}
}
}
Expand Down Expand Up @@ -1463,4 +1464,43 @@ mod tests {
.await
.unwrap();
}

#[tokio::test]
async fn default_retries_on_unauth_only() {
use http::StatusCode;
use wiremock::{matchers, Mock, MockServer, ResponseTemplate};
let mock_server = MockServer::start().await;
Mock::given(matchers::method("GET"))
.and(matchers::path_regex(".*"))
.respond_with(ResponseTemplate::new(StatusCode::INTERNAL_SERVER_ERROR))
.expect(1)
.mount(&mock_server)
.await;
let result = crate::OctocrabBuilder::default()
.base_uri(mock_server.uri())
.unwrap()
.build()
.unwrap()
.orgs("hello")
.get()
.await;
assert_eq!(result.is_err(), true);

let mock_server = MockServer::start().await;
Mock::given(matchers::method("GET"))
.and(matchers::path_regex(".*"))
.respond_with(ResponseTemplate::new(StatusCode::UNAUTHORIZED))
.expect(3)
.mount(&mock_server)
.await;
let result = crate::OctocrabBuilder::default()
.base_uri(mock_server.uri())
.unwrap()
.build()
.unwrap()
.orgs("hello")
.get()
.await;
assert_eq!(result.is_err(), true);
}
}
Loading

0 comments on commit 796c2af

Please sign in to comment.