向同一资源发出多个请求时,Chrome 会停顿吗? [英] Chrome stalls when making multiple requests to same resource?

查看:35
本文介绍了向同一资源发出多个请求时,Chrome 会停顿吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试实现长轮询,并且我正在使用 XMLHttpRequest 对象来完成它.到目前为止,我已经成功地在 Firefox 和 Internet Explorer 11 中获取事件,但奇怪的是,这次 Chrome 出现了奇怪的事件.

I'm trying to implement long polling for the first time, and I'm using XMLHttpRequest objects to do it. So far, I've been successful at getting events in Firefox and Internet Explorer 11, but Chrome strangely is the odd one out this time.

我可以加载一页并且运行得很好.它立即发出请求并开始处理和显示事件.如果我在第二个选项卡中打开页面,其中一个页面开始看到接收事件的延迟.在开发工具窗口中,我看到多个具有这种时序的请求:

I can load one page and it runs just fine. It makes the request right away and starts processing and displaying events. If I open the page in a second tab, one of the pages starts seeing delays in receiving events. In the dev tools window, I see multiple requests with this kind of timing:

停顿"的范围最长可达 20 秒.它不会发生在每个请求上,但通常会发生在连续的多个请求中,并且在一个标签中.

"Stalled" will range up to 20 seconds. It won't happen on every request, but will usually happen on several requests in a row, and in one tab.

起初我以为这是我的服务器的问题,但后来我打开了两个 IE 选项卡和两个 Firefox 选项卡,它们都连接并接收相同的事件而没有停顿.只有 Chrome 有这种问题.

At first I thought this was an issue with my server, but then I opened two IE tabs and two Firefox tabs, and they all connect and receive the same events without stalling. Only Chrome is having this kind of trouble.

我认为这可能是我提出或提供请求的方式的问题.作为参考,请求标头如下所示:

I figure this is likely an issue with the way in which I'm making or serving up the request. For reference, the request headers look like this:

Connection: keep-alive
Last-Event-Id: 530
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

响应如下:

HTTP/1.1 200 OK
Cache-Control: no-cache
Transfer-Encoding: chunked
Content-Type: text/event-stream
Expires: Tue, 16 Dec 2014 21:00:40 GMT
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 16 Dec 2014 21:00:40 GMT
Connection: close

尽管涉及到头文件,但我没有使用浏览器的原生 EventSource,而是使用一个 polyfill,它可以让我设置额外的头文件.polyfill 在幕后使用 XMLHttpRequest,但在我看来,无论请求是如何发出的,它都不应该停止 20 秒.

In spite of the headers involved, I'm not using the browser's native EventSource, but rather a polyfill that lets me set additional headers. The polyfill is using XMLHttpRequest under the covers, but it seems to me that no matter how the request is being made, it shouldn't stall for 20 seconds.

什么可能导致 Chrome 像这样停止运行?

What might be causing Chrome to stall like this?

Chrome 的 chrome://net-internals/#events 页面显示存在超时错误:

Chrome's chrome://net-internals/#events page shows that there's a timeout error involved:

t=33627 [st=    5]      HTTP_CACHE_ADD_TO_ENTRY  [dt=20001]
                --> net_error = -409 (ERR_CACHE_LOCK_TIMEOUT)

错误消息是指六个月前添加到 Chrome 的补丁 (https://codereview.chromium.org/345643003),当多次请求同一资源时,它会实现 20 秒的超时.事实上,补丁试图修复的错误之一(错误编号 46104) 指的是类似的情况,补丁是为了减少等待时间.

The error message refers to a patch added to Chrome six months ago (https://codereview.chromium.org/345643003), which implements a 20-second timeout when the same resource is requested multiple times. In fact, one of the bugs the patch tries to fix (bug number 46104) refers to a similar situation, and the patch is meant to reduce the time spent waiting.

这里的答案(或解决方法)可能只是让请求看起来不同,尽管 Chrome 可能会尊重我设置的无缓存"标头.

It's possible the answer (or workaround) here is just to make the requests look different, although perhaps Chrome could respect the "no-cache" header I'm setting.

推荐答案

是的,这种行为是由于 Chrome 锁定了缓存并在再次请求相同资源之前等待查看一次请求的结果.答案是找到一种使请求独一无二的方法.我在查询字符串中添加了一个随机数,现在一切正常.

Yes, this behavior is due to Chrome locking the cache and waiting to see the result of one request before requesting the same resource again. The answer is to find a way to make the requests unique. I added a random number to the query string, and everything is working now.

为了将来参考,这是 Chrome 39.0.2171.95.

For future reference, this was Chrome 39.0.2171.95.

编辑:自从有了这个答案,我开始明白缓存控制:无缓存"并没有像我想象的那样做.尽管名称如此,但可以缓存带有此标头的响应.我还没有尝试过,但我想知道使用Cache-Control: no-store"(它确实可以阻止缓存)是否可以解决这个问题.

Edit: Since this answer, I've come to understand that "Cache-Control: no-cache" doesn't do what I thought it does. Despite its name, responses with this header can be cached. I haven't tried, but I wonder if using "Cache-Control: no-store", which does prevent caching, would fix the issue.

这篇关于向同一资源发出多个请求时,Chrome 会停顿吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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