ios - Swift Network Calling: Latency keeps increasing and finally request timeout -


i working on swift project requires lot of consumption of apis. working fine sometimes, code=-1001 "the request timed out." error while calling api. test properly, using loop , iterating 1000 times, calling same api.

i using public api. api

let apistring = "https://oc-index.library.ubc.ca" 

and loop is

for in 0 ..< 1000 {     alamofire.request(.post, apistring).responsejson { response in         print(response.debugdescription)          if response.result.isfailure {             print("failure")         }     } } 

now while checking print responses, see latency increased on every new api response , goes 60.14 secs , request timeouts. first time while calling api, latency 2.225 secs , in timeline in logs, latency of last successful api 60.14 secs.

timeline of api when being hit first time in loop (when = 1).

[timeline]: timeline: { "request start time": 493671593.424, "initial response time": 493671595.649, "request completed time": 493671595.684, "serialization completed time": 493671595.688, "latency": 2.225 secs, "request duration": 2.260 secs, "serialization duration": 0.004 secs, "total duration": 2.264 secs } 

timeline of last successful api

[timeline]: timeline: { "request start time": 493671593.859, "initial response time": 493671621.500, "request completed time": 493671621.513, "serialization completed time": 493671621.519, "latency": 60.140 secs, "request duration": 60.140 secs, "serialization duration": 0.007 secs, "total duration": 60.147 secs } 

but if execute synchronously, there no case of such timeouts. api called 1000 times , there no case of failure.

i don't understand why latency increasing in such way when hit api multiple time asynchronously. know error can avoided increasing timeout time. want know reason why happening @ first place.

any appreciated.

the networking stack allow number of requests execute simultaneously, , block issuance of new requests until have completed. so, 1000 requests being started possible ones in need wait ones in front finish before can connect, hence increasing amount of time.

see thread details on how deal situation: nsurlsession concurrent requests alamofire


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -