Skip to content

Commit

Permalink
refactor(capture): clean event after end or error
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 19, 2018
1 parent 7d0a1e1 commit fde04f9
Showing 1 changed file with 67 additions and 40 deletions.
107 changes: 67 additions & 40 deletions bin/tsw/runtime/capturer.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ process.nextTick(function() {

timeEnd = new Date().getTime();

request.removeListener('response', onResponse);
request.removeListener('socket', onSocket);
request.removeListener('error', onError);

if (captureBody) {
buffer = Buffer.concat(result);
result = [];
Expand All @@ -160,45 +164,7 @@ process.nextTick(function() {
}
};

request.once('socket', function(socket) {
if (socket.remoteAddress) {
timeLookup = Date.now();
timeConnect = Date.now();
remoteAddress = socket.remoteAddress;
remotePort = socket.remotePort;
const cost = timeLookup - timeStart;
logger.debug(`${logPre}socket reuse ${remoteAddress}:${remotePort}, cost ${cost}ms`);
return;
}

if (!net.isIP(opt.host)) {
socket.once('lookup', (err, address, family, host) => {
timeLookup = Date.now();
if (err) {
logger.error(logPre + err.stack);
finish();
return;
}
const cost = timeLookup - timeStart;
logger.debug(`${logPre}dns lookup ${host} -> ${address}, cost ${cost}ms`);
});
}

socket.once('connect', function() {
timeConnect = Date.now();
const cost = timeConnect - timeStart;
remoteAddress = this.remoteAddress;
remotePort = this.remotePort;
logger.debug(`${logPre}connect ${remoteAddress}:${remotePort}, cost ${cost}ms`);
});
});

request.once('error', function(err) {
logger.error(err.stack);
finish();
});

request.once('response', (response) => {
const onResponse = function(response) {
timeResponse = Date.now();

const socket = response.socket;
Expand Down Expand Up @@ -253,7 +219,68 @@ process.nextTick(function() {
done.call(this);
});

});
};

const onError = function(err) {
logger.error(err.stack);
finish();
};

const onSocket = function(socket) {
if (socket.remoteAddress) {
timeLookup = Date.now();
timeConnect = Date.now();
remoteAddress = socket.remoteAddress;
remotePort = socket.remotePort;
const cost = timeLookup - timeStart;
logger.debug(`${logPre}socket reuse ${remoteAddress}:${remotePort}, cost ${cost}ms`);
return;
}

const onError = function(err) {
logger.error(logPre + err.stack);
clean();
finish();
};

const onConnect = function() {
timeConnect = Date.now();
const cost = timeConnect - timeStart;
remoteAddress = this.remoteAddress;
remotePort = this.remotePort;
logger.debug(`${logPre}connect ${remoteAddress}:${remotePort}, cost ${cost}ms`);
clean();
};

const onLookup = function(err, address, family, host) {
timeLookup = Date.now();
if (err) {
logger.error(logPre + err.stack);
clean();
finish();
return;
}
const cost = timeLookup - timeStart;
logger.debug(`${logPre}dns lookup ${host} -> ${address}, cost ${cost}ms`);
};

const clean = function() {
socket.removeListener('error', onError);
socket.removeListener('connect', onConnect);
socket.removeListener('lookup', onLookup);
};

if (!net.isIP(opt.host)) {
socket.once('lookup', onLookup);
}

socket.once('connect', onConnect);
socket.once('error', onError);
};

request.once('socket', onSocket);
request.once('error', onError);
request.once('response', onResponse);

return request;
};
Expand Down

0 comments on commit fde04f9

Please sign in to comment.