agent.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. 'use strict';
  2. const OriginalAgent = require('http').Agent;
  3. const ms = require('humanize-ms');
  4. const debug = require('debug')('agentkeepalive');
  5. const deprecate = require('depd')('agentkeepalive');
  6. const {
  7. INIT_SOCKET,
  8. CURRENT_ID,
  9. CREATE_ID,
  10. SOCKET_CREATED_TIME,
  11. SOCKET_NAME,
  12. SOCKET_REQUEST_COUNT,
  13. SOCKET_REQUEST_FINISHED_COUNT,
  14. } = require('./constants');
  15. // OriginalAgent come from
  16. // - https://github.com/nodejs/node/blob/v8.12.0/lib/_http_agent.js
  17. // - https://github.com/nodejs/node/blob/v10.12.0/lib/_http_agent.js
  18. // node <= 10
  19. let defaultTimeoutListenerCount = 1;
  20. const majorVersion = parseInt(process.version.split('.', 1)[0].substring(1));
  21. if (majorVersion >= 11 && majorVersion <= 12) {
  22. defaultTimeoutListenerCount = 2;
  23. } else if (majorVersion >= 13) {
  24. defaultTimeoutListenerCount = 3;
  25. }
  26. class Agent extends OriginalAgent {
  27. constructor(options) {
  28. options = options || {};
  29. options.keepAlive = options.keepAlive !== false;
  30. // default is keep-alive and 4s free socket timeout
  31. // see https://medium.com/ssense-tech/reduce-networking-errors-in-nodejs-23b4eb9f2d83
  32. if (options.freeSocketTimeout === undefined) {
  33. options.freeSocketTimeout = 4000;
  34. }
  35. // Legacy API: keepAliveTimeout should be rename to `freeSocketTimeout`
  36. if (options.keepAliveTimeout) {
  37. deprecate('options.keepAliveTimeout is deprecated, please use options.freeSocketTimeout instead');
  38. options.freeSocketTimeout = options.keepAliveTimeout;
  39. delete options.keepAliveTimeout;
  40. }
  41. // Legacy API: freeSocketKeepAliveTimeout should be rename to `freeSocketTimeout`
  42. if (options.freeSocketKeepAliveTimeout) {
  43. deprecate('options.freeSocketKeepAliveTimeout is deprecated, please use options.freeSocketTimeout instead');
  44. options.freeSocketTimeout = options.freeSocketKeepAliveTimeout;
  45. delete options.freeSocketKeepAliveTimeout;
  46. }
  47. // Sets the socket to timeout after timeout milliseconds of inactivity on the socket.
  48. // By default is double free socket timeout.
  49. if (options.timeout === undefined) {
  50. // make sure socket default inactivity timeout >= 8s
  51. options.timeout = Math.max(options.freeSocketTimeout * 2, 8000);
  52. }
  53. // support humanize format
  54. options.timeout = ms(options.timeout);
  55. options.freeSocketTimeout = ms(options.freeSocketTimeout);
  56. options.socketActiveTTL = options.socketActiveTTL ? ms(options.socketActiveTTL) : 0;
  57. super(options);
  58. this[CURRENT_ID] = 0;
  59. // create socket success counter
  60. this.createSocketCount = 0;
  61. this.createSocketCountLastCheck = 0;
  62. this.createSocketErrorCount = 0;
  63. this.createSocketErrorCountLastCheck = 0;
  64. this.closeSocketCount = 0;
  65. this.closeSocketCountLastCheck = 0;
  66. // socket error event count
  67. this.errorSocketCount = 0;
  68. this.errorSocketCountLastCheck = 0;
  69. // request finished counter
  70. this.requestCount = 0;
  71. this.requestCountLastCheck = 0;
  72. // including free socket timeout counter
  73. this.timeoutSocketCount = 0;
  74. this.timeoutSocketCountLastCheck = 0;
  75. this.on('free', socket => {
  76. // https://github.com/nodejs/node/pull/32000
  77. // Node.js native agent will check socket timeout eqs agent.options.timeout.
  78. // Use the ttl or freeSocketTimeout to overwrite.
  79. const timeout = this.calcSocketTimeout(socket);
  80. if (timeout > 0 && socket.timeout !== timeout) {
  81. socket.setTimeout(timeout);
  82. }
  83. });
  84. }
  85. get freeSocketKeepAliveTimeout() {
  86. deprecate('agent.freeSocketKeepAliveTimeout is deprecated, please use agent.options.freeSocketTimeout instead');
  87. return this.options.freeSocketTimeout;
  88. }
  89. get timeout() {
  90. deprecate('agent.timeout is deprecated, please use agent.options.timeout instead');
  91. return this.options.timeout;
  92. }
  93. get socketActiveTTL() {
  94. deprecate('agent.socketActiveTTL is deprecated, please use agent.options.socketActiveTTL instead');
  95. return this.options.socketActiveTTL;
  96. }
  97. calcSocketTimeout(socket) {
  98. /**
  99. * return <= 0: should free socket
  100. * return > 0: should update socket timeout
  101. * return undefined: not find custom timeout
  102. */
  103. let freeSocketTimeout = this.options.freeSocketTimeout;
  104. const socketActiveTTL = this.options.socketActiveTTL;
  105. if (socketActiveTTL) {
  106. // check socketActiveTTL
  107. const aliveTime = Date.now() - socket[SOCKET_CREATED_TIME];
  108. const diff = socketActiveTTL - aliveTime;
  109. if (diff <= 0) {
  110. return diff;
  111. }
  112. if (freeSocketTimeout && diff < freeSocketTimeout) {
  113. freeSocketTimeout = diff;
  114. }
  115. }
  116. // set freeSocketTimeout
  117. if (freeSocketTimeout) {
  118. // set free keepalive timer
  119. // try to use socket custom freeSocketTimeout first, support headers['keep-alive']
  120. // https://github.com/node-modules/urllib/blob/b76053020923f4d99a1c93cf2e16e0c5ba10bacf/lib/urllib.js#L498
  121. const customFreeSocketTimeout = socket.freeSocketTimeout || socket.freeSocketKeepAliveTimeout;
  122. return customFreeSocketTimeout || freeSocketTimeout;
  123. }
  124. }
  125. keepSocketAlive(socket) {
  126. const result = super.keepSocketAlive(socket);
  127. // should not keepAlive, do nothing
  128. if (!result) return result;
  129. const customTimeout = this.calcSocketTimeout(socket);
  130. if (typeof customTimeout === 'undefined') {
  131. return true;
  132. }
  133. if (customTimeout <= 0) {
  134. debug('%s(requests: %s, finished: %s) free but need to destroy by TTL, request count %s, diff is %s',
  135. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT], customTimeout);
  136. return false;
  137. }
  138. if (socket.timeout !== customTimeout) {
  139. socket.setTimeout(customTimeout);
  140. }
  141. return true;
  142. }
  143. // only call on addRequest
  144. reuseSocket(...args) {
  145. // reuseSocket(socket, req)
  146. super.reuseSocket(...args);
  147. const socket = args[0];
  148. const req = args[1];
  149. req.reusedSocket = true;
  150. const agentTimeout = this.options.timeout;
  151. if (getSocketTimeout(socket) !== agentTimeout) {
  152. // reset timeout before use
  153. socket.setTimeout(agentTimeout);
  154. debug('%s reset timeout to %sms', socket[SOCKET_NAME], agentTimeout);
  155. }
  156. socket[SOCKET_REQUEST_COUNT]++;
  157. debug('%s(requests: %s, finished: %s) reuse on addRequest, timeout %sms',
  158. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT],
  159. getSocketTimeout(socket));
  160. }
  161. [CREATE_ID]() {
  162. const id = this[CURRENT_ID]++;
  163. if (this[CURRENT_ID] === Number.MAX_SAFE_INTEGER) this[CURRENT_ID] = 0;
  164. return id;
  165. }
  166. [INIT_SOCKET](socket, options) {
  167. // bugfix here.
  168. // https on node 8, 10 won't set agent.options.timeout by default
  169. // TODO: need to fix on node itself
  170. if (options.timeout) {
  171. const timeout = getSocketTimeout(socket);
  172. if (!timeout) {
  173. socket.setTimeout(options.timeout);
  174. }
  175. }
  176. if (this.options.keepAlive) {
  177. // Disable Nagle's algorithm: http://blog.caustik.com/2012/04/08/scaling-node-js-to-100k-concurrent-connections/
  178. // https://fengmk2.com/benchmark/nagle-algorithm-delayed-ack-mock.html
  179. socket.setNoDelay(true);
  180. }
  181. this.createSocketCount++;
  182. if (this.options.socketActiveTTL) {
  183. socket[SOCKET_CREATED_TIME] = Date.now();
  184. }
  185. // don't show the hole '-----BEGIN CERTIFICATE----' key string
  186. socket[SOCKET_NAME] = `sock[${this[CREATE_ID]()}#${options._agentKey}]`.split('-----BEGIN', 1)[0];
  187. socket[SOCKET_REQUEST_COUNT] = 1;
  188. socket[SOCKET_REQUEST_FINISHED_COUNT] = 0;
  189. installListeners(this, socket, options);
  190. }
  191. createConnection(options, oncreate) {
  192. let called = false;
  193. const onNewCreate = (err, socket) => {
  194. if (called) return;
  195. called = true;
  196. if (err) {
  197. this.createSocketErrorCount++;
  198. return oncreate(err);
  199. }
  200. this[INIT_SOCKET](socket, options);
  201. oncreate(err, socket);
  202. };
  203. const newSocket = super.createConnection(options, onNewCreate);
  204. if (newSocket) onNewCreate(null, newSocket);
  205. }
  206. get statusChanged() {
  207. const changed = this.createSocketCount !== this.createSocketCountLastCheck ||
  208. this.createSocketErrorCount !== this.createSocketErrorCountLastCheck ||
  209. this.closeSocketCount !== this.closeSocketCountLastCheck ||
  210. this.errorSocketCount !== this.errorSocketCountLastCheck ||
  211. this.timeoutSocketCount !== this.timeoutSocketCountLastCheck ||
  212. this.requestCount !== this.requestCountLastCheck;
  213. if (changed) {
  214. this.createSocketCountLastCheck = this.createSocketCount;
  215. this.createSocketErrorCountLastCheck = this.createSocketErrorCount;
  216. this.closeSocketCountLastCheck = this.closeSocketCount;
  217. this.errorSocketCountLastCheck = this.errorSocketCount;
  218. this.timeoutSocketCountLastCheck = this.timeoutSocketCount;
  219. this.requestCountLastCheck = this.requestCount;
  220. }
  221. return changed;
  222. }
  223. getCurrentStatus() {
  224. return {
  225. createSocketCount: this.createSocketCount,
  226. createSocketErrorCount: this.createSocketErrorCount,
  227. closeSocketCount: this.closeSocketCount,
  228. errorSocketCount: this.errorSocketCount,
  229. timeoutSocketCount: this.timeoutSocketCount,
  230. requestCount: this.requestCount,
  231. freeSockets: inspect(this.freeSockets),
  232. sockets: inspect(this.sockets),
  233. requests: inspect(this.requests),
  234. };
  235. }
  236. }
  237. // node 8 don't has timeout attribute on socket
  238. // https://github.com/nodejs/node/pull/21204/files#diff-e6ef024c3775d787c38487a6309e491dR408
  239. function getSocketTimeout(socket) {
  240. return socket.timeout || socket._idleTimeout;
  241. }
  242. function installListeners(agent, socket, options) {
  243. debug('%s create, timeout %sms', socket[SOCKET_NAME], getSocketTimeout(socket));
  244. // listener socket events: close, timeout, error, free
  245. function onFree() {
  246. // create and socket.emit('free') logic
  247. // https://github.com/nodejs/node/blob/master/lib/_http_agent.js#L311
  248. // no req on the socket, it should be the new socket
  249. if (!socket._httpMessage && socket[SOCKET_REQUEST_COUNT] === 1) return;
  250. socket[SOCKET_REQUEST_FINISHED_COUNT]++;
  251. agent.requestCount++;
  252. debug('%s(requests: %s, finished: %s) free',
  253. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT]);
  254. // should reuse on pedding requests?
  255. const name = agent.getName(options);
  256. if (socket.writable && agent.requests[name] && agent.requests[name].length) {
  257. // will be reuse on agent free listener
  258. socket[SOCKET_REQUEST_COUNT]++;
  259. debug('%s(requests: %s, finished: %s) will be reuse on agent free event',
  260. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT]);
  261. }
  262. }
  263. socket.on('free', onFree);
  264. function onClose(isError) {
  265. debug('%s(requests: %s, finished: %s) close, isError: %s',
  266. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT], isError);
  267. agent.closeSocketCount++;
  268. }
  269. socket.on('close', onClose);
  270. // start socket timeout handler
  271. function onTimeout() {
  272. // onTimeout and emitRequestTimeout(_http_client.js)
  273. // https://github.com/nodejs/node/blob/v12.x/lib/_http_client.js#L711
  274. const listenerCount = socket.listeners('timeout').length;
  275. // node <= 10, default listenerCount is 1, onTimeout
  276. // 11 < node <= 12, default listenerCount is 2, onTimeout and emitRequestTimeout
  277. // node >= 13, default listenerCount is 3, onTimeout,
  278. // onTimeout(https://github.com/nodejs/node/pull/32000/files#diff-5f7fb0850412c6be189faeddea6c5359R333)
  279. // and emitRequestTimeout
  280. const timeout = getSocketTimeout(socket);
  281. const req = socket._httpMessage;
  282. const reqTimeoutListenerCount = req && req.listeners('timeout').length || 0;
  283. debug('%s(requests: %s, finished: %s) timeout after %sms, listeners %s, defaultTimeoutListenerCount %s, hasHttpRequest %s, HttpRequest timeoutListenerCount %s',
  284. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT],
  285. timeout, listenerCount, defaultTimeoutListenerCount, !!req, reqTimeoutListenerCount);
  286. if (debug.enabled) {
  287. debug('timeout listeners: %s', socket.listeners('timeout').map(f => f.name).join(', '));
  288. }
  289. agent.timeoutSocketCount++;
  290. const name = agent.getName(options);
  291. if (agent.freeSockets[name] && agent.freeSockets[name].indexOf(socket) !== -1) {
  292. // free socket timeout, destroy quietly
  293. socket.destroy();
  294. // Remove it from freeSockets list immediately to prevent new requests
  295. // from being sent through this socket.
  296. agent.removeSocket(socket, options);
  297. debug('%s is free, destroy quietly', socket[SOCKET_NAME]);
  298. } else {
  299. // if there is no any request socket timeout handler,
  300. // agent need to handle socket timeout itself.
  301. //
  302. // custom request socket timeout handle logic must follow these rules:
  303. // 1. Destroy socket first
  304. // 2. Must emit socket 'agentRemove' event tell agent remove socket
  305. // from freeSockets list immediately.
  306. // Otherise you may be get 'socket hang up' error when reuse
  307. // free socket and timeout happen in the same time.
  308. if (reqTimeoutListenerCount === 0) {
  309. const error = new Error('Socket timeout');
  310. error.code = 'ERR_SOCKET_TIMEOUT';
  311. error.timeout = timeout;
  312. // must manually call socket.end() or socket.destroy() to end the connection.
  313. // https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_settimeout_timeout_callback
  314. socket.destroy(error);
  315. agent.removeSocket(socket, options);
  316. debug('%s destroy with timeout error', socket[SOCKET_NAME]);
  317. }
  318. }
  319. }
  320. socket.on('timeout', onTimeout);
  321. function onError(err) {
  322. const listenerCount = socket.listeners('error').length;
  323. debug('%s(requests: %s, finished: %s) error: %s, listenerCount: %s',
  324. socket[SOCKET_NAME], socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT],
  325. err, listenerCount);
  326. agent.errorSocketCount++;
  327. if (listenerCount === 1) {
  328. // if socket don't contain error event handler, don't catch it, emit it again
  329. debug('%s emit uncaught error event', socket[SOCKET_NAME]);
  330. socket.removeListener('error', onError);
  331. socket.emit('error', err);
  332. }
  333. }
  334. socket.on('error', onError);
  335. function onRemove() {
  336. debug('%s(requests: %s, finished: %s) agentRemove',
  337. socket[SOCKET_NAME],
  338. socket[SOCKET_REQUEST_COUNT], socket[SOCKET_REQUEST_FINISHED_COUNT]);
  339. // We need this function for cases like HTTP 'upgrade'
  340. // (defined by WebSockets) where we need to remove a socket from the
  341. // pool because it'll be locked up indefinitely
  342. socket.removeListener('close', onClose);
  343. socket.removeListener('error', onError);
  344. socket.removeListener('free', onFree);
  345. socket.removeListener('timeout', onTimeout);
  346. socket.removeListener('agentRemove', onRemove);
  347. }
  348. socket.on('agentRemove', onRemove);
  349. }
  350. module.exports = Agent;
  351. function inspect(obj) {
  352. const res = {};
  353. for (const key in obj) {
  354. res[key] = obj[key].length;
  355. }
  356. return res;
  357. }