我如何处理“connect ETIMEDOUT” URL 超载时出现错误“瓶颈”?
我正在用 NodeJs 发布请求。这是我的代码:
var request = require('request');
var rp = require('request-promise');
async function sendRequest(obj){
try{ // TRY STARTS HERE
var requestTarget = await rp({
method: 'POST',
uri: obj.url,
headers: {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,ar-AE;q=0.8,ar;q=0.7",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"cookie":obj.cookie,
"Referer":obj.referer ,
"Referrer-Policy": "strict-origin-when-cross-origin"
},
timeout:16000,
body : obj.body
}).then(
(response)=>{
return response
})
// TRY ENDS HERE
}catch(error){ // catch STARTS HERE
console.error( " error is :" +error);
}// catch ENDS HERE
console.log(" response is : " +requestTarget);
}
无论如何,当我运行代码时,我遇到了 request: connect ETIMEDOUT 问题,但我不知道如何修复它。
什么可能导致此错误?我该如何处理这个服务器?
I am posting request with NodeJs. Here is my code:
var request = require('request');
var rp = require('request-promise');
async function sendRequest(obj){
try{ // TRY STARTS HERE
var requestTarget = await rp({
method: 'POST',
uri: obj.url,
headers: {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9,ar-AE;q=0.8,ar;q=0.7",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"cookie":obj.cookie,
"Referer":obj.referer ,
"Referrer-Policy": "strict-origin-when-cross-origin"
},
timeout:16000,
body : obj.body
}).then(
(response)=>{
return response
})
// TRY ENDS HERE
}catch(error){ // catch STARTS HERE
console.error( " error is :" +error);
}// catch ENDS HERE
console.log(" response is : " +requestTarget);
}
Anyway, I've got Problem with request: connect ETIMEDOUT when I run the code and I have no idea how to fix it.
What could cause this error ? and how can i handle this server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ETIMEDOUT
表示服务器在给定的超时(在您的示例中为 16 秒)时间内没有响应您的请求。该错误可以在 catch 块中处理。ETIMEDOUT
means that the server didn't answer to your request in the given timeout(in your example 16 seconds) time. That error can be handled in the catch block.您的问题标题提到您认为您的目标服务器过载。在这种情况下可能会出现 ETIMEDOUT 错误。拒绝服务攻击?
在尝试出站连接的代码中,除了捕获错误、等待一段时间并重试之外,您无能为力。
理想情况下,每次连续捕获错误时,您将等待两倍的时间才能再次尝试。对于第一次等待,请使用一秒钟。然后等待 2、4、8、16,依此类推。您希望让目标服务器有机会处理其连接队列,而对其进行锤击会减慢该过程。
大约一分钟后你应该放弃。如果您有错误记录系统,请将错误发送到该系统。如果您的服务是 Web 服务,则向您的客户端发送 500(一般服务器错误)、502(上游服务器网关错误)或 504(上游服务器超时),具体取决于您的应用程序的意义。
如果您还控制目标服务器,您就可以弄清楚那里发生了什么。您可能需要更多容量。或者一个流氓客户可能会非常努力地达到目标。您的目标可能正在遭受拒绝服务攻击。
处理 ETIMEDOUT 错误是 xxx 中的一个痛苦。如果您的服务不是关键任务并且这种情况不经常发生,您可以简单地向客户端发出 500、502 或 504 错误。不过,不要忘记记录错误:修复这种情况可能需要人工干预。
Your question title mentioned that you believe your target server is overloaded. ETIMEDOUT errors can come up in that situation. Denial of service attack?
You don't have much you can do in your code attempting the outbound connect, except catch the error, wait for a period of time and try again.
Ideally, each consecutive time you catch the error you will wait for twice as much time before trying again. For the first wait, use one second. Then wait 2, 4, 8, 16, and so forth. You want to give the target server a chance to work through its connection queue, and hammering on it slows that process.
After a minute or so you should give up. Deliver an error to your error logging system if you have one. If your service is a web service, to YOUR client deliver a 500 (generic server error), 502 (bad gateway to upstream server), or 504 (upstream server timed out), depending on what makes sense for your appication.
If you also control the target server, you can figure out what is happening there. You may need more capacity. Or a rogue client may be hitting the target really hard. It's possible your target is experiencing a denial-of-service attack.
Handling ETIMEDOUT errors is a pain in the xxx. If your service isn't mission-critical and this doesn't happen very often you can simply issue the 500, 502, or 504 error to your client. Don't forget to log the error, though: fixing the situation may require human intervention.
由于端口被阻塞,所以发生了错误。
请检查端口是否已启用。
Because the port was blocked, the error happened.
Please check that the port was enable.