如何解决HTTP持久连接错误导致的超时问题? [英] How to solve timeout issues caused by bad HTTP persistent connection?

查看:308
本文介绍了如何解决HTTP持久连接错误导致的超时问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在努力解决HTTP超时问题。经过一个多月的调查,我很确定它是由错误的HTTP持久连接引起的。详情如下:

I've been struggling with an HTTP timeout issue recently. After more than one month of investigation I'm quite sure that it is caused by bad HTTP persistent connections. Details are as follows:


  1. 这是一个iOS应用程序。

  2. 大多数用户正在运行iOS 8 。

  3. 我正在使用 NSURLConnection

  4. iOS 8有一个已知保持活力错误但我的是一个不同的问题。更具体地说,该bug导致 NSURLErrorNetworkConnectionLost 但我的错误是 NSURLErrorTimedOut 。但是,我不确定我的问题是否是由iOS 8的另一个错误造成的。

  5. 我的问题的行为:经过一段时间的使用后 - 在成功发送一些HTTP请求之后收到相应的响应 - 一个请求将导致 NSURLErrorTimedOut ,并且以下所有内容(距离最后一个请求不再重复使用持久连接)请求将导致 NSURLErrorTimedOut

  6. 一些有效的解决方法:

  1. It is an iOS app.
  2. Most users are running iOS 8.
  3. I'm using NSURLConnection.
  4. iOS 8 has one known keep alive bug but mine is a different issue. More specifically, that bug causes NSURLErrorNetworkConnectionLost but my error is NSURLErrorTimedOut. However, I'm not sure whether my issue is caused by another bug of iOS 8.
  5. The behavior of my issue: After some time of using — after some HTTP requests are successfully sent and corresponding responses received — one request would cause NSURLErrorTimedOut, and all following(not too far away from the last one in time to reuse the persistent connection) requests would causes NSURLErrorTimedOut.
  6. Some working workaround:

  1. 杀死并重新启动应用程序。

  2. 在iPhone上关闭WiFi连接以强制使用3G / 4G。

  3. 打开空气模式然后将其关闭。


  • 我的分析:从行为来看,这个问题似乎是由一个糟糕的持续连接引起的。所有后续请求都继续使用此持久连接,因此所有请求都以 NSURLErrorTimedOut 失败。从解决方法中我们可以看到所有这些都有效,因为它们会导致删除坏的持久连接并创建新的持久连接。

  • My analysis: From the behavior the issue seems to be caused by a gone-bad persistent connection. All the subsequent requests keep using this persistent connection so all fail with NSURLErrorTimedOut. From the workaround we can see all of them work because they cause the bad persistent connection to be dropped and a new persistent connection to be created.
  • 我的问题:


    1. 还有其他人遇到过这个问题吗?

    2. 这是一个已知的bug iOS 8?

    3. 是否是由服务器的一些非常规配置引起的?我不控制服务器,但我知道他们使用的是nginx 1.6.1,他们的工程师正在和我一起调查这个问题。我应该要求他们提供哪些信息?

    4. 有没有办法强制 NSURLConnection 不重用当前的持久连接但是要创建一个在我的代码中检测到它后,我可以解决这个问题吗?

    1. Have anyone else encountered this issue?
    2. Is it a known bug of iOS 8?
    3. Is it caused by some unconventional configuration of the servers? I don't control the servers but I know they use nginx 1.6.1 and their engineers are working with me in investigating this issue. What information should I ask them for?
    4. Is there any way to force NSURLConnection to not reuse the current persistent connection but to create a new one so I can work around this issue after I detect it in my code?






    更新:



    我通过使用 CFNetwork 并控制成功地在iOS 8上缓解了这个问题直接连接标题。然而,在iOS 9上似乎问题变得更糟。


    Update:

    I successfully mitigated this issue on iOS 8 by using CFNetwork and controlling Connection header directly. However it seems the issue becomes worse on iOS 9.

    由于我希望Apple能够在iOS 9上修复它,我终于发射了雷达: http://www.openradar.me/22770738

    Since my hope that Apple would fix it on iOS 9 is broken I finally fired a radar: http://www.openradar.me/22770738.

    如果如果你有一个更可靠的可重复样本,你也会遇到这个问题,请复制我的雷达,甚至更好,发射你自己的雷达。

    If you also encounter this issue please duplicate my radar, or even better, fire your own radar if you have a more reliably reproducible sample.

    推荐答案

    经过2周的研究,我可以回答问题3和4:

    After 2 weeks of research, I can give answers to question 3 and 4:


    1. nginx 在服务器上将持久连接超时设置为5秒,这不应该是原因。服务器工程师发现这些超时请求实际上是正常接收和响应的。所以它更可能是客户端问题。由于我有一个最小的可重现代码来排除我的代码作为原因,因此原因应该在iOS中。

    2. 我找到的唯一方法是使用 CFNetwork 。更高级别的API,例如 NSURLConnection NSURLSession 连接标题将被系统覆盖。

    1. nginx's persistent connection timeout is set to 5s on server, which should not be the cause. Server engineers found those timed-out requests are actually normally received and responded. So it is more likely a client side issue. Since I have a minimal reproducible code to rule out my code as the cause, the cause should be in iOS.
    2. The only way I found is to use CFNetwork. Higher level API such as NSURLConnection or NSURLSession's Connection header will be overwritten by system.

    这篇关于如何解决HTTP持久连接错误导致的超时问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆