From 33a5d3b346f05631a8687c421590eef825993121 Mon Sep 17 00:00:00 2001 From: Ryouaki Date: Thu, 23 Aug 2018 09:51:46 +0800 Subject: [PATCH 1/2] Fixing socket hang up error on node side for slow response. --- lib/adapters/http.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 00dedc4c3f..48708a38b0 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -18,13 +18,10 @@ var isHttps = /https:?/; /*eslint consistent-return:0*/ module.exports = function httpAdapter(config) { return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) { - var timer; var resolve = function resolve(value) { - clearTimeout(timer); resolvePromise(value); }; var reject = function reject(value) { - clearTimeout(timer); rejectPromise(value); }; var data = config.data; @@ -246,10 +243,15 @@ module.exports = function httpAdapter(config) { // Handle request timeout if (config.timeout) { - timer = setTimeout(function handleRequestTimeout() { - req.abort(); - reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req)); - }, config.timeout); + // 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() + reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req)) + }) } if (config.cancelToken) { From 1918ab3f8d5e22dd4e2c15c2ce11e76520582743 Mon Sep 17 00:00:00 2001 From: Ryouaki Date: Thu, 23 Aug 2018 10:09:30 +0800 Subject: [PATCH 2/2] eslint check --- lib/adapters/http.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 48708a38b0..bcffef3bac 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -249,9 +249,9 @@ module.exports = function httpAdapter(config) { // 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() - reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req)) - }) + req.abort(); + reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req)); + }); } if (config.cancelToken) {