Skip to content

Commit 304f171

Browse files
committed
Use Int64.random instead of .randomElement in HTTPConnectionPool.calculateBackoff
Motivation: On 32-bit systems, using .randomElement on a range larger than what can fit in Int32 causes a crash. After only 26 or 27 retries of a request using HTTPClient, the calculateBackoff method would run into this and crash consistently on an armv7 (32-bit) device. Modifications: A one-line fix to opt to using Int64.random on the same jitterRange instead of .randomElement, which works as expected without crashing on 32-bit systems. Result: The HTTPClient now works as expected and can perform as many retries as needed without crashing.
1 parent 20216df commit 304f171

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+Backoff.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extension HTTPConnectionPool {
6161
// Calculate a 3% jitter range
6262
let jitterRange = (backoff.nanoseconds / 100) * 3
6363
// Pick a random element from the range +/- jitter range.
64-
let jitter: TimeAmount = .nanoseconds((-jitterRange...jitterRange).randomElement()!)
64+
let jitter: TimeAmount = .nanoseconds(Int64.random(in: -jitterRange...jitterRange))
6565
let jitteredBackoff = backoff + jitter
6666
return jitteredBackoff
6767
}

0 commit comments

Comments
 (0)