http: use arrow fns for lexical this in Agent

PR-URL: https://github.com/nodejs/node/pull/16475
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Bryan English 2017-10-24 23:34:43 -07:00
parent 291ff72f85
commit 1f045f491a

View File

@ -46,33 +46,31 @@ function Agent(options) {
EventEmitter.call(this); EventEmitter.call(this);
var self = this; this.defaultPort = 80;
this.protocol = 'http:';
self.defaultPort = 80; this.options = util._extend({}, options);
self.protocol = 'http:';
self.options = util._extend({}, options);
// don't confuse net and make it think that we're connecting to a pipe // don't confuse net and make it think that we're connecting to a pipe
self.options.path = null; this.options.path = null;
self.requests = {}; this.requests = {};
self.sockets = {}; this.sockets = {};
self.freeSockets = {}; this.freeSockets = {};
self.keepAliveMsecs = self.options.keepAliveMsecs || 1000; this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
self.keepAlive = self.options.keepAlive || false; this.keepAlive = this.options.keepAlive || false;
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets; this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
self.maxFreeSockets = self.options.maxFreeSockets || 256; this.maxFreeSockets = this.options.maxFreeSockets || 256;
self.on('free', function(socket, options) { this.on('free', (socket, options) => {
var name = self.getName(options); var name = this.getName(options);
debug('agent.on(free)', name); debug('agent.on(free)', name);
if (socket.writable && if (socket.writable &&
self.requests[name] && self.requests[name].length) { this.requests[name] && this.requests[name].length) {
self.requests[name].shift().onSocket(socket); this.requests[name].shift().onSocket(socket);
if (self.requests[name].length === 0) { if (this.requests[name].length === 0) {
// don't leak // don't leak
delete self.requests[name]; delete this.requests[name];
} }
} else { } else {
// If there are no pending requests, then put it in // If there are no pending requests, then put it in
@ -81,21 +79,21 @@ function Agent(options) {
if (req && if (req &&
req.shouldKeepAlive && req.shouldKeepAlive &&
socket.writable && socket.writable &&
self.keepAlive) { this.keepAlive) {
var freeSockets = self.freeSockets[name]; var freeSockets = this.freeSockets[name];
var freeLen = freeSockets ? freeSockets.length : 0; var freeLen = freeSockets ? freeSockets.length : 0;
var count = freeLen; var count = freeLen;
if (self.sockets[name]) if (this.sockets[name])
count += self.sockets[name].length; count += this.sockets[name].length;
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) { if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
socket.destroy(); socket.destroy();
} else if (self.keepSocketAlive(socket)) { } else if (this.keepSocketAlive(socket)) {
freeSockets = freeSockets || []; freeSockets = freeSockets || [];
self.freeSockets[name] = freeSockets; this.freeSockets[name] = freeSockets;
socket[async_id_symbol] = -1; socket[async_id_symbol] = -1;
socket._httpMessage = null; socket._httpMessage = null;
self.removeSocket(socket, options); this.removeSocket(socket, options);
freeSockets.push(socket); freeSockets.push(socket);
} else { } else {
// Implementation doesn't want to keep socket alive // Implementation doesn't want to keep socket alive
@ -196,39 +194,39 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
}; };
Agent.prototype.createSocket = function createSocket(req, options, cb) { Agent.prototype.createSocket = function createSocket(req, options, cb) {
var self = this;
options = util._extend({}, options); options = util._extend({}, options);
util._extend(options, self.options); util._extend(options, this.options);
if (options.socketPath) if (options.socketPath)
options.path = options.socketPath; options.path = options.socketPath;
if (!options.servername) if (!options.servername)
options.servername = calculateServerName(options, req); options.servername = calculateServerName(options, req);
var name = self.getName(options); var name = this.getName(options);
options._agentKey = name; options._agentKey = name;
debug('createConnection', name, options); debug('createConnection', name, options);
options.encoding = null; options.encoding = null;
var called = false; var called = false;
const newSocket = self.createConnection(options, oncreate);
if (newSocket)
oncreate(null, newSocket);
function oncreate(err, s) { const oncreate = (err, s) => {
if (called) if (called)
return; return;
called = true; called = true;
if (err) if (err)
return cb(err); return cb(err);
if (!self.sockets[name]) { if (!this.sockets[name]) {
self.sockets[name] = []; this.sockets[name] = [];
} }
self.sockets[name].push(s); this.sockets[name].push(s);
debug('sockets', name, self.sockets[name].length); debug('sockets', name, this.sockets[name].length);
installListeners(self, s, options); installListeners(this, s, options);
cb(null, s); cb(null, s);
} };
const newSocket = this.createConnection(options, oncreate);
if (newSocket)
oncreate(null, newSocket);
}; };
function calculateServerName(options, req) { function calculateServerName(options, req) {