async-http-client icon indicating copy to clipboard operation
async-http-client copied to clipboard

HTTPClient should fail early if the internet connection is offline or disconnects

Open gregcotten opened this issue 8 months ago • 3 comments

Another big bump in a transition from URLSession to HTTPClient:

HTTPClient has no way of knowing the internet is offline or is disconnected. An executed request that hasn't connected will just result in a connection timeout, and a connected request that becomes disconnected during read/write will result in a read/write timeout, which with the shared singleton configuration is 90 seconds (!!!).

As a comparison, URLSession's default configuration causes a request to fail immediately with NSURLErrorNotConnectedToInternet unless waitsForConnectivity is set to true, in which case it then waits the full timeoutIntervalForRequest before failing, equivalent to AHC's Timeout.connect. No matter what waitsForConnectivity is set to, a disconnection from the internet during a connected request's read/write results in an immediate failure with NSURLErrorNetworkConnectionLost

I think URLSession's default behavior is quite sensible, and it would be great to offer an equivalent in AHC.

gregcotten avatar Apr 09 '25 19:04 gregcotten

Does setting https://github.com/swift-server/async-http-client/blob/2064247c2b4a924bb199d5b1eb5adff81a8f6318/Sources/AsyncHTTPClient/HTTPClient.swift#L832 to false help?

Lukasa avatar Apr 10 '25 09:04 Lukasa

Does setting

async-http-client/Sources/AsyncHTTPClient/HTTPClient.swift

Line 832 in 2064247

public var networkFrameworkWaitForConnectivity: Bool to false help?

Doesn't appear to, though I do get a different error after the full connection timeout period lapses: POSIXErrorCode(rawValue: 50): Network is down

In any case we'd definitely want this fast fail behavior to work on every platform, not just ones that can use Network.framework.

I just tested with URLSession on Linux (Swift 6.1 Release) with a disabled internet connection and I get a slightly different (but just as quick to fail) error: NSURLErrorCannotConnectToHost

gregcotten avatar Apr 10 '25 17:04 gregcotten

Yeah, I think what we really want here is an extra feature alongside the connect timeout suggesting how many times we should try to reconnect.

Lukasa avatar Apr 11 '25 15:04 Lukasa