How to verify HTTP/2 connection pooling/reuse in iOS?

Viewed 580

From WWDC videos, it is clearly mentioned if we use HTTP/2 on the server and if we are using URLSession the connection pooling works out of the box. How to verify this is working?

I am using URLSessionTaskMetrics to verify this, but when i see the metrics, it is using networkProtocolName is h2. so in the server HTTP/2 is already enabled, but the property isReusedConnection is FALSE

  1. In HTTP/2 the connections are reused to increase the performance, but any idea why isReusedConnection is false?
  2. Do i need to turn on any settings for connection pooling? or am I missing something?
  3. Is there any other way to verify connection pooling in iOS?
1 Answers

isReusedConnection can be true or false for both HTTP/1.1 of HTTP/2 protocols because it's depends on URLSession's alive connections.

HTTP/1.1

URLSession creates a connection pool of 4 connections to a single domain by default and use them to send requests e.g.:

var requestNum = 0

class Metrics : NSObject, URLSessionDataDelegate {
    func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
           for metric in metrics.transactionMetrics {
                print("\(requestNum). protocol: \(metric.networkProtocolName!), reused: \(metric.isReusedConnection)")
                requestNum += 1
           }
       }
}

let metrics = Metrics()
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: metrics, delegateQueue: nil)

func makeRequests(_ http: String) {
    requestNum = 0
    for i in 0...10 {
        let request = URLRequest(url: URL(string: "https://\(http).akamai.com/demo/tile-\(i).png")!)
        let task = session.dataTask(with: request)
        task.resume()
    }
}

makeRequests("http1")

Outputs:

0. protocol: http/1.1, reused: false
1. protocol: http/1.1, reused: true
2. protocol: http/1.1, reused: true
3. protocol: http/1.1, reused: false
4. protocol: http/1.1, reused: false
5. protocol: http/1.1, reused: false
6. protocol: http/1.1, reused: true
7. protocol: http/1.1, reused: true
8. protocol: http/1.1, reused: true
9. protocol: http/1.1, reused: true
10. protocol: http/1.1, reused: true

As you can see there are 4 new connections (reused: false) are created for the session and all next requests reuse them later.

HTTP/2

URLSession works the same as for HTTP/1.1 but creates a single connection to a domain and sends all requests through it e.g.:

makeRequests("http2")

Output:

0. protocol: h2, reused: false
1. protocol: h2, reused: true
2. protocol: h2, reused: true
3. protocol: h2, reused: true
4. protocol: h2, reused: true
5. protocol: h2, reused: true
6. protocol: h2, reused: true
7. protocol: h2, reused: true
8. protocol: h2, reused: true
9. protocol: h2, reused: true
10. protocol: h2, reused: true

New connection was created for the first request only from above and next requests just reuse it later.

Connection lifetime

URLSession is designed for typically client applications and doesn't hold connections to a server all the time. Connections are alive about 20 secs and then the session closes them so it creates new connections each time for not frequent requests.

Related