Waiters
Waiters are a client-side abstraction used to poll a resource until a desired state is reached, or until it is determined that the resource will not enter the desired state. This is a common task when working with services that are eventually consistent, like Amazon Simple Storage Service, or services that asynchronously create resources, like Amazon Elastic Compute Cloud. Writing logic to continuously poll the status of a resource can be cumbersome and error-prone. The goal of waiters is to move this responsibility out of customer code and into the AWS SDK for Rust, which has in-depth knowledge of the timing aspects for the AWS operation.
AWS services that provide support for waiters include a
module. <service>
::waiters
-
The
trait provides waiter methods for the client. The methods are implemented for the<service>
::client::WaitersClient
struct. All waiter methods follow a standard naming convention ofwait_until_
<Condition>
-
For Amazon S3, this trait is
aws_sdk_s3::client::Waiters
.
-
The following example uses Amazon S3. However, the concepts are the same for any AWS service that has one or more waiters defined.
The following code example shows using a waiter function instead of writing polling logic to wait for a bucket to exist after being created.
use std::time::Duration; use aws_config::BehaviorVersion; // Import Waiters trait to get `wait_until_<Condition>` methods on Client. use aws_sdk_s3::client::Waiters; let config = aws_config::defaults(BehaviorVersion::latest()) .load() .await; let s3 = aws_sdk_s3::Client::new(&config); // This initiates creating an S3 bucket and potentially returns before the bucket exists. s3.create_bucket() .bucket("my-bucket") .send() .await?; // When this function returns, the bucket either exists or an error is propagated. s3.wait_until_bucket_exists() .bucket("my-bucket") .wait(Duration::from_secs(5)) .await?; // The bucket now exists.
Note
Each wait method returns a Result<FinalPoll<...>, WaiterError<...>>
that can be used to get at the
final response from reaching the desired condition or an error. See FinalPoll