Google Cloud Rust 適用的用戶端程式庫可以自動重試因暫時性錯誤而失敗的作業。瞭解如何建立及自訂重試迴圈、為所有要求實作常見的重試政策,以及為特定要求覆寫預設政策。
必要條件
本指南中的範例使用 Secret Manager 服務。不過,同樣的概念也適用於其他 Google Cloud 服務。
繼續操作之前,請按照「使用 Secret Manager 建立及存取密鑰」一文的說明,啟用及驗證 Secret Manager API。
依附元件
啟用 Secret Manager 後,請在 Cargo.toml 檔案中宣告依附元件:
cargo add google-cloud-secretmanager-v1
設定預設重試政策
本範例使用 Aip194Strict 政策。這項政策是以 AIP-194 的規範為依據,該規範記錄了 Google API 用戶端應自動重試要求的條件。
如要將 Aip194Strict 政策設為服務的預設政策,請在用戶端初始化期間設定政策:
let client = SecretManagerService::builder()
.with_retry_policy(Aip194Strict)
.build()
.await?;
設定政策後,即可照常使用服務:
let mut list = client
.list_secrets()
.set_parent(format!("projects/{project_id}"))
.by_item();
while let Some(secret) = list.next().await {
let secret = secret?;
println!(" secret={}", secret.name);
}
設定預設重試政策:完整程式碼
這個程式碼範例會將 Aip194Strict 政策套用至 Secret Manager 服務。
use google_cloud_gax::paginator::ItemPaginator as _;
use google_cloud_gax::retry_policy::Aip194Strict;
use google_cloud_secretmanager_v1::client::SecretManagerService;
pub async fn sample(project_id: &str) -> anyhow::Result<()> {
let client = SecretManagerService::builder()
.with_retry_policy(Aip194Strict)
.build()
.await?;
let mut list = client
.list_secrets()
.set_parent(format!("projects/{project_id}"))
.by_item();
while let Some(secret) = list.next().await {
let secret = secret?;
println!(" secret={}", secret.name);
}
Ok(())
}
設定預設重試政策和限制
根據預設,Aip194Strict 政策不會限制重試次數或重試要求所花費的時間。不過,您可以為政策新增限制。
舉例來說,您可以同時限制嘗試次數和重試迴圈中的時間:
let client = SecretManagerService::builder()
.with_retry_policy(
Aip194Strict
.with_attempt_limit(5)
.with_time_limit(Duration::from_secs(15)),
)
.build()
.await?;
完成這項設定後,要求就會照常運作:
let mut list = client
.list_secrets()
.set_parent(format!("projects/{project_id}"))
.by_item();
while let Some(secret) = list.next().await {
let secret = secret?;
println!(" secret={}", secret.name);
}
設定預設重試政策和限制:完整程式碼
這個程式碼範例會將 Aip194Strict 政策套用至 Secret Manager 服務,並自訂嘗試次數和時間限制。
use google_cloud_gax::paginator::ItemPaginator as _;
use google_cloud_gax::retry_policy::Aip194Strict;
use google_cloud_gax::retry_policy::RetryPolicyExt;
use google_cloud_secretmanager_v1::client::SecretManagerService;
use std::time::Duration;
pub async fn sample(project_id: &str) -> anyhow::Result<()> {
let client = SecretManagerService::builder()
.with_retry_policy(
Aip194Strict
.with_attempt_limit(5)
.with_time_limit(Duration::from_secs(15)),
)
.build()
.await?;
let mut list = client
.list_secrets()
.set_parent(format!("projects/{project_id}"))
.by_item();
while let Some(secret) = list.next().await {
let secret = secret?;
println!(" secret={}", secret.name);
}
Ok(())
}
覆寫要求的重試政策
有時應用程式需要針對特定要求覆寫重試政策。舉例來說,應用程式開發人員可能瞭解服務或應用程式的具體詳細資料,並判斷容許更多錯誤是安全的。
舉例來說,刪除密鑰是等冪運算,因為只能成功一次。 但用戶端程式庫會假設所有刪除作業都不安全。您可以為要求覆寫政策:
client
.delete_secret()
.set_name(format!("projects/{project_id}/secrets/{secret_id}"))
.with_retry_policy(
AlwaysRetry
.with_attempt_limit(5)
.with_time_limit(Duration::from_secs(15)),
)
.send()
.await?;
覆寫要求的重試政策:完整程式碼
這個程式碼範例會針對 Secret Manager 服務的特定要求,覆寫嘗試次數限制和時間限制。
use google_cloud_gax::options::RequestOptionsBuilder;
use google_cloud_gax::retry_policy::AlwaysRetry;
use google_cloud_gax::retry_policy::RetryPolicyExt;
use google_cloud_secretmanager_v1::client::SecretManagerService;
use std::time::Duration;
pub async fn sample(
client: &SecretManagerService,
project_id: &str,
secret_id: &str,
) -> anyhow::Result<()> {
client
.delete_secret()
.set_name(format!("projects/{project_id}/secrets/{secret_id}"))
.with_retry_policy(
AlwaysRetry
.with_attempt_limit(5)
.with_time_limit(Duration::from_secs(15)),
)
.send()
.await?;
Ok(())
}