gpt4 book ai didi

node.js - 超时 POST 请求的浏览器重试行为不一致

转载 作者:IT老高 更新时间:2023-10-28 22:12:05 61 4
gpt4 key购买 nike

当由于超时而没有来自服务器的响应时,我偶尔会重试 POST 请求。所有现代浏览器都有幂等请求(GET、HEAD 等)的重试逻辑,但我无法解释为什么它会发生在 POST 请求中。

我正在使用带有 3 个路由和 chrome 浏览器的简单 node.js 服务器测试这个案例。

/       : gives a html page with jquery and code snippets to fire ajax requests
/hi : gives a text response 'hello'
/sleep : request will timeout without any response

默认情况下,node.js http 服务器会在 2 分钟后超时。

重试.js

var http = require('http');
var server = http.createServer();

server.on('request', function(req, res) {
console.log(new Date() + ' ' + req.method + ' request on ' + req.url);

if (req.url === '/sleep') {
console.log('!!! sleeping');
} else if (req.url === '/') {
html = "$.post('/hi', {'for':'server'}, function() { console.log(arguments) } ).error(function() { console.log(arguments) })";
html += "<br><br>";
html += "$.post('/sleep', {'for':'infinite'}, function() { console.log(arguments) } ).error(function() { console.log(arguments) })";
html += '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>';

res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello');
}
});

server.listen(2020);
console.log('server listening on port 2020');

运行它

$ node retry.js 
server listening on port 2020

1

Load this page in browser http://localhost:2020

Fri Mar 01 2013 12:21:59 GMT+0530 (IST) GET request on /
Fri Mar 01 2013 12:21:59 GMT+0530 (IST) GET request on /favicon.ico

2

从开发控制台,使用 jquery 向/hi 发起一个 ajax POST 请求

$.post('/hi', {'for':'server'}, function() { console.log(arguments) } ).error(function() { console.log(arguments) })

Fri Mar 01 2013 12:22:05 GMT+0530 (IST) POST request on /hi

3

向/sleep 发出 POST 请求,2 分钟后重试,4 分钟后出错。

$.post('/sleep', {'for':'infinite'}, function() { console.log(arguments) } ).error(function() { console.log(arguments) })

服务器日志显示 2 个请求

Fri Mar 01 2013 12:22:21 GMT+0530 (IST) POST request on /sleep
!!! sleeping
Fri Mar 01 2013 12:24:21 GMT+0530 (IST) POST request on /sleep
!!! sleeping

再次触发,2分钟后出错,无需重试。

Fri Mar 01 2013 12:30:01 GMT+0530 (IST) POST request on /sleep
!!! sleeping ?

在我们向/hi(或任何其他 url)发出请求并导致响应之前,它不会重试。并且仅对一个后续请求/sleep 进行重试。

在浏览器中,网络标签显示类似

/hi - success 
/sleep - cancelled - 4 mins (retry happens)
/sleep - cancelled - 2 mins (no retry)
/sleep - cancelled - 2 mins (no retry)
/hi - success
/sleep - cancelled - 4 mins (retry happens)
/sleep - cancelled - 2 mins (no retry)
/sleep - cancelled - 2 mins (no retry)

问题

虽然我们需要设计我们的 Web 应用程序来容忍这些额外的请求(通过浏览器或任何其他中介),但这种不一致的浏览器重试看起来很奇怪。我在 chrome (v16 & v24) 和 firefox 中观察到了这种行为。

谁能帮我理解超时非幂等请求背后的浏览器重试逻辑?

其他相关的 stackoverflow 问题

What happens when no response is received for a request? I'm seeing retries

最佳答案

当连接关闭时,浏览器会在收到服务器响应之前重试请求(包括 POST)。这在 HTTP 1.1 Spec Section 8.2.4 中定义。 .

关于node.js - 超时 POST 请求的浏览器重试行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15155014/

61 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com