#[allow(dead_code)]
mod adaptive;
use std::fmt;
use aws_sdk_s3::error::SdkError;
use aws_smithy_runtime_api::http::Response;
use aws_smithy_types::error::metadata::ProvideErrorMetadata;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ErrorKind {
Throttle,
Transient,
Permanent,
}
#[derive(Debug, Clone)]
pub(crate) struct CompletionSample {
pub error: Option<ErrorKind>,
}
pub(crate) trait ConcurrencyController: Send + Sync + fmt::Debug {
fn target(&self) -> usize;
fn on_dispatch(&self) {}
fn on_completion(&self, _sample: Option<&CompletionSample>) {}
}
#[derive(Debug)]
pub(crate) struct FixedConcurrency(usize);
impl FixedConcurrency {
pub(crate) fn new(target: usize) -> Self {
assert!(target > 0, "concurrency target must be > 0");
Self(target)
}
}
impl ConcurrencyController for FixedConcurrency {
fn target(&self) -> usize {
self.0
}
}
const THROTTLING_CODES: &[&str] = &[
"Throttling",
"ThrottlingException",
"SlowDown",
"RequestLimitExceeded",
"BandwidthLimitExceeded",
];
pub(crate) fn classify_sdk_error<E>(err: &SdkError<E, Response>) -> ErrorKind
where
E: ProvideErrorMetadata,
{
match err {
SdkError::ServiceError(ctx) => {
if let Some(code) = ctx.err().code() {
if THROTTLING_CODES.contains(&code) {
return ErrorKind::Throttle;
}
}
match ctx.raw().status().as_u16() {
429 | 503 => ErrorKind::Throttle,
500 | 502 | 504 => ErrorKind::Transient,
_ => ErrorKind::Permanent,
}
}
SdkError::TimeoutError(_) | SdkError::DispatchFailure(_) => ErrorKind::Transient,
_ => ErrorKind::Transient,
}
}
pub(crate) fn classify_error(err: &crate::error::Error) -> Option<ErrorKind> {
use aws_sdk_s3::error::SdkError;
use aws_sdk_s3::operation::{
complete_multipart_upload::CompleteMultipartUploadError,
create_multipart_upload::CreateMultipartUploadError, get_object::GetObjectError,
put_object::PutObjectError, upload_part::UploadPartError,
};
use std::error::Error as _;
let mut source: Option<&(dyn std::error::Error + 'static)> = err.source();
while let Some(e) = source {
if let Some(e) = e.downcast_ref::<SdkError<GetObjectError>>() {
return Some(classify_sdk_error(e));
}
if let Some(e) = e.downcast_ref::<SdkError<CreateMultipartUploadError>>() {
return Some(classify_sdk_error(e));
}
if let Some(e) = e.downcast_ref::<SdkError<UploadPartError>>() {
return Some(classify_sdk_error(e));
}
if let Some(e) = e.downcast_ref::<SdkError<CompleteMultipartUploadError>>() {
return Some(classify_sdk_error(e));
}
if let Some(e) = e.downcast_ref::<SdkError<PutObjectError>>() {
return Some(classify_sdk_error(e));
}
if let Some(e) = e.downcast_ref::<SdkError<aws_sdk_s3::Error>>() {
return Some(classify_sdk_error(e));
}
source = e.source();
}
None
}