配置轮询政策

Rust 版 Cloud 客户端库提供了辅助函数,用于监控长时间运行的操作 (LRO) 的进度。这些辅助程序使用政策来配置轮询频率,并确定哪些轮询错误是暂时性的且可以安全重试,哪些错误是不可恢复的。

有两个政策可控制 LRO 轮询循环的行为:

  • 轮询退避政策控制循环在轮询进行中 LRO 的状态之前等待的时间。
  • 轮询错误政策控制如何处理轮询错误。某些轮询错误是不可恢复的,这表示操作已停止或调用方无权检查其状态。其他轮询错误是暂时性的,表示存在临时网络或服务问题。

您可以单独设置每项政策,既可以针对在客户端上启动的所有 LRO 进行设置,也可以针对单个请求进行设置。

准备工作

本文档使用 Cloud Storage 来说明轮询政策配置,但相同的概念也适用于使用 LRO 的任何 Google Cloud 服务。

在运行本文档中的代码之前,请先完成以下前提条件:

  • 按照 Rust 使用入门中的说明设置开发环境。
  • 按照将 Cloud Storage 与 Rust 搭配使用中的说明设置 Google Cloud 项目并启用结算功能,启用 Cloud Storage API,然后配置凭据。
  • 创建一个启用了分层命名空间的 Cloud Storage 存储桶,并创建一个要重命名的源文件夹。向您的主账号授予具有管理文件夹权限的 Identity and Access Management (IAM) 角色,例如 Storage Admin (roles/storage.admin)。

依赖项

将所需的 crate 添加到 Cargo.toml 文件中:

cargo add google-cloud-storage google-cloud-lro google-cloud-gax anyhow

为客户端上的所有请求配置轮询频率

如果您在客户端的大多数请求中使用相同的轮询退避政策,请在客户端构建器上配置该政策。以下示例为 StorageControl 客户端启动的所有 LRO 配置了指数轮询退避:

pub async fn client_backoff(bucket: &str, folder: &str, dest: &str) -> Result<()> {
    use google_cloud_gax::exponential_backoff::ExponentialBackoffBuilder;
    use google_cloud_lro::Poller;
    use std::time::Duration;

    let client = StorageControl::builder()
        .with_polling_backoff_policy(
            ExponentialBackoffBuilder::new()
                .with_initial_delay(Duration::from_millis(250))
                .with_maximum_delay(Duration::from_secs(10))
                .build()?,
        )
        .build()
        .await?;

    let response = client
        .rename_folder()
        .set_name(format!("projects/_/buckets/{bucket}/folders/{folder}"))
        .set_destination_folder_id(dest)
        .poller()
        .until_done()
        .await?;

    println!("LRO completed, response={response:?}");

    Ok(())
}

除非您在单个请求中替换该政策,否则此配置会应用于客户端启动的每个 LRO。在此示例中,客户端库在第一次轮询后等待 250 毫秒,在第二次轮询后等待 500 毫秒。然后,延迟时间会加倍,变为 1 秒、2 秒、4 秒和 8 秒,最后在所有后续轮询中达到 10 秒的上限。

为特定请求配置轮询频率

您还可以在单个请求构建器上配置轮询频率,这会替换任何客户端级轮询退避政策。将 RequestOptionsBuilder 引入作用域,并在请求构建器上调用 with_polling_backoff_policy:

pub async fn rpc_backoff(bucket: &str, folder: &str, dest: &str) -> Result<()> {
    use google_cloud_gax::exponential_backoff::ExponentialBackoffBuilder;
    use std::time::Duration;
    use google_cloud_gax::options::RequestOptionsBuilder;
    use google_cloud_lro::Poller;

    let client = StorageControl::builder().build().await?;

    let response = client
        .rename_folder()
        .with_polling_backoff_policy(
            ExponentialBackoffBuilder::new()
                .with_initial_delay(Duration::from_millis(250))
                .with_maximum_delay(Duration::from_secs(10))
                .build()?,
        )
        .set_name(format!("projects/_/buckets/{bucket}/folders/{folder}"))
        .set_destination_folder_id(dest)
        .poller()
        .until_done()
        .await?;

    println!("LRO completed, response={response:?}");

    Ok(())
}

为客户端上的所有请求配置轮询错误政策

如需控制循环重试哪些轮询错误以及何时停止轮询,请传递实现 PollingErrorPolicy 特征的类型,例如 Aip194Strict。

轮询错误政策仅在 LRO 等待循环期间控制错误。如需在发送启动 LRO 的初始请求时处理暂时性错误,还需在客户端上配置标准重试政策:

pub async fn client_errors(bucket: &str, folder: &str, dest: &str) -> Result<()> {
    use google_cloud_gax::polling_error_policy::Aip194Strict;
    use google_cloud_gax::polling_error_policy::PollingErrorPolicyExt;
    use google_cloud_gax::retry_policy;
    use google_cloud_gax::retry_policy::RetryPolicyExt;
    use std::time::Duration;
    use google_cloud_lro::Poller;

    let builder = StorageControl::builder().with_polling_error_policy(
        Aip194Strict
            .with_attempt_limit(100)
            .with_time_limit(Duration::from_secs(300)),
    );

    let client = builder
        .with_retry_policy(
            retry_policy::Aip194Strict
                .with_attempt_limit(100)
                .with_time_limit(Duration::from_secs(300)),
        )
        .build()
        .await?;

    let response = client
        .rename_folder()
        .set_name(format!("projects/_/buckets/{bucket}/folders/{folder}"))
        .set_destination_folder_id(dest)
        .poller()
        .until_done()
        .await?;

    println!("LRO completed, response={response:?}");

    Ok(())
}

为特定请求配置轮询错误政策

如需替换单个请求的轮询错误政策,请将 RequestOptionsBuilder 引入范围,并在请求构建器上调用 with_polling_error_policy:

pub async fn rpc_errors(bucket: &str, folder: &str, dest: &str) -> Result<()> {
    use google_cloud_gax::polling_error_policy::Aip194Strict;
    use google_cloud_gax::polling_error_policy::PollingErrorPolicyExt;
    use google_cloud_gax::retry_policy;
    use google_cloud_gax::retry_policy::RetryPolicyExt;
    use std::time::Duration;
    use google_cloud_gax::options::RequestOptionsBuilder;
    use google_cloud_lro::Poller;

    let client = StorageControl::builder()
        .with_retry_policy(
            retry_policy::Aip194Strict
                .with_attempt_limit(100)
                .with_time_limit(Duration::from_secs(300)),
        )
        .build()
        .await?;

    let response = client
        .rename_folder()
        .with_polling_error_policy(
            Aip194Strict
                .with_attempt_limit(100)
                .with_time_limit(Duration::from_secs(300)),
        )
        .set_name(format!("projects/_/buckets/{bucket}/folders/{folder}"))
        .set_destination_folder_id(dest)
        .poller()
        .until_done()
        .await?;

    println!("LRO completed, response={response:?}");

    Ok(())
}