Valentin Shamardin
Valentin Shamardin

Reputation: 3658

URLRequest fails before timeout

I want to set different timeouts for different requests. My request routine looks like:

var request = URLRequest(url: url,
                         cachePolicy: .reloadIgnoringLocalCacheData,
                         timeoutInterval: timeout)
// setting headers and body...
sessionTask = localURLSession.dataTask(with: request)
sessionTask?.resume()

where localURLSession is defined as public var:

public var localURLSession: Foundation.URLSession {
    return Foundation.URLSession(configuration: localConfig, delegate: self, delegateQueue: nil)
}

public var localConfig: URLSessionConfiguration {
    let res = URLSessionConfiguration.default
    res.timeoutIntervalForRequest = Self.ordinaryRequestsTimeout // 20 seconds
    return res
}

Then I have 2 problems:

  1. When I make 2 simultaneous requests with 100% loss Network Link Conditioner (first with 20 seconds timeout and second – with 40 seconds), both requests fails after 8 seconds. I don't understand why.
  2. When I make one request for the first time with 100% loss Network Link Conditioner, it fails in timeout like expected, but retrying this request fails in 1 second. I want to wait all the timeout every time.

Upvotes: 2

Views: 214

Answers (1)

dgatwood
dgatwood

Reputation: 10407

In all likelihood, for the 8-second failure, the DNS request is timing out, so you aren't connecting at all.

For the 1-second failure, the OS has probably concluded that the host is unreachable, and won't even try again until the network changes or it successfully makes at least one request to some host somewhere (negative DNS caching).

That said, without a packet trace, I can't be certain of either of those statements.

Upvotes: 1

Related Questions