@@ -62,10 +62,10 @@ public extension APIClient.Configs {
6262 set { self [ \. retryCondition] = newValue }
6363 }
6464
65- /// The maximum number of retries for a request. If `nil`, it will retry indefinitely.
65+ /// The maximum number of retries for a request. If `nil`, it will retry indefinitely. Default to 5.
6666 /// - Note: This configuration works only if you use the `retry()` modifier.
6767 var retryLimit : Int ? {
68- get { self [ \. retryLimit] }
68+ get { self [ \. retryLimit] ?? 5 }
6969 set { self [ \. retryLimit] = newValue }
7070 }
7171
@@ -332,20 +332,20 @@ public struct RetryBackoffPolicy {
332332 let scopeHash : ( _ request: HTTPRequestComponents ) -> AnyHashable ?
333333
334334 /// Decide if the response must trigger a global backoff for the scope.
335- let isGlobalBackoff : ( _ request: HTTPRequestComponents , _ status : HTTPResponse . Status ) -> Bool
335+ let isGlobalBackoff : ( _ request: HTTPRequestComponents , _ response : HTTPResponse ) -> Bool
336336
337337 public init (
338338 scopeHash: @escaping ( _ request : HTTPRequestComponents ) -> AnyHashable ? ,
339- isGlobalBackoff: @escaping ( _ request: HTTPRequestComponents , _ status : HTTPResponse . Status ) -> Bool
339+ isGlobalBackoff: @escaping ( _ request: HTTPRequestComponents , _ response : HTTPResponse ) -> Bool
340340 ) {
341341 self . isGlobalBackoff = isGlobalBackoff
342342 self . scopeHash = scopeHash
343343 }
344344
345345 public static let `default` = RetryBackoffPolicy { req in
346346 req. urlComponents. host
347- } isGlobalBackoff: { _, status in
348- Set ( [ 429 , 503 ] ) . contains ( status. code)
347+ } isGlobalBackoff: { _, response in
348+ Set ( [ 429 , 503 ] ) . contains ( response . status. code)
349349 }
350350}
351351
@@ -383,7 +383,9 @@ private struct retryMiddleware: HTTPClientMiddleware {
383383 if count > 0 {
384384 let interval = UInt64 ( max ( retryAfterHeader, interval ( count - 1 , response) ) * 1_000_000_000 )
385385 if interval > 0 {
386- if let response, let hash = backoffPolicy. scopeHash ( request) , backoffPolicy. isGlobalBackoff ( request, response. status) {
386+ if let response, let hash = backoffPolicy. scopeHash ( request) , backoffPolicy. isGlobalBackoff ( request, response) {
387+ Logger ( label: " SwiftAPIClient " )
388+ . trace ( " Backing off requests to ' \( hash. base) ' for \( Double ( interval) / 1_000_000_000 ) seconds due to \( response. status) status code. " )
387389 _ = try await withThrowingSynchronizedAccess ( id: hash) {
388390 try await Task . sleep ( nanoseconds: interval)
389391 return interval
@@ -414,6 +416,8 @@ private struct retryMiddleware: HTTPClientMiddleware {
414416 retryAfterHeader = httpResponse. headerFields [ . retryAfter] . flatMap {
415417 decodeRetryAfterHeader ( $0, formatter: configs. retryAfterHeaderDateFormatter)
416418 } ?? 0
419+ } else {
420+ retryAfterHeader = 0
417421 }
418422 if !needRetry( nil ) {
419423 return ( data, httpResponse)
0 commit comments