pool-options.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. let ConnOptions = require('./connection-options');
  3. class PoolOptions {
  4. constructor(opts) {
  5. if (typeof opts === 'string') {
  6. opts = ConnOptions.parse(opts);
  7. //set data type
  8. if (opts.acquireTimeout) opts.acquireTimeout = parseInt(opts.acquireTimeout);
  9. if (opts.connectionLimit) opts.connectionLimit = parseInt(opts.connectionLimit);
  10. if (opts.idleTimeout) opts.idleTimeout = parseInt(opts.idleTimeout);
  11. if (opts.leakDetectionTimeout)
  12. opts.leakDetectionTimeout = parseInt(opts.leakDetectionTimeout);
  13. if (opts.initializationTimeout)
  14. opts.initializationTimeout = parseInt(opts.initializationTimeout);
  15. if (opts.minDelayValidation) opts.minDelayValidation = parseInt(opts.minDelayValidation);
  16. if (opts.minimumIdle) opts.minimumIdle = parseInt(opts.minimumIdle);
  17. if (opts.noControlAfterUse) opts.noControlAfterUse = opts.noControlAfterUse == 'true';
  18. if (opts.resetAfterUse) opts.resetAfterUse = opts.resetAfterUse == 'true';
  19. if (opts.pingTimeout) opts.pingTimeout = parseInt(opts.pingTimeout);
  20. }
  21. this.acquireTimeout = opts.acquireTimeout === undefined ? 10000 : opts.acquireTimeout;
  22. this.connectionLimit = opts.connectionLimit === undefined ? 10 : opts.connectionLimit;
  23. this.idleTimeout = opts.idleTimeout || 1800;
  24. this.leakDetectionTimeout = opts.leakDetectionTimeout || 0;
  25. this.initializationTimeout =
  26. opts.initializationTimeout === undefined ? 30000 : opts.initializationTimeout;
  27. this.minDelayValidation = opts.minDelayValidation === undefined ? 500 : opts.minDelayValidation;
  28. this.minimumIdle =
  29. opts.minimumIdle === undefined
  30. ? this.connectionLimit
  31. : Math.min(opts.minimumIdle, this.connectionLimit);
  32. this.noControlAfterUse = opts.noControlAfterUse || false;
  33. this.resetAfterUse = opts.resetAfterUse === undefined ? true : opts.resetAfterUse;
  34. this.pingTimeout = opts.pingTimeout || 250;
  35. this.connOptions = new ConnOptions(opts);
  36. if (this.acquireTimeout > 0 && this.connOptions.connectTimeout > this.acquireTimeout) {
  37. this.connOptions.connectTimeout = this.acquireTimeout;
  38. }
  39. }
  40. }
  41. module.exports = PoolOptions;