iosnsurlsessionnsurlsessiondownloadtasknsurlsessionconfiguration

NSURLSession HTTPMaximumConnectionsPerHost not working as expected


I am trying to download .ts files of a .m3u8 Video. I have created a download task for each of the .ts url and the session configuration HTTPMaximumConnectionsPerHost property set to 4:

NSURLSessionConfiguration *sessionConfig    = [NSURLSessionConfiguration defaultSessionConfiguration];
  sessionConfig.HTTPMaximumConnectionsPerHost = 4;
_session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];

Expected behavior: Only 4 ts should download concurrently and once any one of these download finishes, the next download item will be put in the queue such that at any time maximum 4 ts are downloading.

Actual behavior: Around 50 or more ts are downloading concurrently ignoring the HTTPMaximumConnectionsPerHost property.

Screenshot of Charles Timeline display the multiple .ts request happening concurrently.enter image description here

When I am trying to download images using NSURLSession by specifying the HTTPMaximumConnectionsPerHost to 3, I can see that only 3 downloads are happening at a time.enter image description here

To download m3u8, I can also use the AVAssetDownloadURLSession instead of NSURLSession, which is downloading only 1 .ts at a time.enter image description here

I am trying to find out:

1) Why HTTPMaximumConnectionsPerHost property is working properly for the image downloads, while not working for .ts downloads as a result of which more than 4 .ts download are happening concurrently.

2) Is there a way to increase the maximum concurrent .ts downloads to 4, using the AVAssetURLDownloadSession, which is downloading only 1 .ts


Solution

  • Unless you've discovered a bug in the API, this probably indicates that you are sending the .ts requests in multiple sessions and/or in some session other than the one you have configured. Sessions don't know about requests in other sessions; the maximum is per-session.