Skip to content
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 SharedAsyncSleep wrapping Arc<dyn AsyncSleep> #2742

Merged
merged 13 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,15 @@ message = "The SDK has added support for timestreamwrite and timestreamquery. Su
meta = { "breaking" = false, "tada" = true, "bug" = false }
references = ["smithy-rs#2707", "aws-sdk-rust#114"]
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"
4 changes: 2 additions & 2 deletions aws/rust-runtime/aws-config/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ pub use aws_smithy_client::conns::default_connector;
#[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");

/// Given `ConnectorSettings` and an `AsyncSleep`, create a `DynConnector` from defaults depending on what cargo features are activated.
/// Given `ConnectorSettings` and an `SharedAsyncSleep`, create a `DynConnector` from defaults depending on what cargo features are activated.
ysaito1001 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "client-hyper"))]
pub fn default_connector(
_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> {
None
}
4 changes: 2 additions & 2 deletions aws/rust-runtime/aws-config/src/imds/client/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::imds::client::error::{ImdsError, TokenError, TokenErrorKind};
use crate::imds::client::ImdsResponseRetryClassifier;
use aws_credential_types::cache::ExpiringCache;
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_client::erase::DynConnector;
use aws_smithy_client::retry;
Expand Down Expand Up @@ -84,7 +84,7 @@ impl TokenMiddleware {
token_ttl: Duration,
retry_config: retry::Config,
timeout_config: TimeoutConfig,
sleep_impl: Option<Arc<dyn AsyncSleep>>,
sleep_impl: Option<SharedAsyncSleep>,
) -> Self {
let mut inner_builder = aws_smithy_client::Client::builder()
.connector(connector)
Expand Down
6 changes: 3 additions & 3 deletions aws/rust-runtime/aws-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ mod loader {

use aws_credential_types::cache::CredentialsCache;
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_client::http_connector::HttpConnector;
use aws_smithy_types::retry::RetryConfig;
Expand Down Expand Up @@ -185,7 +185,7 @@ mod loader {
endpoint_url: Option<String>,
region: Option<Box<dyn ProvideRegion>>,
retry_config: Option<RetryConfig>,
sleep: Option<Arc<dyn AsyncSleep>>,
sleep: Option<SharedAsyncSleep>,
timeout_config: Option<TimeoutConfig>,
provider_config: Option<ProviderConfig>,
http_connector: Option<HttpConnector>,
Expand Down Expand Up @@ -260,7 +260,7 @@ mod loader {
/// is used to create timeout futures.
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
self.sleep = Some(Arc::new(sleep));
self.sleep = Some(SharedAsyncSleep::new(sleep));
self
}

Expand Down
13 changes: 6 additions & 7 deletions aws/rust-runtime/aws-config/src/provider_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! Configuration Options for Credential Providers

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

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

Expand Down Expand Up @@ -332,8 +332,7 @@ impl ProviderConfig {
C::Future: Unpin + Send + 'static,
C::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
let connector_fn = move |settings: &ConnectorSettings,
sleep: Option<Arc<dyn AsyncSleep>>| {
let connector_fn = move |settings: &ConnectorSettings, sleep: Option<SharedAsyncSleep>| {
let mut builder = aws_smithy_client::hyper_ext::Adapter::builder()
.connector_settings(settings.clone());
if let Some(sleep) = sleep {
Expand All @@ -350,7 +349,7 @@ impl ProviderConfig {
/// Override the sleep implementation for this configuration
pub fn with_sleep(self, sleep: impl AsyncSleep + 'static) -> Self {
ProviderConfig {
sleep: Some(Arc::new(sleep)),
sleep: Some(SharedAsyncSleep::new(sleep)),
..self
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
allowed_external_types = [
"aws_smithy_async::rt::sleep::AsyncSleep",
"aws_smithy_async::rt::sleep::SharedAsyncSleep",
]
28 changes: 13 additions & 15 deletions aws/rust-runtime/aws-credential-types/src/cache/lazy_caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

//! Lazy, credentials cache implementation

use std::sync::Arc;
use std::time::{Duration, Instant};

use aws_smithy_async::future::timeout::Timeout;
use aws_smithy_async::rt::sleep::AsyncSleep;
use aws_smithy_async::rt::sleep::{AsyncSleep, SharedAsyncSleep};
use tracing::{debug, info, info_span, Instrument};

use crate::cache::{ExpiringCache, ProvideCachedCredentials};
Expand All @@ -25,7 +24,7 @@ const DEFAULT_BUFFER_TIME_JITTER_FRACTION: fn() -> f64 = fastrand::f64;
#[derive(Debug)]
pub(crate) struct LazyCredentialsCache {
time: TimeSource,
sleeper: Arc<dyn AsyncSleep>,
sleeper: SharedAsyncSleep,
cache: ExpiringCache<Credentials, CredentialsError>,
provider: SharedCredentialsProvider,
load_timeout: Duration,
Expand All @@ -37,7 +36,7 @@ pub(crate) struct LazyCredentialsCache {
impl LazyCredentialsCache {
fn new(
time: TimeSource,
sleeper: Arc<dyn AsyncSleep>,
sleeper: SharedAsyncSleep,
provider: SharedCredentialsProvider,
load_timeout: Duration,
buffer_time: Duration,
Expand Down Expand Up @@ -133,12 +132,11 @@ use crate::Credentials;
pub use builder::Builder;

mod builder {
use std::sync::Arc;
use std::time::Duration;

use crate::cache::{CredentialsCache, Inner};
use crate::provider::SharedCredentialsProvider;
use aws_smithy_async::rt::sleep::{default_async_sleep, AsyncSleep};
use aws_smithy_async::rt::sleep::{default_async_sleep, SharedAsyncSleep};

use super::TimeSource;
use super::{
Expand All @@ -160,7 +158,7 @@ mod builder {
/// `build` to create a `LazyCredentialsCache`.
#[derive(Clone, Debug, Default)]
pub struct Builder {
sleep: Option<Arc<dyn AsyncSleep>>,
sleep: Option<SharedAsyncSleep>,
time_source: Option<TimeSource>,
load_timeout: Option<Duration>,
buffer_time: Option<Duration>,
Expand All @@ -174,22 +172,22 @@ mod builder {
Default::default()
}

/// Implementation of [`AsyncSleep`] to use for timeouts.
/// Implementation of [`AsyncSleep`](aws_smithy_async::rt::sleep::AsyncSleep) to use for timeouts.
///
/// This enables use of the `LazyCredentialsCache` with other async runtimes.
/// If using Tokio as the async runtime, this should be set to an instance of
/// [`TokioSleep`](aws_smithy_async::rt::sleep::TokioSleep).
pub fn sleep(mut self, sleep: Arc<dyn AsyncSleep>) -> Self {
pub fn sleep(mut self, sleep: SharedAsyncSleep) -> Self {
self.set_sleep(Some(sleep));
self
}

/// Implementation of [`AsyncSleep`] to use for timeouts.
/// Implementation of [`AsyncSleep`](aws_smithy_async::rt::sleep::AsyncSleep) to use for timeouts.
///
/// This enables use of the `LazyCredentialsCache` with other async runtimes.
/// If using Tokio as the async runtime, this should be set to an instance of
/// [`TokioSleep`](aws_smithy_async::rt::sleep::TokioSleep).
pub fn set_sleep(&mut self, sleep: Option<Arc<dyn AsyncSleep>>) -> &mut Self {
pub fn set_sleep(&mut self, sleep: Option<SharedAsyncSleep>) -> &mut Self {
self.sleep = sleep;
self
}
Expand Down Expand Up @@ -347,7 +345,7 @@ mod tests {
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use aws_smithy_async::rt::sleep::TokioSleep;
use aws_smithy_async::rt::sleep::{SharedAsyncSleep, TokioSleep};
use tracing::info;
use tracing_test::traced_test;

Expand All @@ -372,7 +370,7 @@ mod tests {
let load_list = Arc::new(Mutex::new(load_list));
LazyCredentialsCache::new(
time,
Arc::new(TokioSleep::new()),
SharedAsyncSleep::new(TokioSleep::new()),
SharedCredentialsProvider::new(provide_credentials_fn(move || {
let list = load_list.clone();
async move {
Expand Down Expand Up @@ -414,7 +412,7 @@ mod tests {
}));
let credentials_cache = LazyCredentialsCache::new(
TimeSource::testing(&time),
Arc::new(TokioSleep::new()),
SharedAsyncSleep::new(TokioSleep::new()),
provider,
DEFAULT_LOAD_TIMEOUT,
DEFAULT_BUFFER_TIME,
Expand Down Expand Up @@ -534,7 +532,7 @@ mod tests {
let time = TestingTimeSource::new(epoch_secs(100));
let credentials_cache = LazyCredentialsCache::new(
TimeSource::testing(&time),
Arc::new(TokioSleep::new()),
SharedAsyncSleep::new(TokioSleep::new()),
SharedCredentialsProvider::new(provide_credentials_fn(|| async {
aws_smithy_async::future::never::Never::new().await;
Ok(credentials(1000))
Expand Down
28 changes: 14 additions & 14 deletions aws/rust-runtime/aws-inlineable/src/endpoint_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

//! Maintain a cache of discovered endpoints

use aws_smithy_async::rt::sleep::AsyncSleep;
use aws_smithy_async::time::TimeSource;
use aws_smithy_async::rt::sleep::{AsyncSleep, SharedAsyncSleep};
use aws_smithy_async::time::SharedTimeSource;
use aws_smithy_client::erase::boxclone::BoxFuture;
use aws_smithy_http::endpoint::{ResolveEndpoint, ResolveEndpointError};
use aws_smithy_types::endpoint::Endpoint;
Expand All @@ -24,8 +24,8 @@ pub struct ReloadEndpoint {
endpoint: Arc<Mutex<Option<ExpiringEndpoint>>>,
error: Arc<Mutex<Option<ResolveEndpointError>>>,
rx: Receiver<()>,
sleep: Arc<dyn AsyncSleep>,
time: Arc<dyn TimeSource>,
sleep: SharedAsyncSleep,
time: SharedTimeSource,
}

impl Debug for ReloadEndpoint {
Expand Down Expand Up @@ -106,8 +106,8 @@ impl ExpiringEndpoint {

pub(crate) async fn create_cache<F>(
loader_fn: impl Fn() -> F + Send + Sync + 'static,
sleep: Arc<dyn AsyncSleep>,
time: Arc<dyn TimeSource>,
sleep: SharedAsyncSleep,
time: SharedTimeSource,
) -> Result<(EndpointCache, ReloadEndpoint), ResolveEndpointError>
where
F: Future<Output = Result<(Endpoint, SystemTime), ResolveEndpointError>> + Send + 'static,
Expand Down Expand Up @@ -155,9 +155,9 @@ impl EndpointCache {
#[cfg(test)]
mod test {
use crate::endpoint_discovery::create_cache;
use aws_smithy_async::rt::sleep::TokioSleep;
use aws_smithy_async::rt::sleep::{SharedAsyncSleep, TokioSleep};
use aws_smithy_async::test_util::controlled_time_and_sleep;
use aws_smithy_async::time::SystemTimeSource;
use aws_smithy_async::time::{SharedTimeSource, SystemTimeSource};
use aws_smithy_types::endpoint::Endpoint;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
Expand All @@ -178,8 +178,8 @@ mod test {
SystemTime::now(),
))
},
Arc::new(TokioSleep::new()),
Arc::new(SystemTimeSource::new()),
SharedAsyncSleep::new(TokioSleep::new()),
SharedTimeSource::new(SystemTimeSource::new()),
)
.await
.unwrap();
Expand All @@ -204,8 +204,8 @@ mod test {
))
}
},
Arc::new(TokioSleep::new()),
Arc::new(SystemTimeSource::new()),
SharedAsyncSleep::new(TokioSleep::new()),
SharedTimeSource::new(SystemTimeSource::new()),
)
.await
.expect("returns an endpoint");
Expand Down Expand Up @@ -248,8 +248,8 @@ mod test {
))
}
},
Arc::new(sleep.clone()),
Arc::new(time.clone()),
SharedAsyncSleep::new(sleep.clone()),
SharedTimeSource::new(time.clone()),
)
.await
.expect("first load success");
Expand Down
2 changes: 1 addition & 1 deletion aws/rust-runtime/aws-types/external-types.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
allowed_external_types = [
"aws_credential_types::cache::CredentialsCache",
"aws_credential_types::provider::SharedCredentialsProvider",
"aws_smithy_async::rt::sleep::AsyncSleep",
"aws_smithy_async::rt::sleep::SharedAsyncSleep",
"aws_smithy_async::time::TimeSource",
"aws_smithy_async::time::SharedTimeSource",
"aws_smithy_client::http_connector",
Expand Down
Loading