設定輪詢政策

Rust 適用的 Cloud 用戶端程式庫提供輔助函式,可監控長時間執行的作業 (LRO) 進度。這些輔助程式會使用政策設定輪詢頻率,並判斷哪些輪詢錯誤是暫時性錯誤,可以安全重試,哪些錯誤則無法復原。

有兩項政策會控管 LRO 輪詢迴圈的行為:

  • 輪詢輪詢政策可控制迴圈在輪詢進行中 LRO 的狀態之前,應等待多久。
  • 輪詢錯誤政策可控管輪詢錯誤的處理方式。部分輪詢錯誤無法復原,表示作業已停止,或呼叫端沒有檢查狀態的權限。其他輪詢錯誤則為暫時性,表示網路或服務暫時發生問題。

您可以單獨設定每項政策,無論是針對用戶端啟動的所有 LRO,還是個別要求。

事前準備

本文以 Cloud Storage 為例,說明輪詢政策設定,但相同概念也適用於任何使用 LRO 的 Google Cloud 服務。

執行本文件中的程式碼前,請先完成下列必要條件:

  • 請按照「開始使用 Rust」一文的說明設定開發環境。
  • 按照「搭配使用 Cloud Storage 與 Rust」一文的指示,設定已啟用計費功能的 Google Cloud 專案、啟用 Cloud Storage API,以及設定憑證。
  • 建立 Cloud Storage bucket 並啟用階層命名空間,然後建立要重新命名的來源資料夾。為您的主體授予具備資料夾管理權限的 Identity and Access Management (IAM) 角色,例如 Storage 管理員 (roles/storage.admin)。

依附元件

在 Cargo.toml 檔案中新增必要的 crate:

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(())
}