123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- import Foundation
- open class RetryPolicy: RequestInterceptor {
-
- public static let defaultRetryLimit: UInt = 2
-
- public static let defaultExponentialBackoffBase: UInt = 2
-
- public static let defaultExponentialBackoffScale: Double = 0.5
-
-
- public static let defaultRetryableHTTPMethods: Set<HTTPMethod> = [.delete,
- .get,
- .head,
- .options,
- .put,
- .trace
- ]
-
-
- public static let defaultRetryableHTTPStatusCodes: Set<Int> = [408,
- 500,
- 502,
- 503,
- 504
- ]
-
- public static let defaultRetryableURLErrorCodes: Set<URLError.Code> = [
-
-
-
-
-
- .backgroundSessionInUseByAnotherProcess,
-
-
-
-
-
- .backgroundSessionWasDisconnected,
-
-
- .badServerResponse,
-
-
-
-
-
-
- .callIsActive,
-
-
-
-
-
-
-
-
- .cannotConnectToHost,
-
-
-
-
-
-
-
-
-
-
-
- .cannotFindHost,
-
-
- .cannotLoadFromNetwork,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- .dataNotAllowed,
-
-
- .dnsLookupFailed,
-
-
- .downloadDecodingFailedMidStream,
-
-
- .downloadDecodingFailedToComplete,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- .internationalRoamingOff,
-
-
- .networkConnectionLost,
-
-
-
-
-
-
- .notConnectedToInternet,
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- .secureConnectionFailed,
-
-
- .serverCertificateHasBadDate,
-
-
-
-
-
- .serverCertificateNotYetValid,
-
-
-
-
-
- .timedOut
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ]
-
- public let retryLimit: UInt
-
- public let exponentialBackoffBase: UInt
-
- public let exponentialBackoffScale: Double
-
- public let retryableHTTPMethods: Set<HTTPMethod>
-
- public let retryableHTTPStatusCodes: Set<Int>
-
- public let retryableURLErrorCodes: Set<URLError.Code>
-
-
-
-
-
-
-
-
-
-
-
-
- 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) {
- precondition(exponentialBackoffBase >= 2, "The `exponentialBackoffBase` must be a minimum of 2.")
- self.retryLimit = retryLimit
- self.exponentialBackoffBase = exponentialBackoffBase
- self.exponentialBackoffScale = exponentialBackoffScale
- self.retryableHTTPMethods = retryableHTTPMethods
- self.retryableHTTPStatusCodes = retryableHTTPStatusCodes
- self.retryableURLErrorCodes = retryableURLErrorCodes
- }
- open func retry(_ request: Request,
- for session: Session,
- dueTo error: Error,
- completion: @escaping (RetryResult) -> Void) {
- if request.retryCount < retryLimit, shouldRetry(request: request, dueTo: error) {
- completion(.retryWithDelay(pow(Double(exponentialBackoffBase), Double(request.retryCount)) * exponentialBackoffScale))
- } else {
- completion(.doNotRetry)
- }
- }
-
-
-
-
-
-
-
- open func shouldRetry(request: Request, dueTo error: Error) -> Bool {
- guard let httpMethod = request.request?.method, retryableHTTPMethods.contains(httpMethod) else { return false }
- if let statusCode = request.response?.statusCode, retryableHTTPStatusCodes.contains(statusCode) {
- return true
- } else {
- let errorCode = (error as? URLError)?.code
- let afErrorCode = (error.asAFError?.underlyingError as? URLError)?.code
- guard let code = errorCode ?? afErrorCode else { return false }
- return retryableURLErrorCodes.contains(code)
- }
- }
- }
- open class ConnectionLostRetryPolicy: RetryPolicy {
-
-
-
-
-
-
-
-
-
-
-
- public init(retryLimit: UInt = RetryPolicy.defaultRetryLimit,
- exponentialBackoffBase: UInt = RetryPolicy.defaultExponentialBackoffBase,
- exponentialBackoffScale: Double = RetryPolicy.defaultExponentialBackoffScale,
- retryableHTTPMethods: Set<HTTPMethod> = RetryPolicy.defaultRetryableHTTPMethods) {
- super.init(retryLimit: retryLimit,
- exponentialBackoffBase: exponentialBackoffBase,
- exponentialBackoffScale: exponentialBackoffScale,
- retryableHTTPMethods: retryableHTTPMethods,
- retryableHTTPStatusCodes: [],
- retryableURLErrorCodes: [.networkConnectionLost])
- }
- }
|