Skip to content

Commit

Permalink
added jitter to retry after
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaysubra committed Apr 9, 2024
1 parent 04e37c2 commit e757f66
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Sources/KlaviyoSwift/APIRequestErrorHandling.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ enum InvalidField: Equatable {
}
}

private func getDelaySeconds(for count: Int) -> Int {
let delay = Int(pow(2.0, Double(count)))
private func addJitter(to value: Int) -> Int {
let jitter = environment.randomInt()
return min(delay + jitter, ErrorHandlingConstants.maxBackoff)
return value + jitter
}

private func parseError(_ data: Data) -> [InvalidField]? {
Expand Down Expand Up @@ -112,7 +111,9 @@ func handleRequestError(
case let .retryWithBackoff(requestCount, totalCount, _):
requestRetryCount = requestCount + 1
totalRetryCount = totalCount + 1
nextBackoff = retryAfter ?? getDelaySeconds(for: totalRetryCount)
let exponentialBackOff = Int(pow(2.0, Double(totalRetryCount)))

nextBackoff = addJitter(to: retryAfter ?? exponentialBackOff)
}
return .requestFailed(
request, .retryWithBackoff(
Expand Down

0 comments on commit e757f66

Please sign in to comment.