mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Fixing socket hang up error on node side for slow response. (#1752)
* Fixing socket hang up error on node side for slow response. * eslint check
This commit is contained in:
committed by
Felipe Martins
parent
d74385f1c8
commit
93e69625a6
@@ -19,13 +19,10 @@ var isHttps = /https:?/;
|
|||||||
/*eslint consistent-return:0*/
|
/*eslint consistent-return:0*/
|
||||||
module.exports = function httpAdapter(config) {
|
module.exports = function httpAdapter(config) {
|
||||||
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
|
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
|
||||||
var timer;
|
|
||||||
var resolve = function resolve(value) {
|
var resolve = function resolve(value) {
|
||||||
clearTimeout(timer);
|
|
||||||
resolvePromise(value);
|
resolvePromise(value);
|
||||||
};
|
};
|
||||||
var reject = function reject(value) {
|
var reject = function reject(value) {
|
||||||
clearTimeout(timer);
|
|
||||||
rejectPromise(value);
|
rejectPromise(value);
|
||||||
};
|
};
|
||||||
var data = config.data;
|
var data = config.data;
|
||||||
@@ -249,10 +246,15 @@ module.exports = function httpAdapter(config) {
|
|||||||
|
|
||||||
// Handle request timeout
|
// Handle request timeout
|
||||||
if (config.timeout) {
|
if (config.timeout) {
|
||||||
timer = setTimeout(function handleRequestTimeout() {
|
// Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
|
||||||
|
// And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
|
||||||
|
// At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.
|
||||||
|
// And then these socket which be hang up will devoring CPU little by little.
|
||||||
|
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
|
||||||
|
req.setTimeout(config.timeout, function handleRequestTimeout() {
|
||||||
req.abort();
|
req.abort();
|
||||||
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
|
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req));
|
||||||
}, config.timeout);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.cancelToken) {
|
if (config.cancelToken) {
|
||||||
|
|||||||
Reference in New Issue
Block a user