forked from request/forever-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (148 loc) · 4.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module.exports = ForeverAgent
ForeverAgent.SSL = ForeverAgentSSL
var util = require('util');
var net = require('net');
var tls = require('tls');
var Agent = require('http').Agent;
var AgentSSL = require('https').Agent;
function getConnectionName(host, port) {
if (typeof host === 'string') {
return host + ':' + port;
} else {
// For node.js v012.0 and iojs-v1.5.1, host is an object. And any existing localAddress is part of the connection name.
return host.host + ':' + host.port + ':' + (host.localAddress ? (host.localAddress + ':') : ':');
}
}
// We need to get data list from { name1 : [data1, data2], ... }
function getDataList(obj) {
return Object.keys(obj).reduce(function (list, name) {
return list.concat(obj[name]);
}, []);
}
function ForeverAgent(options) {
var self = this;
self.options = options || {};
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets;
self.requests = {};
self.sockets = {};
self.freeSockets = {};
self.on('free', function(socket, host, port) {
// Ignore destroyed sockets
if (!socket.writable) {
return;
}
var name = getConnectionName(host, port);
var requests = self.requests[name];
var sockets = self.sockets[name];
if (requests && requests.length > 0) {
requests.shift().onSocket(socket);
} else if (sockets && sockets.length < self.minSockets) {
var freeSockets = self.freeSockets[name];
if (!freeSockets) {
self.freeSockets[name] = freeSockets = [];
}
freeSockets.push(socket);
// if an error happens while we don't use the socket anyway, meh, throw the socket away
var onIdleError = function() {
socket.destroy();
}
socket._onIdleError = onIdleError;
socket.on('error', onIdleError);
} else {
// If there are no pending requests just destroy the
// socket and it will get removed from the pool. This
// gets us out of timeout issues and allows us to
// default to Connection:keep-alive.
socket.destroy();
}
});
}
util.inherits(ForeverAgent, Agent);
ForeverAgent.defaultMinSockets = 5;
ForeverAgent.prototype.createConnection = net.createConnection;
ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest;
ForeverAgent.prototype.addRequest = function(req, host, port) {
var name = getConnectionName(host, port);
if (typeof host !== 'string') {
var options = host;
port = options.port;
host = options.host;
}
var freeSockets = this.freeSockets[name];
if (freeSockets && freeSockets.length > 0 && !req.useChunkedEncodingByDefault) {
var idleSocket = freeSockets.pop();
idleSocket.removeListener('error', idleSocket._onIdleError);
delete idleSocket._onIdleError;
req._reusedSocket = true;
req.onSocket(idleSocket);
} else {
this.addRequestNoreuse(req, host, port);
}
}
ForeverAgent.prototype.removeSocket = function(s, name, host, port) {
var sockets = this.sockets[name];
if (sockets) {
var index = this.sockets[name].indexOf(s);
if (index !== -1) {
sockets.splice(index, 1);
if (sockets.length === 0) {
delete this.sockets[name];
delete this.requests[name];
}
}
}
var freeSockets = this.freeSockets[name];
if (freeSockets) {
var index = freeSockets.indexOf(s);
if (index !== -1) {
freeSockets.splice(index, 1);
if (freeSockets.length === 0) {
delete this.freeSockets[name];
}
}
}
var requests = this.requests[name];
if (requests && requests.length > 0) {
// If we have pending requests and a socket gets closed a new one
// needs to be created to take over in the pool for the one that closed.
this.createSocket(name, host, port).emit('free');
}
}
ForeverAgent.prototype.destroy = function() {
getDataList(this.requests).forEach(function (request) {
request.abort();
});
getDataList(this.sockets)
.concat(getDataList(this.freeSockets))
.forEach(function (socket) {
socket.destroy();
});
this.requests = {};
this.sockets = {};
this.freeSockets = {};
}
function ForeverAgentSSL (options) {
ForeverAgent.call(this, options);
}
util.inherits(ForeverAgentSSL, ForeverAgent);
ForeverAgentSSL.prototype.createConnection = createConnectionSSL;
ForeverAgentSSL.prototype.addRequestNoreuse = AgentSSL.prototype.addRequest;
function createConnectionSSL (port, host, options) {
if (typeof port === 'object') {
options = port;
} else if (typeof host === 'object') {
options = host;
} else if (typeof options === 'object') {
options = options;
} else {
options = {};
}
if (typeof port === 'number') {
options.port = port;
}
if (typeof host === 'string') {
options.host = host;
}
return tls.connect(options);
}