Unverified Commit 500aef39 authored by ysaito1001's avatar ysaito1001 Committed by GitHub
Browse files

Add SharedAsyncSleep wrapping Arc<dyn AsyncSleep> (#2742)



## Motivation and Context
Add `SharedAsyncSleep` wrapping `Arc<dyn AsyncSleep>` and update call
sites of `Arc<dyn AsyncSleep>` to use `SharedAsyncSleep`

## Description
Public APIs that take/return a trait object for `AsyncSleep` have been
using `Arc<dyn AsyncSleep>` in the codebase. This has a downside of
exposing a bare `Arc`, making it difficult to change the APIs in the
future without breaking backwards compatibility. This PR adds a newtype
wrapper `SharedAsyncSleep` that hides `Arc<dyn AsyncSleep>` to alleviate
the said issue (the idiom of having `SharedXXX` has been seen in the
codebase, e.g. `SharedTimeSource`, `SharedCredentialsProvider`, and so
on).

## Testing
- [x] Passed tests in CI

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: default avatarYuki Saito <awsaito@amazon.com>
Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>
parent 7ea3ed92
Loading
Loading
Loading
Loading
+12 −0
Original line number Original line Diff line number Diff line
@@ -121,3 +121,15 @@ message = "The SDK has added support for timestreamwrite and timestreamquery. Su
meta = { "breaking" = false, "tada" = true, "bug" = false }
meta = { "breaking" = false, "tada" = true, "bug" = false }
references = ["smithy-rs#2707", "aws-sdk-rust#114"]
references = ["smithy-rs#2707", "aws-sdk-rust#114"]
author = "rcoh"
author = "rcoh"

[[smithy-rs]]
message = "A newtype wrapper `SharedAsyncSleep` has been introduced and occurrences of `Arc<dyn AsyncSleep>` that appear in public APIs have been replaced with it."
references = ["smithy-rs#2742"]
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client" }
author = "ysaito1001"

[[aws-sdk-rust]]
message = "A newtype wrapper `SharedAsyncSleep` has been introduced and occurrences of `Arc<dyn AsyncSleep>` that appear in public APIs have been replaced with it."
references = ["smithy-rs#2742"]
meta = { "breaking" = true, "tada" = false, "bug" = false }
author = "ysaito1001"
+2 −2
Original line number Original line Diff line number Diff line
@@ -19,11 +19,11 @@ pub use aws_smithy_client::conns::default_connector;
#[cfg(all(feature = "native-tls", not(feature = "allow-compilation")))]
#[cfg(all(feature = "native-tls", not(feature = "allow-compilation")))]
compile_error!("Feature native-tls has been removed. For upgrade instructions, see: https://awslabs.github.io/smithy-rs/design/transport/connector.html");
compile_error!("Feature native-tls has been removed. For upgrade instructions, see: https://awslabs.github.io/smithy-rs/design/transport/connector.html");


/// Given `ConnectorSettings` and an `AsyncSleep`, create a `DynConnector` from defaults depending on what cargo features are activated.
/// Given `ConnectorSettings` and a [`SharedAsyncSleep`](aws_smithy_async::rt::sleep::SharedAsyncSleep), create a `DynConnector` from defaults depending on what cargo features are activated.
#[cfg(not(feature = "client-hyper"))]
#[cfg(not(feature = "client-hyper"))]
pub fn default_connector(
pub fn default_connector(
    _settings: &aws_smithy_client::http_connector::ConnectorSettings,
    _settings: &aws_smithy_client::http_connector::ConnectorSettings,
    _sleep: Option<std::sync::Arc<dyn aws_smithy_async::rt::sleep::AsyncSleep>>,
    _sleep: Option<aws_smithy_async::rt::sleep::SharedAsyncSleep>,
) -> Option<DynConnector> {
) -> Option<DynConnector> {
    None
    None
}
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -18,7 +18,7 @@ use crate::imds::client::error::{ImdsError, TokenError, TokenErrorKind};
use crate::imds::client::ImdsResponseRetryClassifier;
use crate::imds::client::ImdsResponseRetryClassifier;
use aws_credential_types::cache::ExpiringCache;
use aws_credential_types::cache::ExpiringCache;
use aws_http::user_agent::UserAgentStage;
use aws_http::user_agent::UserAgentStage;
use aws_smithy_async::rt::sleep::AsyncSleep;
use aws_smithy_async::rt::sleep::SharedAsyncSleep;
use aws_smithy_async::time::SharedTimeSource;
use aws_smithy_async::time::SharedTimeSource;
use aws_smithy_client::erase::DynConnector;
use aws_smithy_client::erase::DynConnector;
use aws_smithy_client::retry;
use aws_smithy_client::retry;
@@ -84,7 +84,7 @@ impl TokenMiddleware {
        token_ttl: Duration,
        token_ttl: Duration,
        retry_config: retry::Config,
        retry_config: retry::Config,
        timeout_config: TimeoutConfig,
        timeout_config: TimeoutConfig,
        sleep_impl: Option<Arc<dyn AsyncSleep>>,
        sleep_impl: Option<SharedAsyncSleep>,
    ) -> Self {
    ) -> Self {
        let mut inner_builder = aws_smithy_client::Client::builder()
        let mut inner_builder = aws_smithy_client::Client::builder()
            .connector(connector)
            .connector(connector)
+3 −3
Original line number Original line Diff line number Diff line
@@ -154,7 +154,7 @@ mod loader {


    use aws_credential_types::cache::CredentialsCache;
    use aws_credential_types::cache::CredentialsCache;
    use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
    use aws_credential_types::provider::{ProvideCredentials, SharedCredentialsProvider};
    use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep};
    use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep, SharedAsyncSleep};
    use aws_smithy_async::time::{SharedTimeSource, TimeSource};
    use aws_smithy_async::time::{SharedTimeSource, TimeSource};
    use aws_smithy_client::http_connector::HttpConnector;
    use aws_smithy_client::http_connector::HttpConnector;
    use aws_smithy_types::retry::RetryConfig;
    use aws_smithy_types::retry::RetryConfig;
@@ -185,7 +185,7 @@ mod loader {
        endpoint_url: Option<String>,
        endpoint_url: Option<String>,
        region: Option<Box<dyn ProvideRegion>>,
        region: Option<Box<dyn ProvideRegion>>,
        retry_config: Option<RetryConfig>,
        retry_config: Option<RetryConfig>,
        sleep: Option<Arc<dyn AsyncSleep>>,
        sleep: Option<SharedAsyncSleep>,
        timeout_config: Option<TimeoutConfig>,
        timeout_config: Option<TimeoutConfig>,
        provider_config: Option<ProviderConfig>,
        provider_config: Option<ProviderConfig>,
        http_connector: Option<HttpConnector>,
        http_connector: Option<HttpConnector>,
@@ -260,7 +260,7 @@ mod loader {
        /// is used to create timeout futures.
        /// is used to create timeout futures.
        pub fn sleep_impl(mut self, sleep: impl AsyncSleep + 'static) -> Self {
        pub fn sleep_impl(mut self, sleep: impl AsyncSleep + 'static) -> Self {
            // it's possible that we could wrapping an `Arc in an `Arc` and that's OK
            // it's possible that we could wrapping an `Arc in an `Arc` and that's OK
            self.sleep = Some(Arc::new(sleep));
            self.sleep = Some(SharedAsyncSleep::new(sleep));
            self
            self
        }
        }


+6 −7
Original line number Original line Diff line number Diff line
@@ -6,7 +6,7 @@
//! Configuration Options for Credential Providers
//! Configuration Options for Credential Providers


use aws_credential_types::time_source::TimeSource;
use aws_credential_types::time_source::TimeSource;
use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep};
use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep, SharedAsyncSleep};
use aws_smithy_async::time::SharedTimeSource;
use aws_smithy_async::time::SharedTimeSource;
use aws_smithy_client::erase::DynConnector;
use aws_smithy_client::erase::DynConnector;
use aws_smithy_types::error::display::DisplayErrorContext;
use aws_smithy_types::error::display::DisplayErrorContext;
@@ -41,7 +41,7 @@ pub struct ProviderConfig {
    fs: Fs,
    fs: Fs,
    time_source: SharedTimeSource,
    time_source: SharedTimeSource,
    connector: HttpConnector,
    connector: HttpConnector,
    sleep: Option<Arc<dyn AsyncSleep>>,
    sleep: Option<SharedAsyncSleep>,
    region: Option<Region>,
    region: Option<Region>,
    /// An AWS profile created from `ProfileFiles` and a `profile_name`
    /// An AWS profile created from `ProfileFiles` and a `profile_name`
    parsed_profile: Arc<OnceCell<Result<ProfileSet, ProfileFileLoadError>>>,
    parsed_profile: Arc<OnceCell<Result<ProfileSet, ProfileFileLoadError>>>,
@@ -65,7 +65,7 @@ impl Debug for ProviderConfig {
impl Default for ProviderConfig {
impl Default for ProviderConfig {
    fn default() -> Self {
    fn default() -> Self {
        let connector = HttpConnector::ConnectorFn(Arc::new(
        let connector = HttpConnector::ConnectorFn(Arc::new(
            |settings: &ConnectorSettings, sleep: Option<Arc<dyn AsyncSleep>>| {
            |settings: &ConnectorSettings, sleep: Option<SharedAsyncSleep>| {
                default_connector(settings, sleep)
                default_connector(settings, sleep)
            },
            },
        ));
        ));
@@ -195,7 +195,7 @@ impl ProviderConfig {
    }
    }


    #[allow(dead_code)]
    #[allow(dead_code)]
    pub(crate) fn sleep(&self) -> Option<Arc<dyn AsyncSleep>> {
    pub(crate) fn sleep(&self) -> Option<SharedAsyncSleep> {
        self.sleep.clone()
        self.sleep.clone()
    }
    }


@@ -332,8 +332,7 @@ impl ProviderConfig {
        C::Future: Unpin + Send + 'static,
        C::Future: Unpin + Send + 'static,
        C::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
        C::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    {
    {
        let connector_fn = move |settings: &ConnectorSettings,
        let connector_fn = move |settings: &ConnectorSettings, sleep: Option<SharedAsyncSleep>| {
                                 sleep: Option<Arc<dyn AsyncSleep>>| {
            let mut builder = aws_smithy_client::hyper_ext::Adapter::builder()
            let mut builder = aws_smithy_client::hyper_ext::Adapter::builder()
                .connector_settings(settings.clone());
                .connector_settings(settings.clone());
            if let Some(sleep) = sleep {
            if let Some(sleep) = sleep {
@@ -350,7 +349,7 @@ impl ProviderConfig {
    /// Override the sleep implementation for this configuration
    /// Override the sleep implementation for this configuration
    pub fn with_sleep(self, sleep: impl AsyncSleep + 'static) -> Self {
    pub fn with_sleep(self, sleep: impl AsyncSleep + 'static) -> Self {
        ProviderConfig {
        ProviderConfig {
            sleep: Some(Arc::new(sleep)),
            sleep: Some(SharedAsyncSleep::new(sleep)),
            ..self
            ..self
        }
        }
    }
    }
Loading