Skip to content

Commit

Permalink
fix(capture): fix event leek
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Jun 15, 2018
1 parent 2c31bc0 commit 7d0a1e1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
24 changes: 16 additions & 8 deletions bin/tsw/ajax/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const http = require('http');
const https = require('https');
const vm = require('vm');
const url = require('url');
const net = require('net');
const qs = require('qs');
const form = require('./form.js');
const token = require('./token.js');
Expand Down Expand Up @@ -553,14 +554,21 @@ Ajax.prototype.doRequest = function(opt) {
request.setSocketKeepAlive(true);

request.once('socket', function(socket) {
socket.once('lookup', (err, address, family, host) => {
if (err) {
logger.error(logPre + err.stack);
this.emit('fail');
return;
}
this.remoteIp = address;
});
if (socket.remoteAddress) {
this.remoteIp = socket.remoteAddress;
return;
}

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;
});
}
});

defer.always(function() {
Expand Down
29 changes: 21 additions & 8 deletions bin/tsw/runtime/capturer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
const http = require('http');
const https = require('https');
const net = require('net');
let isFirstLoad = true;

if (global[__filename]) {
Expand Down Expand Up @@ -160,16 +161,28 @@ process.nextTick(function() {
};

request.once('socket', function(socket) {
socket.once('lookup', (err, address, family, host) => {
if (socket.remoteAddress) {
timeLookup = Date.now();
if (err) {
logger.error(logPre + err.stack);
finish();
return;
}
timeConnect = Date.now();
remoteAddress = socket.remoteAddress;
remotePort = socket.remotePort;
const cost = timeLookup - timeStart;
logger.debug(`${logPre}dns lookup ${host} -> ${address}, cost ${cost}ms`);
});
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();
Expand Down

0 comments on commit 7d0a1e1

Please sign in to comment.