connection_pool.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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 (Object.prototype.hasOwnProperty.call(options, 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. get waitQueueSize() {
  180. return this[kWaitQueue].length;
  181. }
  182. /**
  183. * Check a connection out of this pool. The connection will continue to be tracked, but no reference to it
  184. * will be held by the pool. This means that if a connection is checked out it MUST be checked back in or
  185. * explicitly destroyed by the new owner.
  186. *
  187. * @param {ConnectionPool~checkOutCallback} callback
  188. */
  189. checkOut(callback) {
  190. this.emit('connectionCheckOutStarted', new ConnectionCheckOutStartedEvent(this));
  191. if (this.closed) {
  192. this.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(this, 'poolClosed'));
  193. callback(new PoolClosedError(this));
  194. return;
  195. }
  196. const waitQueueMember = { callback };
  197. const pool = this;
  198. const waitQueueTimeoutMS = this.options.waitQueueTimeoutMS;
  199. if (waitQueueTimeoutMS) {
  200. waitQueueMember.timer = setTimeout(() => {
  201. waitQueueMember[kCancelled] = true;
  202. waitQueueMember.timer = undefined;
  203. pool.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(pool, 'timeout'));
  204. waitQueueMember.callback(new WaitQueueTimeoutError(pool));
  205. }, waitQueueTimeoutMS);
  206. }
  207. this[kWaitQueue].push(waitQueueMember);
  208. process.nextTick(() => 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. if (!willDestroy) {
  220. connection.markAvailable();
  221. this[kConnections].push(connection);
  222. }
  223. this.emit('connectionCheckedIn', new ConnectionCheckedInEvent(this, connection));
  224. if (willDestroy) {
  225. const reason = connection.closed ? 'error' : poolClosed ? 'poolClosed' : 'stale';
  226. destroyConnection(this, connection, reason);
  227. }
  228. process.nextTick(() => processWaitQueue(this));
  229. }
  230. /**
  231. * Clear the pool
  232. *
  233. * Pool reset is handled by incrementing the pool's generation count. Any existing connection of a
  234. * previous generation will eventually be pruned during subsequent checkouts.
  235. */
  236. clear() {
  237. this[kGeneration] += 1;
  238. this.emit('connectionPoolCleared', new ConnectionPoolClearedEvent(this));
  239. }
  240. /**
  241. * Close the pool
  242. *
  243. * @param {object} [options] Optional settings
  244. * @param {boolean} [options.force] Force close connections
  245. * @param {Function} callback
  246. */
  247. close(options, callback) {
  248. if (typeof options === 'function') {
  249. callback = options;
  250. }
  251. options = Object.assign({ force: false }, options);
  252. if (this.closed) {
  253. return callback();
  254. }
  255. // immediately cancel any in-flight connections
  256. this[kCancellationToken].emit('cancel');
  257. // drain the wait queue
  258. while (this.waitQueueSize) {
  259. const waitQueueMember = this[kWaitQueue].pop();
  260. clearTimeout(waitQueueMember.timer);
  261. if (!waitQueueMember[kCancelled]) {
  262. waitQueueMember.callback(new MongoError('connection pool closed'));
  263. }
  264. }
  265. // clear the min pool size timer
  266. if (this[kMinPoolSizeTimer]) {
  267. clearTimeout(this[kMinPoolSizeTimer]);
  268. }
  269. // end the connection counter
  270. if (typeof this[kConnectionCounter].return === 'function') {
  271. this[kConnectionCounter].return();
  272. }
  273. // mark the pool as closed immediately
  274. this.closed = true;
  275. eachAsync(
  276. this[kConnections].toArray(),
  277. (conn, cb) => {
  278. this.emit('connectionClosed', new ConnectionClosedEvent(this, conn, 'poolClosed'));
  279. conn.destroy(options, cb);
  280. },
  281. err => {
  282. this[kConnections].clear();
  283. this.emit('connectionPoolClosed', new ConnectionPoolClosedEvent(this));
  284. callback(err);
  285. }
  286. );
  287. }
  288. /**
  289. * Runs a lambda with an implicitly checked out connection, checking that connection back in when the lambda
  290. * has completed by calling back.
  291. *
  292. * NOTE: please note the required signature of `fn`
  293. *
  294. * @param {ConnectionPool~withConnectionCallback} fn A function which operates on a managed connection
  295. * @param {Function} callback The original callback
  296. * @return {Promise}
  297. */
  298. withConnection(fn, callback) {
  299. this.checkOut((err, conn) => {
  300. // don't callback with `err` here, we might want to act upon it inside `fn`
  301. fn(err, conn, (fnErr, result) => {
  302. if (typeof callback === 'function') {
  303. if (fnErr) {
  304. callback(fnErr);
  305. } else {
  306. callback(undefined, result);
  307. }
  308. }
  309. if (conn) {
  310. this.checkIn(conn);
  311. }
  312. });
  313. });
  314. }
  315. }
  316. function ensureMinPoolSize(pool) {
  317. if (pool.closed || pool.options.minPoolSize === 0) {
  318. return;
  319. }
  320. const minPoolSize = pool.options.minPoolSize;
  321. for (let i = pool.totalConnectionCount; i < minPoolSize; ++i) {
  322. createConnection(pool);
  323. }
  324. pool[kMinPoolSizeTimer] = setTimeout(() => ensureMinPoolSize(pool), 10);
  325. }
  326. function connectionIsStale(pool, connection) {
  327. return connection.generation !== pool[kGeneration];
  328. }
  329. function connectionIsIdle(pool, connection) {
  330. return !!(pool.options.maxIdleTimeMS && connection.idleTime > pool.options.maxIdleTimeMS);
  331. }
  332. function createConnection(pool, callback) {
  333. const connectOptions = Object.assign(
  334. {
  335. id: pool[kConnectionCounter].next().value,
  336. generation: pool[kGeneration]
  337. },
  338. pool.options
  339. );
  340. pool[kPermits]--;
  341. connect(connectOptions, pool[kCancellationToken], (err, connection) => {
  342. if (err) {
  343. pool[kPermits]++;
  344. pool[kLogger].debug(`connection attempt failed with error [${JSON.stringify(err)}]`);
  345. if (typeof callback === 'function') {
  346. callback(err);
  347. }
  348. return;
  349. }
  350. // The pool might have closed since we started trying to create a connection
  351. if (pool.closed) {
  352. connection.destroy({ force: true });
  353. return;
  354. }
  355. // forward all events from the connection to the pool
  356. relayEvents(connection, pool, [
  357. 'commandStarted',
  358. 'commandFailed',
  359. 'commandSucceeded',
  360. 'clusterTimeReceived'
  361. ]);
  362. pool.emit('connectionCreated', new ConnectionCreatedEvent(pool, connection));
  363. connection.markAvailable();
  364. pool.emit('connectionReady', new ConnectionReadyEvent(pool, connection));
  365. // if a callback has been provided, check out the connection immediately
  366. if (typeof callback === 'function') {
  367. callback(undefined, connection);
  368. return;
  369. }
  370. // otherwise add it to the pool for later acquisition, and try to process the wait queue
  371. pool[kConnections].push(connection);
  372. process.nextTick(() => processWaitQueue(pool));
  373. });
  374. }
  375. function destroyConnection(pool, connection, reason) {
  376. pool.emit('connectionClosed', new ConnectionClosedEvent(pool, connection, reason));
  377. // allow more connections to be created
  378. pool[kPermits]++;
  379. // destroy the connection
  380. process.nextTick(() => connection.destroy());
  381. }
  382. function processWaitQueue(pool) {
  383. if (pool.closed) {
  384. return;
  385. }
  386. while (pool.waitQueueSize) {
  387. const waitQueueMember = pool[kWaitQueue].peekFront();
  388. if (waitQueueMember[kCancelled]) {
  389. pool[kWaitQueue].shift();
  390. continue;
  391. }
  392. if (!pool.availableConnectionCount) {
  393. break;
  394. }
  395. const connection = pool[kConnections].shift();
  396. const isStale = connectionIsStale(pool, connection);
  397. const isIdle = connectionIsIdle(pool, connection);
  398. if (!isStale && !isIdle && !connection.closed) {
  399. pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
  400. clearTimeout(waitQueueMember.timer);
  401. pool[kWaitQueue].shift();
  402. waitQueueMember.callback(undefined, connection);
  403. return;
  404. }
  405. const reason = connection.closed ? 'error' : isStale ? 'stale' : 'idle';
  406. destroyConnection(pool, connection, reason);
  407. }
  408. const maxPoolSize = pool.options.maxPoolSize;
  409. if (pool.waitQueueSize && (maxPoolSize <= 0 || pool.totalConnectionCount < maxPoolSize)) {
  410. createConnection(pool, (err, connection) => {
  411. const waitQueueMember = pool[kWaitQueue].shift();
  412. if (waitQueueMember == null || waitQueueMember[kCancelled]) {
  413. if (err == null) {
  414. pool[kConnections].push(connection);
  415. }
  416. return;
  417. }
  418. if (err) {
  419. pool.emit('connectionCheckOutFailed', new ConnectionCheckOutFailedEvent(pool, err));
  420. } else {
  421. pool.emit('connectionCheckedOut', new ConnectionCheckedOutEvent(pool, connection));
  422. }
  423. clearTimeout(waitQueueMember.timer);
  424. waitQueueMember.callback(err, connection);
  425. });
  426. return;
  427. }
  428. }
  429. /**
  430. * A callback provided to `withConnection`
  431. *
  432. * @callback ConnectionPool~withConnectionCallback
  433. * @param {MongoError} error An error instance representing the error during the execution.
  434. * @param {Connection} connection The managed connection which was checked out of the pool.
  435. * @param {Function} callback A function to call back after connection management is complete
  436. */
  437. /**
  438. * A callback provided to `checkOut`
  439. *
  440. * @callback ConnectionPool~checkOutCallback
  441. * @param {MongoError} error An error instance representing the error during checkout
  442. * @param {Connection} connection A connection from the pool
  443. */
  444. /**
  445. * Emitted once when the connection pool is created
  446. *
  447. * @event ConnectionPool#connectionPoolCreated
  448. * @type {PoolCreatedEvent}
  449. */
  450. /**
  451. * Emitted once when the connection pool is closed
  452. *
  453. * @event ConnectionPool#connectionPoolClosed
  454. * @type {PoolClosedEvent}
  455. */
  456. /**
  457. * Emitted each time a connection is created
  458. *
  459. * @event ConnectionPool#connectionCreated
  460. * @type {ConnectionCreatedEvent}
  461. */
  462. /**
  463. * Emitted when a connection becomes established, and is ready to use
  464. *
  465. * @event ConnectionPool#connectionReady
  466. * @type {ConnectionReadyEvent}
  467. */
  468. /**
  469. * Emitted when a connection is closed
  470. *
  471. * @event ConnectionPool#connectionClosed
  472. * @type {ConnectionClosedEvent}
  473. */
  474. /**
  475. * Emitted when an attempt to check out a connection begins
  476. *
  477. * @event ConnectionPool#connectionCheckOutStarted
  478. * @type {ConnectionCheckOutStartedEvent}
  479. */
  480. /**
  481. * Emitted when an attempt to check out a connection fails
  482. *
  483. * @event ConnectionPool#connectionCheckOutFailed
  484. * @type {ConnectionCheckOutFailedEvent}
  485. */
  486. /**
  487. * Emitted each time a connection is successfully checked out of the connection pool
  488. *
  489. * @event ConnectionPool#connectionCheckedOut
  490. * @type {ConnectionCheckedOutEvent}
  491. */
  492. /**
  493. * Emitted each time a connection is successfully checked into the connection pool
  494. *
  495. * @event ConnectionPool#connectionCheckedIn
  496. * @type {ConnectionCheckedInEvent}
  497. */
  498. /**
  499. * Emitted each time the connection pool is cleared and it's generation incremented
  500. *
  501. * @event ConnectionPool#connectionPoolCleared
  502. * @type {PoolClearedEvent}
  503. */
  504. module.exports = {
  505. ConnectionPool
  506. };