NSURLSession HTTPMaximumConnectionsPerHost 未按预期工作

NSURLSession HTTPMaximumConnectionsPerHost not working as expected

我正在尝试下载 .m3u8 视频的 .ts 文件。我为每个 .ts url 创建了一个下载任务,会话配置 HTTPMaximumConnectionsPerHost 属性 设置为 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.

Charles Timeline 的屏幕截图显示同时发生的多个 .ts 请求。

当我尝试通过将 HTTPMaximumConnectionsPerHost 指定为 3 来使用 NSURLSession 下载图像时,我可以看到一次只有 3 次下载。

要下载 m3u8,我也可以使用 AVAssetDownloadURLSession 而不是 NSURLSession,它一次只下载 1 个 .ts。

我想知道:

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

除非您在 API 中发现了错误,这可能表明您在多个会话 and/or 中发送 .ts 请求,而不是在您配置的会话中。会话不知道其他会话中的请求;最大值是每个会话。