Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn default_setting() -> RetrySetting {
factor: 1u64,
take: 20,
codes: vec![Code::Unavailable, Code::Unknown],
jitter: false,
}
}

Expand Down
1 change: 1 addition & 0 deletions bigquery/src/grpc/apiv1/bigquery_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn default_setting() -> RetrySetting {
factor: 1u64,
take: 20,
codes: vec![Code::Unavailable, Code::Unknown],
jitter: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion foundation/gax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ thiserror = "2.0"
tower = { version = "0.5", features = ["filter", "util"] }
http = "1.1"
token-source = "1.0"
tokio-retry2 = "0.6"
tokio-retry2 = { version = "0.6", features = ["jitter"] }
Comment on lines 20 to +23

16 changes: 14 additions & 2 deletions foundation/gax/src/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::iter::Take;
use std::time::Duration;

pub use tokio_retry2::strategy::ExponentialBackoff;
use tokio_retry2::strategy::jitter;
use tokio_retry2::{Action, RetryIf};
pub use tokio_retry2::{Condition, MapErr};

Expand All @@ -22,6 +23,9 @@ pub trait Retry<E: TryAs<Status>, T: Condition<E>> {
fn strategy(&self) -> Take<ExponentialBackoff>;
fn condition(&self) -> T;
fn notify(error: &E, duration: Duration);
fn jitter(&self) -> bool {
false
}
}

pub struct CodeCondition {
Expand Down Expand Up @@ -57,6 +61,7 @@ pub struct RetrySetting {
pub factor: u64,
pub take: usize,
pub codes: Vec<Code>,
pub jitter: bool,
}

impl Retry<Status, CodeCondition> for RetrySetting {
Expand All @@ -75,6 +80,10 @@ impl Retry<Status, CodeCondition> for RetrySetting {
fn notify(_error: &Status, _duration: Duration) {
tracing::trace!("retry fn");
}

fn jitter(&self) -> bool {
self.jitter
}
}

impl Default for RetrySetting {
Expand All @@ -85,6 +94,7 @@ impl Default for RetrySetting {
factor: 1u64,
take: 5,
codes: vec![Code::Unavailable, Code::Unknown, Code::Aborted],
jitter: false,
}
}
}
Expand All @@ -97,7 +107,8 @@ where
RT: Retry<E, C> + Default,
{
let retry = retry.unwrap_or_default();
RetryIf::spawn(retry.strategy(), action, retry.condition(), RT::notify).await
let apply: fn(Duration) -> Duration = if retry.jitter() { jitter } else { |d| d };
RetryIf::spawn(retry.strategy().map(apply), action, retry.condition(), RT::notify).await
}
/// Repeats retries when the specified error is detected.
/// The argument specified by 'v' can be reused for each retry.
Expand All @@ -109,7 +120,8 @@ where
RT: Retry<E, C> + Default,
{
let retry = retry.unwrap_or_default();
let mut strategy = retry.strategy();
let apply: fn(Duration) -> Duration = if retry.jitter() { jitter } else { |d| d };
let mut strategy = retry.strategy().map(apply);
loop {
let result = f(v).await;
let status = match result {
Expand Down
1 change: 1 addition & 0 deletions foundation/longrunning/src/autogen/operations_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn default_retry_setting() -> RetrySetting {
factor: 1u64,
take: 20,
codes: vec![Code::Unavailable, Code::Unknown],
jitter: false,
}
}

Expand Down
1 change: 1 addition & 0 deletions kms/src/grpc/apiv1/kms_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn default_setting() -> RetrySetting {
factor: 1u64,
take: 20,
codes: vec![Code::Unavailable, Code::Unknown],
jitter: false,
}
}

Expand Down
1 change: 1 addition & 0 deletions spanner/src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ pub fn default_retry_setting() -> RetrySetting {
factor: 1u64,
take: 20,
codes: vec![Code::Unavailable, Code::Unknown, Code::DeadlineExceeded],
jitter: false,
}
}
1 change: 1 addition & 0 deletions spanner/src/apiv1/spanner_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn default_setting() -> RetrySetting {
factor: 1u64,
take: 20,
codes: vec![Code::Unavailable, Code::Unknown],
jitter: false,
}
}

Expand Down
Loading