Skip to content

Don't crash when hitting long backoffs. #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -46,9 +46,12 @@ extension HTTPConnectionPool {
// - 29 failed attempts: ~60s (max out)

let start = Double(TimeAmount.milliseconds(100).nanoseconds)
let backoffNanoseconds = Int64(start * pow(1.25, Double(attempts - 1)))
let backoffNanosecondsDouble = start * pow(1.25, Double(attempts - 1))

let backoff: TimeAmount = min(.nanoseconds(backoffNanoseconds), .seconds(60))
// Cap to 60s _before_ we convert to Int64, to avoid trapping in the Int64 initializer.
let backoffNanoseconds = Int64(min(backoffNanosecondsDouble, Double(TimeAmount.seconds(60).nanoseconds)))

let backoff = TimeAmount.nanoseconds(backoffNanoseconds)

// Calculate a 3% jitter range
let jitterRange = (backoff.nanoseconds / 100) * 3
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ extension HTTPConnectionPoolTests {
("testConnectionCreationIsRetriedUntilRequestIsCancelled", testConnectionCreationIsRetriedUntilRequestIsCancelled),
("testConnectionShutdownIsCalledOnActiveConnections", testConnectionShutdownIsCalledOnActiveConnections),
("testConnectionPoolStressResistanceHTTP1", testConnectionPoolStressResistanceHTTP1),
("testBackoffBehavesSensibly", testBackoffBehavesSensibly),
]
}
}
26 changes: 26 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPConnectionPoolTests.swift
Original file line number Diff line number Diff line change
@@ -453,6 +453,32 @@ class HTTPConnectionPoolTests: XCTestCase {
pool.shutdown()
XCTAssertNoThrow(try poolDelegate.future.wait())
}

func testBackoffBehavesSensibly() throws {
var backoff = HTTPConnectionPool.calculateBackoff(failedAttempt: 1)

// The value should be 100ms±3ms
XCTAssertLessThanOrEqual((backoff - .milliseconds(100)).nanoseconds.magnitude, TimeAmount.milliseconds(3).nanoseconds.magnitude)

// Should always increase
// We stop when we get within the jitter of 60s, which is 1.8s
var attempt = 1
while backoff < (.seconds(60) - .milliseconds(1800)) {
attempt += 1
let newBackoff = HTTPConnectionPool.calculateBackoff(failedAttempt: attempt)

XCTAssertGreaterThan(newBackoff, backoff)
backoff = newBackoff
}

// Ok, now we should be able to do a hundred increments, and always hit 60s, plus or minus 1.8s of jitter.
for offset in 0..<100 {
XCTAssertLessThanOrEqual(
(HTTPConnectionPool.calculateBackoff(failedAttempt: attempt + offset) - .seconds(60)).nanoseconds.magnitude,
TimeAmount.milliseconds(1800).nanoseconds.magnitude
)
}
}
}

class TestDelegate: HTTPConnectionPoolDelegate {