次の方法で共有


RetryPolicy クラス

再試行ポリシー。

パイプライン内の再試行ポリシーは、直接構成することも、呼び出しごとに調整することもできます。

継承
azure.core.pipeline.policies._retry.RetryPolicyBase
RetryPolicy
azure.core.pipeline.policies._base.HTTPPolicy
RetryPolicy

コンストラクター

RetryPolicy(**kwargs: Any)

キーワードのみのパラメーター

名前 説明
retry_total
int

許可する再試行の合計数。 他のカウントよりも優先されます。 既定値は 10 です。

retry_connect
int

再試行する接続関連エラーの数。 これらは、要求がリモート サーバーに送信される前に発生するエラーです。これは、要求を処理するためにサーバーがトリガーされていないと想定しています。 既定値は 3 です。

retry_read
int

読み取りエラーで再試行する回数。 これらのエラーは、要求がサーバーに送信された後に発生するため、要求に副作用が生じる可能性があります。 既定値は 3 です。

retry_status
int

無効な状態コードで再試行する回数。 既定値は 3 です。

retry_backoff_factor

2 回目の試行後の試行間に適用するバックオフ係数 (ほとんどのエラーは、遅延なしで 2 回目の試行によってすぐに解決されます)。 固定モードでは、再試行ポリシーは常に {backoff factor} のスリープ状態になります。 'exponential' モードでは、再試行ポリシーは {backoff factor} * (2 ** ({合計再試行回数} - 1)) 秒でスリープ状態になります。 backoff_factorが 0.1 の場合、再試行の間に [0.0s,0.2s, 0.4s, ...] がスリープ状態になります。 既定値は 0.8 です。

retry_backoff_max
int

最大バックオフ時間。 既定値は 120 秒 (2 分) です。

retry_mode

attemps 間の固定遅延または指数遅延。既定値は指数関数です。

timeout
int

操作のタイムアウト設定 (秒単位)、既定値は 604800 秒 (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 ヘッダーを尊重するかどうか、このヘッダーが存在するかどうか、返された状態コードが前述のヘッダーの存在時に再試行される状態コードのリストにあるかどうかなど)。

動作は次のとおりです。

  •   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])
    
no_retries

再試行を無効にします。

parse_retry_after

Retry-After を解析し、秒単位で値を取得するヘルパー。

send

PipelineRequest オブジェクトを次のポリシーに送信します。 必要に応じて再試行設定を使用します。

sleep

再試行の間にスリープ状態になります。

このメソッドは、サーバーの Retry-After 応答ヘッダーを尊重し、要求された時間の期間をスリープ状態にします。 存在しない場合は、指数バックオフが使用されます。 既定では、バックオフ係数は 0 で、このメソッドはすぐに返されます。

update_context

パイプライン コンテキストで再試行履歴を更新します。

configure_retries

再試行設定を構成します。

configure_retries(options: Dict[str, Any]) -> Dict[str, Any]

パラメーター

名前 説明
options
必須

コンテキストから引数をキーワード (keyword)します。

戻り値

説明

再試行の設定と履歴を含むディクテーション。

get_backoff_time

現在のバックオフ時間を返します。

get_backoff_time(settings: Dict[str, Any]) -> float

パラメーター

名前 説明
settings
必須

再試行設定。

戻り値

説明

現在のバックオフ値。

get_retry_after

Retry-After の値を秒単位で取得します。

get_retry_after(response: PipelineResponse[Any, AllHttpResponseType]) -> float | None

パラメーター

名前 説明
response
必須

PipelineResponse オブジェクト

戻り値

説明

Retry-After の値 (秒単位)。

increment

再試行カウンターをインクリメントします。

increment(settings: Dict[str, Any], response: PipelineRequest[HTTPRequestType] | PipelineResponse[HTTPRequestType, AllHttpResponseType] | None = None, error: Exception | None = None) -> bool

パラメーター

名前 説明
settings
必須

再試行設定。

response

パイプライン応答オブジェクト。

既定値: None
error

要求中にエラーが発生しました。応答が正常に受信された場合は None です。

既定値: None

戻り値

説明

再試行回数が増える場合は True、それ以外の場合は False

is_exhausted

再試行が残っているかどうかを確認します。

is_exhausted(settings: Dict[str, Any]) -> bool

パラメーター

名前 説明
settings
必須

再試行設定

戻り値

説明

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

パラメーター

名前 説明
settings
必須

再試行設定。

response
必須

PipelineResponse オブジェクト

戻り値

説明

メソッド/状態コードが再試行可能な場合は True。 False の場合は再試行できません。

no_retries

再試行を無効にします。

no_retries() -> ClsRetryPolicy

戻り値

説明

再試行が無効になっている再試行ポリシー。

parse_retry_after

Retry-After を解析し、秒単位で値を取得するヘルパー。

parse_retry_after(retry_after: str) -> float

パラメーター

名前 説明
retry_after
必須
str

Retry-After ヘッダー

戻り値

説明

Retry-After の値 (秒単位)。

send

PipelineRequest オブジェクトを次のポリシーに送信します。 必要に応じて再試行設定を使用します。

send(request: PipelineRequest[HTTPRequestType]) -> PipelineResponse[HTTPRequestType, HTTPResponseType]

パラメーター

名前 説明
request
必須

PipelineRequest オブジェクト

戻り値

説明

PipelineResponse を返すか、最大再試行回数を超えた場合にエラーが発生します。

例外

説明
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

パラメーター

名前 説明
settings
必須

再試行設定。

transport
必須

HTTP トランスポートの種類。

response

PipelineResponse オブジェクト。

既定値: None

update_context

パイプライン コンテキストで再試行履歴を更新します。

update_context(context: PipelineContext, retry_settings: Dict[str, Any]) -> None

パラメーター

名前 説明
context
必須

パイプライン コンテキスト。

retry_settings
必須

再試行設定。

属性

BACKOFF_MAX

最大バックオフ時間。

BACKOFF_MAX = 120

next

次のポリシーまたはトランスポート (ポリシーとしてラップ) へのポインター。 パイプラインの作成時に設定されます。

next: HTTPPolicy[HTTPRequestType, HTTPResponseType]