use*_*355 3 linux request node.js
node.js request我想知道模块在参数方面如何工作timeout。
timeout一段时间过去后会发生什么?IE:
var request = require('request');
var options = {
url: Theurl,
timeout: 300000
};
request(options, function(error, resp, body) {...
Run Code Online (Sandbox Code Playgroud)
之后会发生什么300000?request是否尝试再次请求该url?
我还发现Linux Kernel有一个默认的 20 秒TCP socket connection timeout.(http://www.sekuda.com/overriding_the_default_linux_kernel_20_second_tcp_socket_connect_timeout),这是否意味着无论我设置什么,该timeout选项request将是最大 20 秒(如果我不更改) ?我用。Linux Kernel timeoutoptionsUbuntu
来自请求包的自述文件:
Note that if the underlying TCP connection cannot be established,
the OS-wide TCP connection timeout will overrule the timeout option
Run Code Online (Sandbox Code Playgroud)
因此,在您的情况下,请求将在 20 秒后中止。该请求不会再次尝试请求该 url(即使超时设置为低于 20000 的值)。您必须为此编写自己的逻辑或使用其他包,例如requestretry。
例子:
var options = {
url: 'http://www.gooooerererere.com/',
timeout: 5000
}
var maxRequests = 5;
function requestWithTimeout(attempt){
request(options, function(error,response,body){
if(error){
console.log(error);
if(attempt==maxRequests)
return;
else
requestWithTimeout(attempt+1);
}
else {
//do something with result
}
});
}
requestWithTimeout(1);
Run Code Online (Sandbox Code Playgroud)
您还可以检查特定的错误消息,例如 ETIMEDOUT,
if(error.code == [ERROR_MESSAGE])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11791 次 |
| 最近记录: |