Maximum number of concurrent NSURLConnections to t

2019-02-16 19:23发布

问题:

I'm running in to an issue in an OS X app that creates multiple, persistent connections to the same host using NSURLConnection. I create a separate connection for different rooms, and it stays connected the entire time the room is open to consume a streaming API. When opening many rooms, it stops working correctly.

I created a separate sample app that creates 10 connections, and it seems to only allow 6 connections to work, and the others are queued. Does anyone know if there is a way to override this limit? I can't find it documented anywhere. The only workaround I've found is it seems to be per host name, so testing with "localhost" and "127.0.0.1" allows 6 connections per host. I uploaded a sample project with client and server here - http://cl.ly/1x3K0D1F072V3U2T0C0I.

回答1:

I filed a Radar for something that seems like the same issue but on iOS. I found that you can't have more than 5 connections open at once. The connections don't have to be pointing to the same domain. Anything after that would be queued. So if you have 5 connections open to an extremely slow endpoint, any other connections will not go through.

Radar: http://openradar.appspot.com/radar?id=2542401

Apple's reply:

This is the effect of our NSURLConnection connection cache. It is expected. We expect to address this type of configuration with new API.

I asked if they could give me anymore information (does it vary? does the type of connection affect it?) and they said:

Unfortunately, we can't give details about the connection limit behavior.

User agents in general (Chrome, Firefox, Safari) use six simultaneous TCP connections per hostname, with potential one-offs.



回答2:

You could break this limitation by using CFNetwork API (CFHTTPMessage).

Here is the CFNetwork Programming Guide. https://developer.apple.com/library/mac/documentation/Networking/Conceptual/CFNetwork/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001132

BTW, if you decide to use CFNetwork, you'll need to work around the proxy and authenticate.

Wish this could helped!