aws_smithy_runtime_api/client/
retries.rspub mod classifiers;
use crate::box_error::BoxError;
use crate::client::interceptors::context::InterceptorContext;
use crate::client::runtime_components::sealed::ValidateConfig;
use crate::client::runtime_components::RuntimeComponents;
use aws_smithy_types::config_bag::{ConfigBag, Storable, StoreReplace};
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use crate::impl_shared_conversions;
pub use aws_smithy_types::retry::ErrorKind;
#[cfg(feature = "test-util")]
pub use test_util::AlwaysRetry;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShouldAttempt {
Yes,
No,
YesAfterDelay(Duration),
}
#[cfg(feature = "test-util")]
impl ShouldAttempt {
pub fn expect_delay(self) -> Duration {
match self {
ShouldAttempt::YesAfterDelay(delay) => delay,
_ => panic!("Expected this to be the `YesAfterDelay` variant but it was the `{self:?}` variant instead"),
}
}
pub fn expect_no(self) {
if ShouldAttempt::No == self {
return;
}
panic!("Expected this to be the `No` variant but it was the `{self:?}` variant instead");
}
}
impl_shared_conversions!(convert SharedRetryStrategy from RetryStrategy using SharedRetryStrategy::new);
pub trait RetryStrategy: Send + Sync + fmt::Debug {
fn should_attempt_initial_request(
&self,
runtime_components: &RuntimeComponents,
cfg: &ConfigBag,
) -> Result<ShouldAttempt, BoxError>;
fn should_attempt_retry(
&self,
context: &InterceptorContext,
runtime_components: &RuntimeComponents,
cfg: &ConfigBag,
) -> Result<ShouldAttempt, BoxError>;
}
#[derive(Clone, Debug)]
pub struct SharedRetryStrategy(Arc<dyn RetryStrategy>);
impl SharedRetryStrategy {
pub fn new(retry_strategy: impl RetryStrategy + 'static) -> Self {
Self(Arc::new(retry_strategy))
}
}
impl RetryStrategy for SharedRetryStrategy {
fn should_attempt_initial_request(
&self,
runtime_components: &RuntimeComponents,
cfg: &ConfigBag,
) -> Result<ShouldAttempt, BoxError> {
self.0
.should_attempt_initial_request(runtime_components, cfg)
}
fn should_attempt_retry(
&self,
context: &InterceptorContext,
runtime_components: &RuntimeComponents,
cfg: &ConfigBag,
) -> Result<ShouldAttempt, BoxError> {
self.0
.should_attempt_retry(context, runtime_components, cfg)
}
}
impl ValidateConfig for SharedRetryStrategy {}
#[derive(Debug, Clone, Copy)]
pub struct RequestAttempts {
attempts: u32,
}
impl RequestAttempts {
pub fn new(attempts: u32) -> Self {
Self { attempts }
}
pub fn attempts(&self) -> u32 {
self.attempts
}
}
impl From<u32> for RequestAttempts {
fn from(attempts: u32) -> Self {
Self::new(attempts)
}
}
impl From<RequestAttempts> for u32 {
fn from(value: RequestAttempts) -> Self {
value.attempts()
}
}
impl Storable for RequestAttempts {
type Storer = StoreReplace<Self>;
}
#[cfg(feature = "test-util")]
mod test_util {
use super::ErrorKind;
use crate::client::interceptors::context::InterceptorContext;
use crate::client::retries::classifiers::{ClassifyRetry, RetryAction};
#[derive(Debug)]
pub struct AlwaysRetry(pub ErrorKind);
impl ClassifyRetry for AlwaysRetry {
fn classify_retry(&self, error: &InterceptorContext) -> RetryAction {
tracing::debug!("Retrying error {:?} as an {:?}", error, self.0);
RetryAction::retryable_error(self.0)
}
fn name(&self) -> &'static str {
"Always Retry"
}
}
}