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