connection_pool.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. 'use strict';
  2. const Denque = require('denque');
  3. const EventEmitter = require('events').EventEmitter;
  4. const Logger = require('../core/connection/logger');
  5. const makeCounter = require('../utils').makeCounter;
  6. const MongoError = require('../core/error').MongoError;
  7. const Connection = require('./connection').Connection;
  8. const eachAsync = require('../core/utils').eachAsync;
  9. const connect = require('../core/connection/connect');
  10. const relayEvents = require('../core/utils').relayEvents;
  11. const errors = require('./errors');
  12. const PoolClosedError = errors.PoolClosedError;
  13. const WaitQueueTimeoutError = errors.WaitQueueTimeoutError;
  14. const events = require('./events');
  15. const ConnectionPoolCreatedEvent = events.ConnectionPoolCreatedEvent;
  16. const ConnectionPoolClosedEvent = events.ConnectionPoolClosedEvent;
  17. const ConnectionCreatedEvent = events.ConnectionCreatedEvent;
  18. const ConnectionReadyEvent = events.ConnectionReadyEvent;
  19. const ConnectionClosedEvent = events.ConnectionClosedEvent;
  20. const ConnectionCheckOutStartedEvent = events.ConnectionCheckOutStartedEvent;
  21. const ConnectionCheckOutFailedEvent = events.ConnectionCheckOutFailedEvent;
  22. const ConnectionCheckedOutEvent = events.ConnectionCheckedOutEvent;
  23. const ConnectionCheckedInEvent = events.ConnectionCheckedInEvent;
  24. const ConnectionPoolClearedEvent = events.ConnectionPoolClearedEvent;
  25. const kLogger = Symbol('logger');
  26. const kConnections = Symbol('connections');
  27. const kPermits = Symbol('permits');
  28. const kMinPoolSizeTimer = Symbol('minPoolSizeTimer');
  29. const kGeneration = Symbol('generation');
  30. const kConnectionCounter = Symbol('connectionCounter');
  31. const kCancellationToken = Symbol('cancellationToken');
  32. const kWaitQueue = Symbol('waitQueue');
  33. const kCancelled = Symbol('cancelled');
  34. const VALID_POOL_OPTIONS = new Set([
  35. // `connect` options
  36. 'ssl',
  37. 'bson',
  38. 'connectionType',
  39. 'monitorCommands',
  40. 'socketTimeout',
  41. 'credentials',
  42. 'compression',
  43. // node Net options
  44. 'host',
  45. 'port',
  46. 'localAddress',
  47. 'localPort',
  48. 'family',
  49. 'hints',
  50. 'lookup',
  51. 'path',
  52. // node TLS options
  53. 'ca',
  54. 'cert',
  55. 'sigalgs',
  56. 'ciphers',
  57. 'clientCertEngine',
  58. 'crl',
  59. 'dhparam',
  60. 'ecdhCurve',
  61. 'honorCipherOrder',
  62. 'key',
  63. 'privateKeyEngine',
  64. 'privateKeyIdentifier',
  65. 'maxVersion',
  66. 'minVersion',
  67. 'passphrase',
  68. 'pfx',
  69. 'secureOptions',
  70. 'secureProtocol',
  71. 'sessionIdContext',
  72. 'allowHalfOpen',
  73. 'rejectUnauthorized',
  74. 'pskCallback',
  75. 'ALPNProtocols',
  76. 'servername',
  77. 'checkServerIdentity',
  78. 'session',
  79. 'minDHSize',
  80. 'secureContext',
  81. // spec options
  82. 'maxPoolSize',
  83. 'minPoolSize',
  84. 'maxIdleTimeMS',
  85. 'waitQueueTimeoutMS'
  86. ]);
  87. function resolveOptions(options, defaults) {
  88. const newOptions = Array.from(VALID_POOL_OPTIONS).reduce((obj, key) => {
  89. if (options.hasOwnProperty(key)) {
  90. obj[key] = options[key];
  91. }
  92. return obj;
  93. }, {});
  94. return Object.freeze(Object.assign({}, defaults, newOptions));
  95. }
  96. /**
  97. * Configuration options for drivers wrapping the node driver.
  98. *
  99. * @typedef {Object} ConnectionPoolOptions
  100. * @property
  101. * @property {string} [host] The host to connect to
  102. * @property {number} [port] The port to connect to
  103. * @property {bson} [bson] The BSON instance to use for new connections
  104. * @property {number} [maxPoolSize=100] The maximum number of connections that may be associated with a pool at a given time. This includes in use and available connections.
  105. * @property {number} [minPoolSize=0] The minimum number of connections that MUST exist at any moment in a single connection pool.
  106. * @property {number} [maxIdleTimeMS] The maximum amount of time a connection should remain idle in the connection pool before being marked idle.
  107. * @property {number} [waitQueueTimeoutMS=0] The maximum amount of time operation execution should wait for a connection to become available. The default is 0 which means there is no limit.
  108. */
  109. /**
  110. * A pool of connections which dynamically resizes, and emit events related to pool activity
  111. *
  112. * @property {number} generation An integer representing the SDAM generation of the pool
  113. * @property {number} totalConnectionCount An integer expressing how many total connections (active + in use) the pool currently has
  114. * @property {number} availableConnectionCount An integer expressing how many connections are currently available in the pool.
  115. * @property {string} address The address of the endpoint the pool is connected to
  116. *
  117. * @emits ConnectionPool#connectionPoolCreated
  118. * @emits ConnectionPool#connectionPoolClosed
  119. * @emits ConnectionPool#connectionCreated
  120. * @emits ConnectionPool#connectionReady
  121. * @emits ConnectionPool#connectionClosed
  122. * @emits ConnectionPool#connectionCheckOutStarted
  123. * @emits ConnectionPool#connectionCheckOutFailed
  124. * @emits ConnectionPool#connectionCheckedOut
  125. * @emits ConnectionPool#connectionCheckedIn
  126. * @emits ConnectionPool#connectionPoolCleared
  127. */
  128. class ConnectionPool extends EventEmitter {
  129. /**
  130. * Create a new Connection Pool
  131. *
  132. * @param {ConnectionPoolOptions} options
  133. */
  134. constructor(options) {
  135. super();
  136. options = options || {};
  137. this.closed = false;
  138. this.options = resolveOptions(options, {
  139. connectionType: Connection,
  140. maxPoolSize: typeof options.maxPoolSize === 'number' ? options.maxPoolSize : 100,
  141. minPoolSize: typeof options.minPoolSize === 'number' ? options.minPoolSize : 0,
  142. maxIdleTimeMS: typeof options.maxIdleTimeMS === 'number' ? options.maxIdleTimeMS : 0,
  143. waitQueueTimeoutMS:
  144. typeof options.waitQueueTimeoutMS === 'number' ? options.waitQueueTimeoutMS : 0,
  145. autoEncrypter: options.autoEncrypter,
  146. metadata: options.metadata
  147. });
  148. if (options.minSize > options.maxSize) {
  149. throw new TypeError(
  150. 'Connection pool minimum size must not be greater than maxiumum pool size'
  151. );
  152. }
  153. this[kLogger] = Logger('ConnectionPool', options);
  154. this[kConnections] = new Denque();
  155. this[kPermits] = this.options.maxPoolSize;
  156. this[kMinPoolSizeTimer] = undefined;
  157. this[kGeneration] = 0;
  158. this[kConnectionCounter] = makeCounter(1);
  159. this[kCancellationToken] = new EventEmitter();
  160. this[kCancellationToken].setMaxListeners(Infinity);
  161. this[kWaitQueue] = new Denque();
  162. process.nextTick(() => {
  163. this.emit('connectionPoolCreated', new ConnectionPoolCreatedEvent(this));
  164. ensureMinPoolSize(this);
  165. });
  166. }
  167. get address() {
  168. return `${this.options.host}:${this.options.port}`;
  169. }
  170. get generation() {
  171. return this[kGeneration];
  172. }
  173. get totalConnectionCount() {
  174. return this[kConnections].length + (this.options.maxPoolSize - this[kPermits]);
  175. }
  176. get availableConnectionCount() {
  177. return this[kConnections].length;
  178. }
  179. /**
  180. * Check a connection out of this pool. The connection will continue to be tracked, but no reference to it
  181. * will be held by the pool. This means that if a connection is checked out it MUST be checked back in or
  182. * explicitly destroyed by the new owner.
  183. *
  184. * @param {ConnectionPool~checkOutCallback} callback
  185. */
  186. checkOut(callback) {
  187. this.emit('connectionCheckOutStarted', new ConnectionCheckOutStartedEvent(this));
  188. if (this.closed) {
  189. this.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(this, 'poolClosed'));
  190. callback(new PoolClosedError(this));
  191. return;
  192. }
  193. // add this request to the wait queue
  194. const waitQueueMember = { callback };
  195. const pool = this;
  196. const waitQueueTimeoutMS = this.options.waitQueueTimeoutMS;
  197. if (waitQueueTimeoutMS) {
  198. waitQueueMember.timer = setTimeout(() => {
  199. waitQueueMember[kCancelled] = true;
  200. waitQueueMember.timer = undefined;
  201. pool.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(pool, 'timeout'));
  202. waitQueueMember.callback(new WaitQueueTimeoutError(pool));
  203. }, waitQueueTimeoutMS);
  204. }
  205. // place the member at the end of the wait queue
  206. this[kWaitQueue].push(waitQueueMember);
  207. // process the wait queue
  208. processWaitQueue(this);
  209. }
  210. /**
  211. * Check a connection into the pool.
  212. *
  213. * @param {Connection} connection The connection to check in
  214. */
  215. checkIn(connection) {
  216. const poolClosed = this.closed;
  217. const stale = connectionIsStale(this, connection);
  218. const willDestroy = !!(poolClosed || stale || connection.closed);
  219. // Properly adjust state of connection
  220. if (!willDestroy) {
  221. connection.markAvailable();
  222. this[kConnections].push(connection);
  223. }
  224. this.emit('connectionCheckedIn', new ConnectionCheckedInEvent(this, connection));
  225. if (willDestroy) {
  226. const reason = connection.closed ? 'error' : poolClosed ? 'poolClosed' : 'stale';
  227. destroyConnection(this, connection, reason);
  228. }
  229. processWaitQueue(this);
  230. }
  231. /**
  232. * Clear the pool
  233. *
  234. * Pool reset is handled by incrementing the pool's generation count. Any existing connection of a
  235. * previous generation will eventually be pruned during subsequent checkouts.
  236. */
  237. clear() {
  238. this[kGeneration] += 1;
  239. this.emit('connectionPoolCleared', new ConnectionPoolClearedEvent(this));
  240. }
  241. /**
  242. * Close the pool
  243. *
  244. * @param {object} [options] Optional settings
  245. * @param {boolean} [options.force] Force close connections
  246. * @param {Function} callback
  247. */
  248. close(options, callback) {
  249. if (typeof options === 'function') {
  250. callback = options;
  251. }
  252. options = Object.assign({ force: false }, options);
  253. if (this.closed) {
  254. return callback();
  255. }
  256. // immediately cancel any in-flight connections
  257. this[kCancellationToken].emit('cancel');
  258. // drain the wait queue
  259. while (this[kWaitQueue].length) {
  260. const waitQueueMember = this[kWaitQueue].pop();
  261. clearTimeout(waitQueueMember.timer);
  262. if (!waitQueueMember[kCancelled]) {
  263. waitQueueMember.callback(new MongoError('connection pool closed'));
  264. }
  265. }
  266. // clear the min pool size timer
  267. if (this[kMinPoolSizeTimer]) {
  268. clearTimeout(this[kMinPoolSizeTimer]);
  269. }
  270. // end the connection counter
  271. if (typeof this[kConnectionCounter].return === 'function') {
  272. this[kConnectionCounter].return();
  273. }
  274. // mark the pool as closed immediately
  275. this.closed = true;
  276. eachAsync(
  277. this[kConnections].toArray(),
  278. (conn, cb) => {
  279. this.emit('connectionClosed', new ConnectionClosedEvent(this, conn, 'poolClosed'));
  280. conn.destroy(options, cb);
  281. },
  282. err => {
  283. this[kConnections].clear();
  284. this.emit('connectionPoolClosed', new ConnectionPoolClosedEvent(this));
  285. callback(err);
  286. }
  287. );
  288. }
  289. /**
  290. * Runs a lambda with an implicitly checked out connection, checking that connection back in when the lambda
  291. * has completed by calling back.
  292. *
  293. * NOTE: please note the required signature of `fn`
  294. *
  295. * @param {ConnectionPool~withConnectionCallback} fn A function which operates on a managed connection
  296. * @param {Function} callback The original callback
  297. * @return {Promise}
  298. */
  299. withConnection(fn, callback) {
  300. this.checkOut((err, conn) => {
  301. // don't callback with `err` here, we might want to act upon it inside `fn`
  302. fn(err, conn, (fnErr, result) => {
  303. if (typeof callback === 'function') {
  304. if (fnErr) {
  305. callback(fnErr);
  306. } else {
  307. callback(undefined, result);
  308. }
  309. }
  310. if (conn) {
  311. this.checkIn(conn);
  312. }
  313. });
  314. });
  315. }
  316. }
  317. function ensureMinPoolSize(pool) {
  318. if (pool.closed || pool.options.minPoolSize === 0) {
  319. return;
  320. }
  321. const minPoolSize = pool.options.minPoolSize;
  322. for (let i = pool.totalConnectionCount; i < minPoolSize; ++i) {
  323. createConnection(pool);
  324. }
  325. pool[kMinPoolSizeTimer] = setTimeout(() => ensureMinPoolSize(pool), 10);
  326. }
  327. function connectionIsStale(pool, connection) {
  328. return connection.generation !== pool[kGeneration];
  329. }
  330. function connectionIsIdle(pool, connection) {
  331. return !!(pool.options.maxIdleTimeMS && connection.idleTime > pool.options.maxIdleTimeMS);
  332. }
  333. function createConnection(pool, callback) {
  334. const connectOptions = Object.assign(
  335. {
  336. id: pool[kConnectionCounter].next().value,
  337. generation: pool[kGeneration]
  338. },
  339. pool.options
  340. );
  341. pool[kPermits]--;
  342. connect(connectOptions, pool[kCancellationToken], (err, connection) => {
  343. if (err) {
  344. pool[kPermits]++;
  345. pool[kLogger].debug(`connection attempt failed with error [${JSON.stringify(err)}]`);
  346. if (typeof callback === 'function') {
  347. callback(err);
  348. }
  349. return;
  350. }
  351. // The pool might have closed since we started trying to create a connection
  352. if (pool.closed) {
  353. connection.destroy({ force: true });
  354. return;
  355. }
  356. // forward all events from the connection to the pool
  357. relayEvents(connection, pool, [
  358. 'commandStarted',
  359. 'commandFailed',
  360. 'commandSucceeded',
  361. 'clusterTimeReceived'
  362. ]);
  363. pool.emit('connectionCreated', new ConnectionCreatedEvent(pool, connection));
  364. connection.markAvailable();
  365. pool.emit('connectionReady', new ConnectionReadyEvent(pool, connection));
  366. // if a callback has been provided, check out the connection immediately
  367. if (typeof callback === 'function') {
  368. callback(undefined, connection);
  369. return;
  370. }
  371. // otherwise add it to the pool for later acquisition, and try to process the wait queue
  372. pool[kConnections].push(connection);
  373. processWaitQueue(pool);
  374. });
  375. }
  376. function destroyConnection(pool, connection, reason) {
  377. pool.emit('connectionClosed', new ConnectionClosedEvent(pool, connection, reason));
  378. // allow more connections to be created
  379. pool[kPermits]++;
  380. // destroy the connection
  381. process.nextTick(() => connection.destroy());
  382. }
  383. function processWaitQueue(pool) {
  384. if (pool.closed) {
  385. return;
  386. }
  387. while (pool[kWaitQueue].length && pool.availableConnectionCount) {
  388. const waitQueueMember = pool[kWaitQueue].peekFront();
  389. if (waitQueueMember[kCancelled]) {
  390. pool[kWaitQueue].shift();
  391. continue;
  392. }
  393. const connection = pool[kConnections].shift();
  394. const isStale = connectionIsStale(pool, connection);
  395. const isIdle = connectionIsIdle(pool, connection);
  396. if (!isStale && !isIdle && !connection.closed) {
  397. pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
  398. clearTimeout(waitQueueMember.timer);
  399. pool[kWaitQueue].shift();
  400. waitQueueMember.callback(undefined, connection);
  401. return;
  402. }
  403. const reason = connection.closed ? 'error' : isStale ? 'stale' : 'idle';
  404. destroyConnection(pool, connection, reason);
  405. }
  406. const maxPoolSize = pool.options.maxPoolSize;
  407. if (pool[kWaitQueue].length && (maxPoolSize <= 0 || pool.totalConnectionCount < maxPoolSize)) {
  408. createConnection(pool, (err, connection) => {
  409. const waitQueueMember = pool[kWaitQueue].shift();
  410. if (waitQueueMember == null) {
  411. if (err == null) {
  412. pool[kConnections].push(connection);
  413. }
  414. return;
  415. }
  416. if (waitQueueMember[kCancelled]) {
  417. return;
  418. }
  419. if (err) {
  420. pool.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(pool, err));
  421. } else {
  422. pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
  423. }
  424. clearTimeout(waitQueueMember.timer);
  425. waitQueueMember.callback(err, connection);
  426. });
  427. return;
  428. }
  429. }
  430. /**
  431. * A callback provided to `withConnection`
  432. *
  433. * @callback ConnectionPool~withConnectionCallback
  434. * @param {MongoError} error An error instance representing the error during the execution.
  435. * @param {Connection} connection The managed connection which was checked out of the pool.
  436. * @param {Function} callback A function to call back after connection management is complete
  437. */
  438. /**
  439. * A callback provided to `checkOut`
  440. *
  441. * @callback ConnectionPool~checkOutCallback
  442. * @param {MongoError} error An error instance representing the error during checkout
  443. * @param {Connection} connection A connection from the pool
  444. */
  445. /**
  446. * Emitted once when the connection pool is created
  447. *
  448. * @event ConnectionPool#connectionPoolCreated
  449. * @type {PoolCreatedEvent}
  450. */
  451. /**
  452. * Emitted once when the connection pool is closed
  453. *
  454. * @event ConnectionPool#connectionPoolClosed
  455. * @type {PoolClosedEvent}
  456. */
  457. /**
  458. * Emitted each time a connection is created
  459. *
  460. * @event ConnectionPool#connectionCreated
  461. * @type {ConnectionCreatedEvent}
  462. */
  463. /**
  464. * Emitted when a connection becomes established, and is ready to use
  465. *
  466. * @event ConnectionPool#connectionReady
  467. * @type {ConnectionReadyEvent}
  468. */
  469. /**
  470. * Emitted when a connection is closed
  471. *
  472. * @event ConnectionPool#connectionClosed
  473. * @type {ConnectionClosedEvent}
  474. */
  475. /**
  476. * Emitted when an attempt to check out a connection begins
  477. *
  478. * @event ConnectionPool#connectionCheckOutStarted
  479. * @type {ConnectionCheckOutStartedEvent}
  480. */
  481. /**
  482. * Emitted when an attempt to check out a connection fails
  483. *
  484. * @event ConnectionPool#connectionCheckOutFailed
  485. * @type {ConnectionCheckOutFailedEvent}
  486. */
  487. /**
  488. * Emitted each time a connection is successfully checked out of the connection pool
  489. *
  490. * @event ConnectionPool#connectionCheckedOut
  491. * @type {ConnectionCheckedOutEvent}
  492. */
  493. /**
  494. * Emitted each time a connection is successfully checked into the connection pool
  495. *
  496. * @event ConnectionPool#connectionCheckedIn
  497. * @type {ConnectionCheckedInEvent}
  498. */
  499. /**
  500. * Emitted each time the connection pool is cleared and it's generation incremented
  501. *
  502. * @event ConnectionPool#connectionPoolCleared
  503. * @type {PoolClearedEvent}
  504. */
  505. module.exports = {
  506. ConnectionPool
  507. };