本頁面中的部分或全部資訊可能不適用於 Trusted Cloud by S3NS。
重試失敗的要求
本頁面說明重試對 Identity and Access Management (IAM) API 提出的失敗要求時,應採取的最佳做法。
對於可安全重試的要求,建議使用部分指數輪詢,並導入隨機延遲。
部分指數輪詢總覽
對 IAM API 提出的要求可能會成功或失敗。如果應用程式在未等待的情況下重試失敗的要求,可能會在短時間內向 IAM 傳送大量重試要求。因此,您可能會超出配額和限制,而這些配額和限制適用於 Trusted Cloud by S3NS 專案中的每個 IAM 資源。
為避免觸發這個問題,我們強烈建議您使用部分指數輪詢,並導入隨機延遲,這是網路應用程式的標準錯誤處理策略。採用這種方法時,用戶端會定期重試失敗的要求,並以指數方式增加每次重試之間的延遲時間。重試之間也會加入隨機延遲時間 (稱為抖動),這項隨機延遲有助於避免多個用戶端同步重試,也就是所謂的雷鳴群效應問題。
指數輪詢演算法
下列演算法會實作指數輪詢,並加入隨機延遲:
-
將要求傳送至 IAM。
-
如果要求失敗,請等待 1 +
random-fraction
秒後再重試要求。
-
如果要求失敗,請等待 2 +
random-fraction
秒後再重試要求。
-
如果要求失敗,請等待 4 +
random-fraction
秒後再重試要求。
-
繼續這個模式,每次重試後等待 2n +
random-fraction
秒,最多重試 maximum-backoff
次。
-
deadline
秒後,停止重試要求。
實作演算法時,請使用下列值:
-
每次重試前,等待時間為
min((2n + random-fraction), maximum-backoff)
,其中 n
從 0 開始,每次重試時會增加 1。
-
將
random-fraction
改為小於或等於 1 的隨機分數值。每次重試時使用不同的值。加入這個隨機值可避免用戶端同步處理,並同時傳送大量重試要求。
-
將
maximum-backoff
取代為重試間等待時間上限 (以秒為單位)。一般值為 32 或 64 (25 或 26) 秒。請選擇最適合您用途的值。
-
將
deadline
改成持續傳送重試要求的秒數上限。選擇反映您用途的值。舉例來說,在時間敏感度不高的持續整合/持續部署 (CI/CD) 管道中,您可以將 deadline
設為 300 秒 (5 分鐘)。
可重試的錯誤類型
如果 IAM API 的所有要求傳回錯誤代碼 500
、502
、503
或 504
,請使用這項重試策略。
您也可以選擇將這項重試策略用於 IAM API 的要求,這些要求會傳回 404
錯誤代碼。IAM 讀取作業最終會保持一致,因此資源可能不會在建立後立即顯示,這可能會導致 404
錯誤。
此外,對於傳回錯誤代碼 409
和狀態 ABORTED
的所有 IAM API 要求,請使用這個重試策略的修改版本。這類錯誤表示發生並行問題。舉例來說,您可能嘗試更新另一個用戶端已覆寫的允許政策。發生這類錯誤時,您應一律重試整個讀取 - 修改 - 寫入系列要求,並使用部分指數輪詢和引入的抖動。如果只重試寫入作業,要求仍會失敗。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eThis document outlines best practices for retrying failed requests to the Identity and Access Management (IAM) API, recommending the use of truncated exponential backoff with jitter for safe-to-retry requests.\u003c/p\u003e\n"],["\u003cp\u003eUsing truncated exponential backoff with jitter helps prevent overwhelming the IAM API with a large number of retries, which can lead to exceeding project quotas and limits.\u003c/p\u003e\n"],["\u003cp\u003eThe retry algorithm involves exponentially increasing wait times between retries, with the addition of a random delay (jitter) to avoid synchronized retries from multiple clients, and a cap on the wait time, followed by a final deadline.\u003c/p\u003e\n"],["\u003cp\u003eThe recommended retry strategy should be used for IAM API requests that return \u003ccode\u003e500\u003c/code\u003e, \u003ccode\u003e502\u003c/code\u003e, \u003ccode\u003e503\u003c/code\u003e, or \u003ccode\u003e504\u003c/code\u003e error codes, and optionally \u003ccode\u003e404\u003c/code\u003e errors due to eventual consistency, and a modified version should be used for \u003ccode\u003e409\u003c/code\u003e \u003ccode\u003eABORTED\u003c/code\u003e errors.\u003c/p\u003e\n"],["\u003cp\u003eFor \u003ccode\u003e409\u003c/code\u003e errors, which typically signify concurrency issues, you should retry the entire read-modify-write series of requests, as retrying only the write operation will not resolve the issue.\u003c/p\u003e\n"]]],[],null,[]]