iOS doesn't to use http keep-alive. Very high

2019-09-10 06:34发布

问题:

I wanted to test latency for my server, so I sent a rest request. Using a rest api client, i got around 500 ms latency from desktop, but when i do the same request on iOS Simulator (using AFNetworking) , I am getting latency of 1.8 seconds.

My server sends keep alive headers, and I noticed that the first request from my PC is of a similar latency as IOS. How do I make iOS honour keep alive , or reduce latency ? Since I am making a realtime app , it is a must that I have low latency. From the same computer , my rest client is about 3x faster than iOS. How may I fix this issue ?

Edit:: I noticed that upon disabling https support from my server, the latency becomes 1 second. To me it is quite obvious that there is some problem with iOS not using keep alive.

I monitored network activity using wireshark. Below is a picture of two http requests made at around 1 second intervals:

It can be seen that encrypted handshake is being done twice.

Using my rest client, this is it what the same http requests at 2 second interval looks like:

回答1:

Without seeing any code, it's hard to say for sure what's happening, but make sure:

  • The requests are in the same session. Each session uses separate underlying connections, so if the session goes away, so do the connections.
  • The requests use the same authentication credentials (if applicable).

Additionally, try enabling HTTPShouldUsePipelining. If there are multiple requests in flight for the same server, the connection won't be closed until (at least) the last one arrives, so that might sometimes help even when the OS is aggressively managing power.