-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for retrying 429 (too many requests) responses #45
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,13 @@ | |
use std::time::Duration; | ||
|
||
use once_cell::sync::Lazy; | ||
use reqwest::Url; | ||
use reqwest::{Response, Url}; | ||
use reqwest_retry::policies::ExponentialBackoff; | ||
use reqwest_retry::{ | ||
default_on_request_failure, RetryTransientMiddleware, Retryable, RetryableStrategy, | ||
}; | ||
|
||
use crate::Client; | ||
use crate::client::Client; | ||
|
||
pub static DEFAULT_ENDPOINT: Lazy<Url> = Lazy::new(|| { | ||
"https://api.billwithorb.com/v1" | ||
|
@@ -35,27 +39,69 @@ pub struct ClientConfig { | |
/// A builder for a [`Client`]. | ||
pub struct ClientBuilder { | ||
endpoint: Url, | ||
retry_policy: Option<ExponentialBackoff>, | ||
} | ||
|
||
impl Default for ClientBuilder { | ||
fn default() -> ClientBuilder { | ||
ClientBuilder { | ||
endpoint: DEFAULT_ENDPOINT.clone(), | ||
retry_policy: Some( | ||
ExponentialBackoff::builder() | ||
.retry_bounds(Duration::from_secs(1), Duration::from_secs(5)) | ||
.build_with_max_retries(5), | ||
), | ||
} | ||
} | ||
} | ||
|
||
/// Retry requests with a successful response of 429 (too many requests). | ||
struct Retry429; | ||
impl RetryableStrategy for Retry429 { | ||
fn handle(&self, res: &Result<Response, reqwest_middleware::Error>) -> Option<Retryable> { | ||
match res { | ||
// Retry if response status is 429 | ||
Ok(success) if success.status() == 429 => Some(Retryable::Transient), | ||
// Otherwise do not retry a successful request | ||
Ok(_) => None, | ||
// Retry failures due to network errors | ||
Err(error) => default_on_request_failure(error), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. based on the default strategy, I'm wondering if there are same Retryable::Fatal cases not being caught? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn't read the default strategy well enough. I've reverted to using the default strategy instead of specifying a new one. I see the default strategy handles the 429 status code and some other cases. |
||
} | ||
} | ||
} | ||
|
||
impl ClientBuilder { | ||
/// Sets the policy for retrying failed API calls. | ||
/// | ||
/// Note that the created [`Client`] will retry all API calls that return a 429 status code. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're using the default policy I don't believe this is entirely accurate, it'll also retry server timeouts |
||
pub fn with_retry_policy(mut self, policy: ExponentialBackoff) -> Self { | ||
self.retry_policy = Some(policy); | ||
self | ||
} | ||
|
||
/// Sets the endpoint. | ||
pub fn with_endpoint(mut self, endpoint: Url) -> Self { | ||
self.endpoint = endpoint; | ||
self | ||
} | ||
|
||
/// Creates a [`Client`] that incorporates the optional parameters | ||
/// configured on the builder and the specified required parameters. | ||
pub fn build(self, config: ClientConfig) -> Client { | ||
let inner = reqwest::ClientBuilder::new() | ||
let client = reqwest::ClientBuilder::new() | ||
.redirect(reqwest::redirect::Policy::none()) | ||
.timeout(Duration::from_secs(60)) | ||
.build() | ||
.unwrap(); | ||
Client { | ||
inner, | ||
client_retryable: match self.retry_policy { | ||
Some(policy) => reqwest_middleware::ClientBuilder::new(client.clone()) | ||
.with(RetryTransientMiddleware::new_with_policy_and_strategy( | ||
policy, Retry429, | ||
)) | ||
.build(), | ||
None => reqwest_middleware::ClientBuilder::new(client.clone()).build(), | ||
}, | ||
api_key: config.api_key, | ||
endpoint: self.endpoint, | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could keep this named inner? The reason this was changed in https://github.com/MaterializeInc/rust-frontegg/pull/13/files was because there was no true inner client, we selectively chose one of two clients depending on the http request method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK that makes sense. I've reverted back to using
inner
.