RetryPolicy
open class RetryPolicy : @unchecked Sendable, RequestInterceptor
A retry policy that retries requests using an exponential backoff for allowed HTTP methods and HTTP status codes as well as certain types of networking errors.
-
The default retry limit for retry policies.
Declaration
Swift
public static let defaultRetryLimit: UInt
-
The default exponential backoff base for retry policies (must be a minimum of 2).
Declaration
Swift
public static let defaultExponentialBackoffBase: UInt
-
The default exponential backoff scale for retry policies.
Declaration
Swift
public static let defaultExponentialBackoffScale: Double
-
The default HTTP methods to retry. See RFC 2616 - Section 9.1.2 for more information.
Declaration
Swift
public static let defaultRetryableHTTPMethods: Set<HTTPMethod>
-
The default HTTP status codes to retry. See RFC 2616 - Section 10 for more information.
Declaration
Swift
public static let defaultRetryableHTTPStatusCodes: Set<Int>
-
The default URL error codes to retry.
Declaration
Swift
public static let defaultRetryableURLErrorCodes: Set<URLError.Code>
-
The total number of times the request is allowed to be retried.
Declaration
Swift
public let retryLimit: UInt
-
The base of the exponential backoff policy (should always be greater than or equal to 2).
Declaration
Swift
public let exponentialBackoffBase: UInt
-
The scale of the exponential backoff.
Declaration
Swift
public let exponentialBackoffScale: Double
-
The HTTP methods that are allowed to be retried.
Declaration
Swift
public let retryableHTTPMethods: Set<HTTPMethod>
-
The HTTP status codes that are automatically retried by the policy.
Declaration
Swift
public let retryableHTTPStatusCodes: Set<Int>
-
The URL error codes that are automatically retried by the policy.
Declaration
Swift
public let retryableURLErrorCodes: Set<URLError.Code>
-
init(retryLimit:
exponentialBackoffBase: exponentialBackoffScale: retryableHTTPMethods: retryableHTTPStatusCodes: retryableURLErrorCodes: ) Creates a
RetryPolicy
from the specified parameters.Declaration
Swift
public init(retryLimit: UInt = RetryPolicy.defaultRetryLimit, exponentialBackoffBase: UInt = RetryPolicy.defaultExponentialBackoffBase, exponentialBackoffScale: Double = RetryPolicy.defaultExponentialBackoffScale, retryableHTTPMethods: Set<HTTPMethod> = RetryPolicy.defaultRetryableHTTPMethods, retryableHTTPStatusCodes: Set<Int> = RetryPolicy.defaultRetryableHTTPStatusCodes, retryableURLErrorCodes: Set<URLError.Code> = RetryPolicy.defaultRetryableURLErrorCodes)
Parameters
retryLimit
The total number of times the request is allowed to be retried.
2
by default.exponentialBackoffBase
The base of the exponential backoff policy.
2
by default.exponentialBackoffScale
The scale of the exponential backoff.
0.5
by default.retryableHTTPMethods
The HTTP methods that are allowed to be retried.
RetryPolicy.defaultRetryableHTTPMethods
by default.retryableHTTPStatusCodes
The HTTP status codes that are automatically retried by the policy.
RetryPolicy.defaultRetryableHTTPStatusCodes
by default.retryableURLErrorCodes
The URL error codes that are automatically retried by the policy.
RetryPolicy.defaultRetryableURLErrorCodes
by default. -
Declaration
Swift
open func retry(_ request: Request, for session: Session, dueTo error: any Error, completion: @escaping (RetryResult) -> Void)
-
Determines whether or not to retry the provided
Request
.Declaration
Swift
open func shouldRetry(request: Request, dueTo error: any Error) -> Bool
Parameters
request
Request
that failed due to the providedError
.error
Error
encountered while executing theRequest
.Return Value
Bool
determining whether or not to retry theRequest
.