Skip to content

Commit 9b8e2a6

Browse files
benglgibfahn
authored andcommitted
http: use arrow fns for lexical this in Agent
PR-URL: #16475 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6583016 commit 9b8e2a6

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

lib/_http_agent.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,33 +46,31 @@ function Agent(options) {
4646

4747
EventEmitter.call(this);
4848

49-
var self = this;
49+
this.defaultPort = 80;
50+
this.protocol = 'http:';
5051

51-
self.defaultPort = 80;
52-
self.protocol = 'http:';
53-
54-
self.options = util._extend({}, options);
52+
this.options = util._extend({}, options);
5553

5654
// don't confuse net and make it think that we're connecting to a pipe
57-
self.options.path = null;
58-
self.requests = {};
59-
self.sockets = {};
60-
self.freeSockets = {};
61-
self.keepAliveMsecs = self.options.keepAliveMsecs || 1000;
62-
self.keepAlive = self.options.keepAlive || false;
63-
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
64-
self.maxFreeSockets = self.options.maxFreeSockets || 256;
65-
66-
self.on('free', function(socket, options) {
67-
var name = self.getName(options);
55+
this.options.path = null;
56+
this.requests = {};
57+
this.sockets = {};
58+
this.freeSockets = {};
59+
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
60+
this.keepAlive = this.options.keepAlive || false;
61+
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
62+
this.maxFreeSockets = this.options.maxFreeSockets || 256;
63+
64+
this.on('free', (socket, options) => {
65+
var name = this.getName(options);
6866
debug('agent.on(free)', name);
6967

7068
if (socket.writable &&
71-
self.requests[name] && self.requests[name].length) {
72-
self.requests[name].shift().onSocket(socket);
73-
if (self.requests[name].length === 0) {
69+
this.requests[name] && this.requests[name].length) {
70+
this.requests[name].shift().onSocket(socket);
71+
if (this.requests[name].length === 0) {
7472
// don't leak
75-
delete self.requests[name];
73+
delete this.requests[name];
7674
}
7775
} else {
7876
// If there are no pending requests, then put it in
@@ -81,21 +79,21 @@ function Agent(options) {
8179
if (req &&
8280
req.shouldKeepAlive &&
8381
socket.writable &&
84-
self.keepAlive) {
85-
var freeSockets = self.freeSockets[name];
82+
this.keepAlive) {
83+
var freeSockets = this.freeSockets[name];
8684
var freeLen = freeSockets ? freeSockets.length : 0;
8785
var count = freeLen;
88-
if (self.sockets[name])
89-
count += self.sockets[name].length;
86+
if (this.sockets[name])
87+
count += this.sockets[name].length;
9088

91-
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
89+
if (count > this.maxSockets || freeLen >= this.maxFreeSockets) {
9290
socket.destroy();
93-
} else if (self.keepSocketAlive(socket)) {
91+
} else if (this.keepSocketAlive(socket)) {
9492
freeSockets = freeSockets || [];
95-
self.freeSockets[name] = freeSockets;
93+
this.freeSockets[name] = freeSockets;
9694
socket[async_id_symbol] = -1;
9795
socket._httpMessage = null;
98-
self.removeSocket(socket, options);
96+
this.removeSocket(socket, options);
9997
freeSockets.push(socket);
10098
} else {
10199
// Implementation doesn't want to keep socket alive
@@ -201,9 +199,8 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
201199
};
202200

203201
Agent.prototype.createSocket = function createSocket(req, options, cb) {
204-
var self = this;
205202
options = util._extend({}, options);
206-
util._extend(options, self.options);
203+
util._extend(options, this.options);
207204
if (options.socketPath)
208205
options.path = options.socketPath;
209206

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

218-
var name = self.getName(options);
215+
var name = this.getName(options);
219216
options._agentKey = name;
220217

221218
debug('createConnection', name, options);
222219
options.encoding = null;
223220
var called = false;
224-
const newSocket = self.createConnection(options, oncreate);
225-
if (newSocket)
226-
oncreate(null, newSocket);
227221

228-
function oncreate(err, s) {
222+
const oncreate = (err, s) => {
229223
if (called)
230224
return;
231225
called = true;
232226
if (err)
233227
return cb(err);
234-
if (!self.sockets[name]) {
235-
self.sockets[name] = [];
228+
if (!this.sockets[name]) {
229+
this.sockets[name] = [];
236230
}
237-
self.sockets[name].push(s);
238-
debug('sockets', name, self.sockets[name].length);
239-
installListeners(self, s, options);
231+
this.sockets[name].push(s);
232+
debug('sockets', name, this.sockets[name].length);
233+
installListeners(this, s, options);
240234
cb(null, s);
241-
}
235+
};
236+
237+
const newSocket = this.createConnection(options, oncreate);
238+
if (newSocket)
239+
oncreate(null, newSocket);
242240
};
243241

244242
function installListeners(agent, s, options) {

0 commit comments

Comments
 (0)