From c0148a01b51f13f534df3cb58ef5eb39151fd9a0 Mon Sep 17 00:00:00 2001 From: youkunhuang Date: Wed, 20 Jun 2018 14:47:06 +0800 Subject: [PATCH] chore(error): logger socker error info --- bin/tsw/ajax/ajax.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/bin/tsw/ajax/ajax.js b/bin/tsw/ajax/ajax.js index 68aec9b9..7bb9405b 100644 --- a/bin/tsw/ajax/ajax.js +++ b/bin/tsw/ajax/ajax.js @@ -556,19 +556,43 @@ Ajax.prototype.doRequest = function(opt) { request.once('socket', function(socket) { if (socket.remoteAddress) { this.remoteIp = socket.remoteAddress; + logger.debug(`${logPre}socket reuse ${socket.remoteAddress}:${socket.remotePort}`); return; } + const onError = (err) => { + logger.error(logPre + err.stack); + clean(); + this.emit('fail'); + }; + + const onConnect = function() { + logger.debug(`${logPre}connect ${this.remoteAddress}:${this.remotePort}`); + clean(); + }; + + const onLookup = (err, address, family, host) => { + if (err) { + logger.error(logPre + err.stack); + clean(); + this.emit('fail'); + return; + } + this.remoteIp = address; + }; + + const clean = function() { + socket.removeListener('error', onError); + socket.removeListener('connect', onConnect); + socket.removeListener('lookup', onLookup); + }; + if (!net.isIP(opt.host)) { - socket.once('lookup', (err, address, family, host) => { - if (err) { - logger.error(logPre + err.stack); - this.emit('fail'); - return; - } - this.remoteIp = address; - }); + socket.once('lookup', onLookup); } + + socket.once('connect', onConnect); + socket.once('error', onError); }); defer.always(function() {