_http_agent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Copyright Joyent, Inc. and other Node contributors.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to permit
  8. // persons to whom the Software is furnished to do so, subject to the
  9. // following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included
  12. // in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. // copy from https://github.com/nodejs/node/blob/v4.x/lib/_http_agent.js
  22. 'use strict';
  23. var net = require('net');
  24. var util = require('util');
  25. var EventEmitter = require('events').EventEmitter;
  26. var debug = require('./utils').debug;
  27. // New Agent code.
  28. // The largest departure from the previous implementation is that
  29. // an Agent instance holds connections for a variable number of host:ports.
  30. // Surprisingly, this is still API compatible as far as third parties are
  31. // concerned. The only code that really notices the difference is the
  32. // request object.
  33. // Another departure is that all code related to HTTP parsing is in
  34. // ClientRequest.onSocket(). The Agent is now *strictly*
  35. // concerned with managing a connection pool.
  36. function Agent(options) {
  37. if (!(this instanceof Agent))
  38. return new Agent(options);
  39. EventEmitter.call(this);
  40. var self = this;
  41. self.defaultPort = 80;
  42. self.protocol = 'http:';
  43. self.options = util._extend({}, options);
  44. // don't confuse net and make it think that we're connecting to a pipe
  45. self.options.path = null;
  46. self.requests = {};
  47. self.sockets = {};
  48. self.freeSockets = {};
  49. self.keepAliveMsecs = self.options.keepAliveMsecs || 1000;
  50. self.keepAlive = self.options.keepAlive || false;
  51. // free keep-alive socket timeout. By default free socket do not have a timeout.
  52. // keepAliveTimeout should be rename to `freeSocketKeepAliveTimeout`
  53. self.keepAliveTimeout = self.options.keepAliveTimeout || 0;
  54. // working socket timeout. By default working socket do not have a timeout.
  55. self.timeout = self.options.timeout || 0;
  56. self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
  57. self.maxFreeSockets = self.options.maxFreeSockets || 256;
  58. self.on('free', function(socket, options) {
  59. var name = self.getName(options);
  60. debug('agent.on(free)', name);
  61. if (!socket.destroyed &&
  62. self.requests[name] && self.requests[name].length) {
  63. self.requests[name].shift().onSocket(socket);
  64. if (self.requests[name].length === 0) {
  65. // don't leak
  66. delete self.requests[name];
  67. }
  68. debug('continue handle next request');
  69. } else {
  70. // If there are no pending requests, then put it in
  71. // the freeSockets pool, but only if we're allowed to do so.
  72. var req = socket._httpMessage;
  73. if (req &&
  74. req.shouldKeepAlive &&
  75. !socket.destroyed &&
  76. self.options.keepAlive) {
  77. var freeSockets = self.freeSockets[name];
  78. var freeLen = freeSockets ? freeSockets.length : 0;
  79. var count = freeLen;
  80. if (self.sockets[name])
  81. count += self.sockets[name].length;
  82. // console.log(count, freeLen, self.maxSockets, self.maxFreeSockets)
  83. if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
  84. // console.log('hit max sockets', count, freeLen, self.maxSockets, self.maxFreeSockets);
  85. self.removeSocket(socket, options);
  86. socket.destroy();
  87. } else {
  88. freeSockets = freeSockets || [];
  89. self.freeSockets[name] = freeSockets;
  90. socket.setKeepAlive(true, self.keepAliveMsecs);
  91. socket.unref && socket.unref();
  92. socket._httpMessage = null;
  93. self.removeSocket(socket, options);
  94. freeSockets.push(socket);
  95. // Add a default error handler to avoid Unhandled 'error' event throw on idle socket
  96. // https://github.com/node-modules/agentkeepalive/issues/25
  97. // https://github.com/nodejs/node/pull/4482 (fixed in >= 4.4.0 and >= 5.4.0)
  98. if (socket.listeners('error').length === 0) {
  99. socket.once('error', freeSocketErrorListener);
  100. }
  101. // set free keepalive timer
  102. socket.setTimeout(self.keepAliveTimeout);
  103. }
  104. } else {
  105. self.removeSocket(socket, options);
  106. socket.destroy();
  107. }
  108. }
  109. });
  110. }
  111. util.inherits(Agent, EventEmitter);
  112. exports.Agent = Agent;
  113. function freeSocketErrorListener(err) {
  114. var socket = this;
  115. debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
  116. socket.destroy();
  117. socket.emit('agentRemove');
  118. }
  119. Agent.defaultMaxSockets = Infinity;
  120. Agent.prototype.createConnection = net.createConnection;
  121. // Get the key for a given set of request options
  122. Agent.prototype.getName = function(options) {
  123. var name = '';
  124. if (options.host)
  125. name += options.host;
  126. else
  127. name += 'localhost';
  128. name += ':';
  129. if (options.port)
  130. name += options.port;
  131. name += ':';
  132. if (options.localAddress)
  133. name += options.localAddress;
  134. name += ':';
  135. return name;
  136. };
  137. Agent.prototype.addRequest = function(req, options) {
  138. // Legacy API: addRequest(req, host, port, path)
  139. if (typeof options === 'string') {
  140. options = {
  141. host: options,
  142. port: arguments[2],
  143. path: arguments[3]
  144. };
  145. }
  146. options = util._extend({}, options);
  147. options = util._extend(options, this.options);
  148. var name = this.getName(options);
  149. if (!this.sockets[name]) {
  150. this.sockets[name] = [];
  151. }
  152. var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
  153. var sockLen = freeLen + this.sockets[name].length;
  154. if (freeLen) {
  155. // we have a free socket, so use that.
  156. var socket = this.freeSockets[name].shift();
  157. debug('have free socket');
  158. socket.removeListener('error', freeSocketErrorListener);
  159. // restart the default timer
  160. socket.setTimeout(this.timeout);
  161. // don't leak
  162. if (!this.freeSockets[name].length)
  163. delete this.freeSockets[name];
  164. socket.ref && socket.ref();
  165. req.onSocket(socket);
  166. this.sockets[name].push(socket);
  167. } else if (sockLen < this.maxSockets) {
  168. debug('call onSocket', sockLen, freeLen);
  169. // If we are under maxSockets create a new one.
  170. req.onSocket(this.createSocket(req, options));
  171. } else {
  172. debug('wait for socket');
  173. // We are over limit so we'll add it to the queue.
  174. if (!this.requests[name]) {
  175. this.requests[name] = [];
  176. }
  177. this.requests[name].push(req);
  178. }
  179. };
  180. Agent.prototype.createSocket = function(req, options) {
  181. var self = this;
  182. options = util._extend({}, options);
  183. options = util._extend(options, self.options);
  184. if (!options.servername) {
  185. options.servername = options.host;
  186. if (req) {
  187. var hostHeader = req.getHeader('host');
  188. if (hostHeader) {
  189. options.servername = hostHeader.replace(/:.*$/, '');
  190. }
  191. }
  192. }
  193. var name = self.getName(options);
  194. debug('createConnection', name, options);
  195. options.encoding = null;
  196. var s = self.createConnection(options);
  197. if (!self.sockets[name]) {
  198. self.sockets[name] = [];
  199. }
  200. this.sockets[name].push(s);
  201. debug('sockets', name, this.sockets[name].length);
  202. function onFree() {
  203. self.emit('free', s, options);
  204. }
  205. s.on('free', onFree);
  206. function onClose(err) {
  207. debug('CLIENT socket onClose');
  208. // fix: socket.destroyed always be undefined on 0.10.x
  209. if (typeof s.destroyed !== 'boolean') {
  210. s.destroyed = true;
  211. }
  212. // This is the only place where sockets get removed from the Agent.
  213. // If you want to remove a socket from the pool, just close it.
  214. // All socket errors end in a close event anyway.
  215. self.removeSocket(s, options);
  216. self.emit('close');
  217. }
  218. s.on('close', onClose);
  219. function onTimeout() {
  220. debug('CLIENT socket onTimeout');
  221. s.destroy();
  222. // Remove it from freeSockets immediately to prevent new requests from being sent through this socket.
  223. self.removeSocket(s, options);
  224. self.emit('timeout');
  225. }
  226. s.on('timeout', onTimeout);
  227. // set the default timer
  228. s.setTimeout(self.timeout);
  229. function onRemove() {
  230. // We need this function for cases like HTTP 'upgrade'
  231. // (defined by WebSockets) where we need to remove a socket from the
  232. // pool because it'll be locked up indefinitely
  233. debug('CLIENT socket onRemove');
  234. self.removeSocket(s, options);
  235. s.removeListener('close', onClose);
  236. s.removeListener('free', onFree);
  237. s.removeListener('agentRemove', onRemove);
  238. // remove timer
  239. s.setTimeout(0, onTimeout);
  240. }
  241. s.on('agentRemove', onRemove);
  242. return s;
  243. };
  244. Agent.prototype.removeSocket = function(s, options) {
  245. var freeLen, sockLen;
  246. var name = this.getName(options);
  247. debug('removeSocket', name, 'destroyed:', s.destroyed);
  248. var sets = [this.sockets];
  249. // If the socket was destroyed, remove it from the free buffers too.
  250. if (s.destroyed)
  251. sets.push(this.freeSockets);
  252. for (var sk = 0; sk < sets.length; sk++) {
  253. var sockets = sets[sk];
  254. if (sockets[name]) {
  255. var index = sockets[name].indexOf(s);
  256. if (index !== -1) {
  257. sockets[name].splice(index, 1);
  258. // Don't leak
  259. if (sockets[name].length === 0)
  260. delete sockets[name];
  261. }
  262. }
  263. }
  264. freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0;
  265. sockLen = freeLen + this.sockets[name] ? this.sockets[name].length : 0;
  266. if (this.requests[name] && this.requests[name].length && sockLen < this.maxSockets) {
  267. debug('removeSocket, have a request, make a socket');
  268. var req = this.requests[name][0];
  269. // If we have pending requests and a socket gets closed make a new one
  270. this.createSocket(req, options).emit('free');
  271. }
  272. };
  273. Agent.prototype.destroy = function() {
  274. var sets = [this.freeSockets, this.sockets];
  275. for (var s = 0; s < sets.length; s++) {
  276. var set = sets[s];
  277. var keys = Object.keys(set);
  278. for (var v = 0; v < keys.length; v++) {
  279. var setName = set[keys[v]];
  280. for (var n = 0; n < setName.length; n++) {
  281. setName[n].destroy();
  282. }
  283. }
  284. }
  285. };
  286. exports.globalAgent = new Agent();