@@ -72,7 +72,7 @@ public extension APIClient.Configs {
7272 /// The interval between retries. It can be a fixed time interval or a closure that takes the current retry count and returns a time interval.
7373 /// - Note: This configuration works only if you use the `retry()` modifier.
7474 var retryInterval : ( Int ) -> TimeInterval {
75- get { self [ \. retryInterval] ?? { _ in 0 } }
75+ get { self [ \. retryInterval] ?? { _ in 1. 0 } }
7676 set { self [ \. retryInterval] = newValue }
7777 }
7878
@@ -310,20 +310,20 @@ public struct RetryBackoffPolicy {
310310 let scopeHash : ( _ request: HTTPRequestComponents ) -> AnyHashable ?
311311
312312 /// Decide if the response must trigger a global backoff for the scope.
313- let isGlobalBackoff : ( _ request: HTTPRequestComponents , _ response : HTTPResponse ) -> Bool
313+ let isGlobalBackoff : ( _ request: HTTPRequestComponents , _ status : HTTPResponse . Status ) -> Bool
314314
315315 public init (
316316 scopeHash: @escaping ( _ request : HTTPRequestComponents ) -> AnyHashable ? ,
317- isGlobalBackoff: @escaping ( _ request: HTTPRequestComponents , _ response : HTTPResponse ) -> Bool
317+ isGlobalBackoff: @escaping ( _ request: HTTPRequestComponents , _ status : HTTPResponse . Status ) -> Bool
318318 ) {
319319 self . isGlobalBackoff = isGlobalBackoff
320320 self . scopeHash = scopeHash
321321 }
322322
323323 public static let `default` = RetryBackoffPolicy { req in
324324 req. urlComponents. host
325- } isGlobalBackoff: { _, resp in
326- Set ( [ 429 , 503 ] ) . contains ( resp . status. code)
325+ } isGlobalBackoff: { _, status in
326+ Set ( [ 429 , 503 ] ) . contains ( status. code)
327327 }
328328}
329329
@@ -344,7 +344,7 @@ private struct retryMiddleware: HTTPClientMiddleware {
344344 }
345345 }
346346 var count = 0
347- var resp : HTTPResponse ?
347+ var status : HTTPResponse . Status ?
348348 var retryAfterHeader : TimeInterval = 0
349349
350350 func needRetry( _ result: Result < HTTPResponse , Error > ) -> Bool {
@@ -361,7 +361,7 @@ private struct retryMiddleware: HTTPClientMiddleware {
361361 if count > 0 {
362362 let interval = UInt64 ( max ( retryAfterHeader, interval ( count - 1 ) ) * 1_000_000_000 )
363363 if interval > 0 {
364- if let resp , let hash = backoffPolicy. scopeHash ( request) , backoffPolicy. isGlobalBackoff ( request, resp ) {
364+ if let status , let hash = backoffPolicy. scopeHash ( request) , backoffPolicy. isGlobalBackoff ( request, status ) {
365365 _ = try await withThrowingSynchronizedAccess ( id: hash) {
366366 try await Task . sleep ( nanoseconds: interval)
367367 return interval
@@ -372,15 +372,17 @@ private struct retryMiddleware: HTTPClientMiddleware {
372372 }
373373 }
374374 count += 1
375-
376- return try await next ( request, configs)
375+ let ( result, sts) = try await extractStatusCodeEvenFailed {
376+ try await next ( request, configs)
377+ }
378+ status = sts
379+ return try result. get ( )
377380 }
378381
379382 while true {
380383 do {
381384 let ( data, response) = try await retry ( )
382- resp = response
383- if [ 429 , 503 ] . contains ( response. status. code) {
385+ if [ 429 , 503 ] . contains ( ( status ?? response. status) . code) {
384386 retryAfterHeader = response. headerFields [ . retryAfter] . flatMap {
385387 decodeRetryAfterHeader ( $0, formatter: configs. retryAfterHeaderDateFormatter)
386388 } ?? 0
0 commit comments