Skip to content

Commit b672f8e

Browse files
committed
hm
1 parent 9c52525 commit b672f8e

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

Sources/SwiftAPIClient/Modifiers/RateLimitModifier.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public extension APIClient {
1010
/// - statusCodes: The set of status codes that indicate a rate limit exceeded. Default to `[429]`.
1111
/// - methods: The set of HTTP methods to retry. If `nil`, all methods are retried. Default to `nil`.
1212
/// - maxRepeatCount: The maximum number of times the request can be repeated. Default to 3.
13+
@available(*, deprecated, message: "Use retry() instead")
1314
func waitIfRateLimitExceeded<ID: Hashable>(
1415
id: @escaping (HTTPRequestComponents) -> ID,
1516
interval: TimeInterval = 30,
@@ -26,6 +27,7 @@ public extension APIClient {
2627
/// - statusCodes: The set of status codes that indicate a rate limit exceeded. Default to `[429]`.
2728
/// - methods: The set of HTTP methods to retry. If `nil`, all methods are retried. Default to `nil`.
2829
/// - maxRepeatCount: The maximum number of times the request can be repeated. Default to 3.
30+
@available(*, deprecated, message: "Use retry() instead")
2931
func waitIfRateLimitExceeded(
3032
interval: TimeInterval = 30,
3133
statusCodes: Set<HTTPResponse.Status> = [.tooManyRequests],

Sources/SwiftAPIClient/Modifiers/RetryModifier.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)