我正在使用request/request模块向API发送 100 并行GET
请求。我遇到的问题是关于80th to 100th request
的随机问题,node
会抛出此异常:
events.js:141
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 0.0.0.0:80 //a dummy ip
at Object.exports._errnoException (util.js:860:11)
at exports._exceptionWithHostPort (util.js:883:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
这是我的请求默认值:
var apiRequest = request.defaults({
url : url, //response is in xml format
headers : {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8'
},
method : 'GET',
gzip : true,
encoding : 'utf8',
pool: {maxSockets: Infinity},
forever : true,
timeout : 120000
});
此外,我正在请求在数组上使用循环
答案 0 :(得分:0)
通过请求失败的网址on error event
递归来解决我的问题,直到没有超时:
function getAPI(url) {
var apiRequest = request.defaults({
url : url,
headers : {
'Accept': 'text/html,application/xhtml+xml,application/xml'
},
method : 'GET',
gzip : true
});
apiRequest(function (error, response, body) {
}).on('error', function(e){
getAPI(this.uri.href);//this is important
}).end();
}