Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: use arrow fns for lexical this in Agent #16475

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 39 additions & 41 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,31 @@ function Agent(options) {

EventEmitter.call(this);

var self = this;
this.defaultPort = 80;
this.protocol = 'http:';

self.defaultPort = 80;
self.protocol = 'http:';

self.options = util._extend({}, options);
this.options = util._extend({}, options);

// don't confuse net and make it think that we're connecting to a pipe
self.options.path = null;
self.requests = {};
self.sockets = {};
self.freeSockets = {};
self.keepAliveMsecs = self.options.keepAliveMsecs || 1000;
self.keepAlive = self.options.keepAlive || false;
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
self.maxFreeSockets = self.options.maxFreeSockets || 256;

self.on('free', function(socket, options) {
var name = self.getName(options);
this.options.path = null;
this.requests = {};
this.sockets = {};
this.freeSockets = {};
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
this.keepAlive = this.options.keepAlive || false;
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
this.maxFreeSockets = this.options.maxFreeSockets || 256;

this.on('free', (socket, options) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW this function can be pulled out, named, and referenced here instead IIRC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an advantage to pulling it out here?

var name = this.getName(options);
debug('agent.on(free)', name);

if (socket.writable &&
self.requests[name] && self.requests[name].length) {
self.requests[name].shift().onSocket(socket);
if (self.requests[name].length === 0) {
this.requests[name] && this.requests[name].length) {
this.requests[name].shift().onSocket(socket);
if (this.requests[name].length === 0) {
// don't leak
delete self.requests[name];
delete this.requests[name];
}
} else {
// If there are no pending requests, then put it in
Expand All @@ -81,21 +79,21 @@ function Agent(options) {
if (req &&
req.shouldKeepAlive &&
socket.writable &&
self.keepAlive) {
var freeSockets = self.freeSockets[name];
this.keepAlive) {
var freeSockets = this.freeSockets[name];
var freeLen = freeSockets ? freeSockets.length : 0;
var count = freeLen;
if (self.sockets[name])
count += self.sockets[name].length;
if (this.sockets[name])
count += this.sockets[name].length;

if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
socket.destroy();
} else if (self.keepSocketAlive(socket)) {
} else if (this.keepSocketAlive(socket)) {
freeSockets = freeSockets || [];
self.freeSockets[name] = freeSockets;
this.freeSockets[name] = freeSockets;
socket[async_id_symbol] = -1;
socket._httpMessage = null;
self.removeSocket(socket, options);
this.removeSocket(socket, options);
freeSockets.push(socket);
} else {
// Implementation doesn't want to keep socket alive
Expand Down Expand Up @@ -201,9 +199,8 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
};

Agent.prototype.createSocket = function createSocket(req, options, cb) {
var self = this;
options = util._extend({}, options);
util._extend(options, self.options);
util._extend(options, this.options);
if (options.socketPath)
options.path = options.socketPath;

Expand All @@ -215,30 +212,31 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
}
}

var name = self.getName(options);
var name = this.getName(options);
options._agentKey = name;

debug('createConnection', name, options);
options.encoding = null;
var called = false;
const newSocket = self.createConnection(options, oncreate);
if (newSocket)
oncreate(null, newSocket);

function oncreate(err, s) {
const oncreate = (err, s) => {
if (called)
return;
called = true;
if (err)
return cb(err);
if (!self.sockets[name]) {
self.sockets[name] = [];
if (!this.sockets[name]) {
this.sockets[name] = [];
}
self.sockets[name].push(s);
debug('sockets', name, self.sockets[name].length);
installListeners(self, s, options);
this.sockets[name].push(s);
debug('sockets', name, this.sockets[name].length);
installListeners(this, s, options);
cb(null, s);
}
};

const newSocket = this.createConnection(options, oncreate);
if (newSocket)
oncreate(null, newSocket);
};

function installListeners(agent, s, options) {
Expand Down