RetryPolicy 類別
重試原則。
您可以直接設定管線中的重試原則,或根據每個呼叫進行調整。
- 繼承
-
azure.core.pipeline.policies._retry.RetryPolicyBaseRetryPolicyazure.core.pipeline.policies._base.HTTPPolicyRetryPolicy
建構函式
RetryPolicy(**kwargs: Any)
僅限關鍵字的參數
名稱 | Description |
---|---|
retry_total
|
允許的重試總數。 優先于其他計數。 預設值為 10。 |
retry_connect
|
要重試的連線相關錯誤數目。 這些是在要求傳送至遠端伺服器之前引發的錯誤,我們假設尚未觸發伺服器來處理要求。 預設值為 3。 |
retry_read
|
讀取錯誤時重試的次數。 這些錯誤會在要求傳送至伺服器之後引發,因此要求可能會有副作用。 預設值為 3。 |
retry_status
|
重試錯誤狀態碼的次數。 預設值為 3。 |
retry_backoff_factor
|
第二次嘗試之後要套用的輪詢因素, (大部分的錯誤都會立即由第二次嘗試解決,而不會延遲) 。 在固定模式中,重試原則一律會睡眠 {backoff factor}。 在 「指數」模式中,重試原則會睡眠: {backoff factor} * (2 ** ({total retries} - 1) ) 秒。 如果backoff_factor為 0.1,則重試會在重試之間睡眠 [0.0s, 0.2s, 0.4s, ...]。 預設值為 0.8。 |
retry_backoff_max
|
退班時間上限。 預設值為 120 秒, (2 分鐘) 。 |
retry_mode
|
已修正或指數延遲在temps 之間,預設值為指數。 |
timeout
|
作業的逾時設定以秒為單位,預設值為 604800s (7 天) 。 |
範例
設定重試原則。
from azure.core.pipeline.policies import RetryPolicy
retry_policy = RetryPolicy()
# Total number of retries to allow. Takes precedence over other counts.
# Default value is 10.
retry_policy.total_retries = 5
# How many connection-related errors to retry on.
# These are errors raised before the request is sent to the remote server,
# which we assume has not triggered the server to process the request. Default value is 3
retry_policy.connect_retries = 2
# How many times to retry on read errors.
# These errors are raised after the request was sent to the server, so the
# request may have side-effects. Default value is 3.
retry_policy.read_retries = 4
# How many times to retry on bad status codes. Default value is 3.
retry_policy.status_retries = 3
# A backoff factor to apply between attempts after the second try
# (most errors are resolved immediately by a second try without a delay).
# Retry policy will sleep for:
# {backoff factor} * (2 ** ({number of total retries} - 1))
# seconds. If the backoff_factor is 0.1, then the retry will sleep
# for [0.0s, 0.2s, 0.4s, ...] between retries.
# The default value is 0.8.
retry_policy.backoff_factor = 0.5
# The maximum back off time. Default value is 120 seconds (2 minutes).
retry_policy.backoff_max = 120
# Alternatively you can disable redirects entirely
retry_policy = RetryPolicy.no_retries()
# All of these settings can also be configured per operation.
policies.append(retry_policy)
client: PipelineClient[HttpRequest, HttpResponse] = PipelineClient(base_url=url, policies=policies)
request = HttpRequest("GET", url)
pipeline_response = client._pipeline.run(
request,
retry_total=10,
retry_connect=1,
retry_read=1,
retry_status=5,
retry_backoff_factor=0.5,
retry_backoff_max=120,
retry_on_methods=["GET"],
)
方法
configure_retries |
設定重試設定。 |
get_backoff_time |
傳回目前的輪詢時間。 |
get_retry_after |
以秒為單位取得 Retry-After 值。 |
increment |
遞增重試計數器。 |
is_exhausted |
檢查是否有任何重試剩餘。 |
is_retry |
檢查方法/狀態碼是否可重試。 根據允許清單和控制變數,例如允許的總重試次數、是否要遵守 Retry-After 標頭、此標頭是否存在,以及傳回的狀態碼是否位於要在上述標頭存在時重試的狀態代碼清單。 此行為為:
|
no_retries |
停用重試。 |
parse_retry_after |
協助程式剖析 Retry-After,並以秒為單位取得值。 |
send |
將 PipelineRequest 物件傳送至下一個原則。 如有必要,請使用重試設定。 |
sleep |
重試嘗試之間的睡眠。 這個方法會遵守伺服器的 |
update_context |
更新管線內容中的重試歷程記錄。 |
configure_retries
設定重試設定。
configure_retries(options: Dict[str, Any]) -> Dict[str, Any]
參數
名稱 | Description |
---|---|
options
必要
|
內容中的關鍵字引數。 |
傳回
類型 | Description |
---|---|
包含重試設定和歷程記錄的聽寫。 |
get_backoff_time
傳回目前的輪詢時間。
get_backoff_time(settings: Dict[str, Any]) -> float
參數
名稱 | Description |
---|---|
settings
必要
|
重試設定。 |
傳回
類型 | Description |
---|---|
目前的輪詢值。 |
get_retry_after
以秒為單位取得 Retry-After 值。
get_retry_after(response: PipelineResponse[Any, AllHttpResponseType]) -> float | None
參數
名稱 | Description |
---|---|
response
必要
|
PipelineResponse 物件 |
傳回
類型 | Description |
---|---|
以秒為單位 Retry-After 值。 |
increment
遞增重試計數器。
increment(settings: Dict[str, Any], response: PipelineRequest[HTTPRequestType] | PipelineResponse[HTTPRequestType, AllHttpResponseType] | None = None, error: Exception | None = None) -> bool
參數
名稱 | Description |
---|---|
settings
必要
|
重試設定。 |
response
|
管線回應物件。 預設值: None
|
error
|
要求期間發生錯誤,如果成功收到回應,則為 None。 預設值: None
|
傳回
類型 | Description |
---|---|
如果有更多可用的重試嘗試,是否可用任何重試嘗試,則為 True,否則為 False |
is_exhausted
檢查是否有任何重試剩餘。
is_exhausted(settings: Dict[str, Any]) -> bool
參數
名稱 | Description |
---|---|
settings
必要
|
重試設定 |
傳回
類型 | Description |
---|---|
False 表示有更多重試次數。 如果重試已用盡,則為 True。 |
is_retry
檢查方法/狀態碼是否可重試。
根據允許清單和控制變數,例如允許的總重試次數、是否要遵守 Retry-After 標頭、此標頭是否存在,以及傳回的狀態碼是否位於要在上述標頭存在時重試的狀態代碼清單。
此行為為:
-
If status_code < 400: don't retry
-
Else if Retry-After present: retry
-
Else: retry based on the safe status code list ([408, 429, 500, 502, 503, 504])
is_retry(settings: Dict[str, Any], response: PipelineResponse[HTTPRequestType, AllHttpResponseType]) -> bool
參數
名稱 | Description |
---|---|
settings
必要
|
重試設定。 |
response
必要
|
PipelineResponse 物件 |
傳回
類型 | Description |
---|---|
如果可重試方法/狀態碼,則為 True。 如果無法重試,則為 False。 |
no_retries
parse_retry_after
協助程式剖析 Retry-After,並以秒為單位取得值。
parse_retry_after(retry_after: str) -> float
參數
名稱 | Description |
---|---|
retry_after
必要
|
Retry-After 標頭 |
傳回
類型 | Description |
---|---|
以秒為單位 Retry-After 值。 |
send
將 PipelineRequest 物件傳送至下一個原則。 如有必要,請使用重試設定。
send(request: PipelineRequest[HTTPRequestType]) -> PipelineResponse[HTTPRequestType, HTTPResponseType]
參數
名稱 | Description |
---|---|
request
必要
|
PipelineRequest 物件 |
傳回
類型 | Description |
---|---|
會傳回 PipelineResponse,如果超過重試次數上限,則引發錯誤。 |
例外狀況
類型 | Description |
---|---|
azure.core.exceptions.AzureError if maximum retries exceeded.
|
|
azure.core.exceptions.ClientAuthenticationError if authentication
|
sleep
重試嘗試之間的睡眠。
這個方法會遵守伺服器的 Retry-After
回應標頭,並睡眠所要求的時間持續時間。 如果不存在,則會使用指數輪詢。 根據預設,輪詢因數為 0,此方法會立即傳回。
sleep(settings: Dict[str, Any], transport: HttpTransport[HTTPRequestType, HTTPResponseType], response: PipelineResponse[HTTPRequestType, HTTPResponseType] | None = None) -> None
參數
名稱 | Description |
---|---|
settings
必要
|
重試設定。 |
transport
必要
|
HTTP 傳輸類型。 |
response
|
PipelineResponse 物件。 預設值: None
|
update_context
更新管線內容中的重試歷程記錄。
update_context(context: PipelineContext, retry_settings: Dict[str, Any]) -> None
參數
名稱 | Description |
---|---|
context
必要
|
管線內容。 |
retry_settings
必要
|
重試設定。 |
屬性
BACKOFF_MAX
輪詢時間上限。
BACKOFF_MAX = 120
next
下一個原則的指標,或包裝為原則) 的傳輸 (。 將會在管線建立時設定。
next: HTTPPolicy[HTTPRequestType, HTTPResponseType]
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應