IOS and Go - Keep-Alive using NSURLSession

In my iOS application, I have a search function that retrieves results from the server. Search updates are updated as the user updates his query, so this leads to several queries being executed sequentially.

So my question is, how can I guarantee that TCP keep-alive is used in these connections? I would like to reduce the latency as much as possible, so it is important that the connection is maintained after the first request and reused for the following requests.

I use NSURLSession and I heard that it uses keep-alive by default, but how can I know for sure? Making requests on the server does not show the difference between each subsequent request, but I did not expect to see any changes only from the header information.

Any help here? I use Go on my server, so it is possible that it also needs additional configuration on this side.

+8
ios web-services go nsurlsession
source share
2 answers

I believe that you confuse TCP keep-alive with HTTP support (persistent connections). These are not related concepts. From your question, you probably mean HTTP persistent connections.

In HTTP / 1.1, persistent connections are standard and are used by NSURLSession and almost every HTTP / 1.1 client. You must ask them to turn off. You can check Connection: close in the HTTP header or on the server side, you can check the Close field in http.Request . But I'm sure you get persistent connections. This means that you do not need to re-examine the TLS tunnel (or at least the tripartite TCP handshake) for each request. (Although if you are doing concurrent requests, there will still be several connections that you need to negotiate. HTTP / 1.1 can only handle one thing at a time, and NSURLSession will try to use the connection pool to improve response time.)

TCP keep-alives is a completely different thing. It sends a periodic ping to the other side to make sure it is still available. There are many ways to lose a network connection and not know it until the next time you try to communicate, and the normal symptom is that the connection just hangs and you need its time. Theoretically, TCP keep-alive is just a tool to detect this, but I almost never found it practical. Difficult to set up correctly (especially in Cocoa). You will almost always need to create higher-level ping functionality for your application, rather than relying on it.

But leading it to your problem, HTTP / 1.1 is probably right for you as it is, but you will need to carefully manage your responses. If you make a new request for each letter and send a massive response, then this will work poorly. You will keep all of your connection pools busy by loading things that you are about to throw away. First you need to focus on good algorithms. At a minimum, you probably only want to submit multiple results at a time and provide a “swap” approach in your API to get more results for the same search.

+6
source share

To know exactly what TCP does, you can enable CFNETWORK_DIAGNOSTICS . From the docs:

During normal development, you can set this environment variable through the Xcode schema editor. If you need to investigate problems outside of Xcode, you can install it programmatically using the code shown in Listing 1.

Listing 1 Programmatically enabling the CFNetwork diagnostic log

 setenv("CFNETWORK_DIAGNOSTICS", "3", 1); 

You must do this correctly at the beginning of the application startup sequence. Normally putting this at the beginning of main is enough, but if you have C ++ static initializers that use CFNetwork, you will have to run them in front of them.

CFNetwork diagnostic logs are triggered the first time you use CFNetwork — or any framework such as Foundation that uses CFNetwork — at this point it will print a message like the one shown in Listing 2.

Listing 2 Example CFNetwork diagnostic log start message

 2014-10-28 14:23:37.115 QTestbed[2626:60b] CFNetwork diagnostics log file created at: /private/var/mobile/Applications/76531F40-3291-4565-8C75-0438052C83BC/Library/Logs/CrashReporter/CFNetwork_com.example.apple-samplecode.QTestbed_2626.nwlrb 
+6
source share

All Articles