NSURLSession HTTPMaximumConnectionsPerHost not working as expected
Asked Answered
D

1

6

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

Disremember answered 31/7, 2018 at 7:5 Comment(0)
R
3

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.

Rafaelof answered 6/8, 2018 at 18:1 Comment(2)
After some analysis, I observed that m3u8 file that I am trying to download is hosted on HTTP/2 server which will only have 1 connection and all requests multiplexed on this connection. Hence, httpmaximumconcurrentdownload is not relevant in context of HTTP/2 as the value will be always 1. I am still trying to figure out how to limit the number of requests per connection in HTTP2 in iOS, because the server that I have to use supports only 150 requests/second and if I am trying to download multiple m3u8 files, my requests are failing.Disremember
Ah. That makes sense, kind of; HTTP/2 basically enables pipelining automatically, so if you make 200 requests, they all get sent immediately, on the same connection. It sounds like you need to limit the rate at which you create the tasks themselves. Basically, keep an NSSet of all outstanding tasks that have been issued but not received, and when that exceeds some preset threshold, put any new requests into a queue and defer the new task creation until an existing task returns data.Rafaelof

© 2022 - 2024 — McMap. All rights reserved.