Comprehensive retry mechanisms including exponential backoff, system error retry, and throttling retry policies for handling transient failures and service throttling.
Implements exponential backoff retry strategy with configurable delay and jitter.
/**
* A retry policy that specifically seeks to handle errors in the underlying transport layer (e.g. DNS lookup failures)
* by retrying the operation with exponentially increasing delays between retries
* @param options - Options for configuring the exponential retry policy
* @returns PipelinePolicy that implements exponential retry logic
*/
function exponentialRetryPolicy(options?: ExponentialRetryPolicyOptions): PipelinePolicy;
/**
* The programmatic identifier of the exponentialRetryPolicy
*/
const exponentialRetryPolicyName: string;
interface ExponentialRetryPolicyOptions {
/**
* The maximum number of retry attempts. Defaults to 3.
*/
maxRetries?: number;
/**
* The amount of delay in milliseconds between retry attempts. Defaults to 1000 (1 second).
* The delay increases exponentially with each retry up to a maximum specified by maxRetryDelayInMs.
*/
retryDelayInMs?: number;
/**
* The maximum delay in milliseconds allowed before retrying an operation. Defaults to 64000 (64 seconds).
*/
maxRetryDelayInMs?: number;
}Usage Examples:
import { exponentialRetryPolicy, type ExponentialRetryPolicyOptions } from "@azure/core-rest-pipeline";
// Basic exponential retry
const basicRetryPolicy = exponentialRetryPolicy();
// Customized exponential retry
const customRetryOptions: ExponentialRetryPolicyOptions = {
maxRetries: 5,
retryDelayInMs: 2000,
maxRetryDelayInMs: 120000
};
const customRetryPolicy = exponentialRetryPolicy(customRetryOptions);
// Add to pipeline in retry phase
pipeline.addPolicy(customRetryPolicy, { phase: "Retry" });Standard retry policy that combines multiple retry strategies for common scenarios.
/**
* A policy that retries according to three strategies:
* - When the server sends a 429 response with a Retry-After header
* - When the server sends a retriable error status code (502, 503, 504)
* - When a request fails due to system errors (ENOTFOUND, ECONNRESET, etc.)
* @param options - Options for configuring the default retry policy
* @returns PipelinePolicy that implements default retry logic
*/
function defaultRetryPolicy(options?: DefaultRetryPolicyOptions): PipelinePolicy;
interface DefaultRetryPolicyOptions {
/**
* The maximum number of retry attempts. Defaults to 3.
*/
maxRetries?: number;
/**
* The amount of delay in milliseconds between retry attempts. Defaults to 1000 (1 second).
*/
retryDelayInMs?: number;
/**
* The maximum delay in milliseconds allowed before retrying an operation. Defaults to 64000 (64 seconds).
*/
maxRetryDelayInMs?: number;
}Usage Examples:
import { defaultRetryPolicy, type DefaultRetryPolicyOptions } from "@azure/core-rest-pipeline";
// Use default retry settings
const standardRetryPolicy = defaultRetryPolicy();
// Customize retry behavior
const retryOptions: DefaultRetryPolicyOptions = {
maxRetries: 4,
retryDelayInMs: 1500,
maxRetryDelayInMs: 30000
};
const customDefaultPolicy = defaultRetryPolicy(retryOptions);
pipeline.addPolicy(customDefaultPolicy, { phase: "Retry" });Specialized retry policy for handling system-level errors like network connectivity issues.
/**
* A retry policy that specifically seeks to handle errors in the underlying transport layer
* (e.g. DNS lookup failures) by retrying the operation
* @param options - Options for configuring the system error retry policy
* @returns PipelinePolicy that handles system-level retry logic
*/
function systemErrorRetryPolicy(options?: SystemErrorRetryPolicyOptions): PipelinePolicy;
/**
* The programmatic identifier of the systemErrorRetryPolicy
*/
const systemErrorRetryPolicyName: string;
interface SystemErrorRetryPolicyOptions {
/**
* The maximum number of retry attempts. Defaults to 3.
*/
maxRetries?: number;
/**
* The amount of delay in milliseconds between retry attempts. Defaults to 1000 (1 second).
*/
retryDelayInMs?: number;
/**
* The maximum delay in milliseconds allowed before retrying an operation. Defaults to 64000 (64 seconds).
*/
maxRetryDelayInMs?: number;
}Usage Examples:
import { systemErrorRetryPolicy, type SystemErrorRetryPolicyOptions } from "@azure/core-rest-pipeline";
// Handle network connectivity issues
const networkRetryPolicy = systemErrorRetryPolicy({
maxRetries: 5,
retryDelayInMs: 500,
maxRetryDelayInMs: 10000
});
pipeline.addPolicy(networkRetryPolicy, { phase: "Retry" });Handles service throttling responses (429 status codes) with respect for Retry-After headers.
/**
* A policy that retries when the server sends a 429 response with a Retry-After header
* @param options - Options for configuring the throttling retry policy
* @returns PipelinePolicy that handles throttling retry logic
*/
function throttlingRetryPolicy(options?: ThrottlingRetryPolicyOptions): PipelinePolicy;
/**
* The programmatic identifier of the throttlingRetryPolicy
*/
const throttlingRetryPolicyName: string;
interface ThrottlingRetryPolicyOptions {
/**
* The maximum number of retry attempts. Defaults to 3.
*/
maxRetries?: number;
}Usage Examples:
import { throttlingRetryPolicy, type ThrottlingRetryPolicyOptions } from "@azure/core-rest-pipeline";
// Handle service throttling
const throttlePolicy = throttlingRetryPolicy({
maxRetries: 10 // Higher retry count for throttling scenarios
});
pipeline.addPolicy(throttlePolicy, { phase: "Retry" });Flexible retry policy with custom retry strategies and conditions.
/**
* A generic retry policy with configurable retry strategies
* @param strategies - Array of retry strategies to apply
* @param options - Options for configuring the retry policy
* @returns PipelinePolicy that implements custom retry logic
*/
function retryPolicy(
strategies: RetryStrategy[],
options?: RetryPolicyOptions
): PipelinePolicy;
interface RetryPolicyOptions {
/**
* The maximum number of retry attempts. Defaults to 3.
*/
maxRetries?: number;
}
/**
* Strategy interface for implementing custom retry logic
*/
interface RetryStrategy {
/**
* The name of the retry strategy
*/
name: string;
/**
* Determines if a request should be retried and calculates the retry delay
* @param retryInformation - Information about the current retry attempt
* @returns RetryModifiers or undefined if should not retry
*/
retry(retryInformation: RetryInformation): RetryModifiers | undefined;
}
interface RetryInformation {
/**
* The response that triggered the retry
*/
response?: PipelineResponse;
/**
* The error that triggered the retry
*/
responseError?: Error;
/**
* The number of retries attempted so far
*/
retryCount: number;
}
interface RetryModifiers {
/**
* Whether to skip this retry attempt
*/
skipStrategy?: boolean;
/**
* Additional delay in milliseconds before retrying
*/
retryAfterInMs?: number;
}Usage Examples:
import {
retryPolicy,
type RetryStrategy,
type RetryInformation,
type RetryModifiers
} from "@azure/core-rest-pipeline";
// Custom retry strategy for specific error codes
const customStrategy: RetryStrategy = {
name: "customErrorRetry",
retry(retryInfo: RetryInformation): RetryModifiers | undefined {
if (retryInfo.response?.status === 503) {
return { retryAfterInMs: 5000 }; // Retry after 5 seconds for 503 errors
}
if (retryInfo.responseError?.message?.includes("ECONNRESET")) {
return { retryAfterInMs: 1000 }; // Retry after 1 second for connection resets
}
return undefined; // Don't retry for other cases
}
};
// Create retry policy with custom strategy
const customRetryPolicy = retryPolicy([customStrategy], {
maxRetries: 5
});
pipeline.addPolicy(customRetryPolicy, { phase: "Retry" });Base interface for configuring retry behavior across all retry policies.
/**
* Options that control how to retry failed requests
*/
interface PipelineRetryOptions {
/**
* The maximum number of retry attempts. Defaults to 3.
*/
maxRetries?: number;
/**
* The amount of delay in milliseconds between retry attempts. Defaults to 1000 (1 second).
* The delay increases exponentially with each retry up to a maximum specified by maxRetryDelayInMs.
*/
retryDelayInMs?: number;
/**
* The maximum delay in milliseconds allowed before retrying an operation. Defaults to 64000 (64 seconds).
*/
maxRetryDelayInMs?: number;
}You can combine multiple retry policies for comprehensive error handling:
import {
createPipelineFromOptions,
exponentialRetryPolicy,
throttlingRetryPolicy,
systemErrorRetryPolicy
} from "@azure/core-rest-pipeline";
// Pipeline with multiple retry strategies
const pipeline = createPipelineFromOptions({});
// Add policies in order of specificity
pipeline.addPolicy(throttlingRetryPolicy({ maxRetries: 10 }), { phase: "Retry" });
pipeline.addPolicy(systemErrorRetryPolicy({ maxRetries: 5 }), { phase: "Retry" });
pipeline.addPolicy(exponentialRetryPolicy({ maxRetries: 3 }), { phase: "Retry" });/**
* Policy name constants for use in pipeline configuration
*/
const exponentialRetryPolicyName: string;
const systemErrorRetryPolicyName: string;
const throttlingRetryPolicyName: string;These constants are used for policy ordering and identification in pipeline configuration.