Swift网络呼叫:延迟不断增加,最后请求超时

时间:2022-10-14 20:41:41

I am working on a Swift project which requires a lot of consumption of APIs. Everything is working fine but sometimes, I get Code=-1001 "The request timed out." error while calling the API. To test it properly, I am using loop and iterating it for 1000 times, calling the same API.

我正在开发一个需要大量使用API​​的Swift项目。一切正常,但有时,我得到Code = -1001“请求超时。”调用API时出错。为了正确测试它,我使用循环并迭代1000次,调用相同的API。

I am using public API. The API is

我正在使用公共API。 API是

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

and my loop is

我的循环是

for i in 0 ..< 1000 {
    Alamofire.request(.POST, apiString).responseJSON { response in
        print(response.debugDescription)

        if response.result.isFailure {
            print("Failure")
        }
    }
}

Now while checking print responses, I see Latency is increased on every new api response and goes to 60.14 secs and finally the request timeouts. First time while calling API, the latency was 2.225 secs and the in timeline in logs, the latency of last successful API was 60.14 secs.

现在,在检查打印响应时,我看到每个新的api响应都会增加Latency,并且会延迟到60.14秒,最后是请求超时。第一次调用API时,延迟为2.225秒,日志中的时间线,上次成功API的延迟为60.14秒。

Timeline of API when it is being hit for the first time in the loop (when i = 1).

API在循环中第一次被击中时的时间线(当i = 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

上次成功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 I execute it synchronously, there is no case of such timeouts. The API is called 1000 times and there is no case of Failure.

但是如果我同步执行它,就不会出现这种超时的情况。 API被调用1000次,并且没有失败的情况。

I don't understand why latency is increasing in such a way when I hit the API multiple time asynchronously. I know the error can be avoided by increasing timeout time. But I want to know the reason why it is happening at first place.

我不明白为什么当我多次异步访问API时,延迟会以这种方式增加。我知道可以通过增加超时时间来避免错误。但我想知道它首先发生的原因。

Any help would be appreciated.

任何帮助,将不胜感激。

1 个解决方案

#1


1  

The networking stack will only allow a certain number of requests to execute simultaneously, and will block the issuance of new requests until some have completed. So, your 1000 requests are being started as soon as possible but the ones in the back need to wait for the ones in the front to finish before they can actually connect, hence the increasing amount of time.

网络堆栈将只允许一定数量的请求同时执行,并将阻止发出新请求,直到某些请求完成。因此,您的1000个请求将尽快启动,但后面的请求需要等待前面的请求才能实际连接,因此时间越来越长。

See this thread for some details on how to deal with that situation: NSURLSession concurrent requests with Alamofire

有关如何处理这种情况的一些详细信息,请参阅此主题:NSURLSession与Alamofire的并发请求

#1


1  

The networking stack will only allow a certain number of requests to execute simultaneously, and will block the issuance of new requests until some have completed. So, your 1000 requests are being started as soon as possible but the ones in the back need to wait for the ones in the front to finish before they can actually connect, hence the increasing amount of time.

网络堆栈将只允许一定数量的请求同时执行,并将阻止发出新请求,直到某些请求完成。因此,您的1000个请求将尽快启动,但后面的请求需要等待前面的请求才能实际连接,因此时间越来越长。

See this thread for some details on how to deal with that situation: NSURLSession concurrent requests with Alamofire

有关如何处理这种情况的一些详细信息,请参阅此主题:NSURLSession与Alamofire的并发请求