PoolOptions.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. const PoolDefaults = require("./PoolDefaults");
  3. class PoolOptions {
  4. /**
  5. * @param {Object} opts
  6. * configuration for the pool
  7. * @param {Number} [opts.max=null]
  8. * Maximum number of items that can exist at the same time. Default: 1.
  9. * Any further acquire requests will be pushed to the waiting list.
  10. * @param {Number} [opts.min=null]
  11. * Minimum number of items in pool (including in-use). Default: 0.
  12. * When the pool is created, or a resource destroyed, this minimum will
  13. * be checked. If the pool resource count is below the minimum, a new
  14. * resource will be created and added to the pool.
  15. * @param {Number} [opts.maxWaitingClients=null]
  16. * maximum number of queued requests allowed after which acquire calls will be rejected
  17. * @param {Boolean} [opts.testOnBorrow=false]
  18. * should the pool validate resources before giving them to clients. Requires that
  19. * `factory.validate` is specified.
  20. * @param {Boolean} [opts.testOnReturn=false]
  21. * should the pool validate resources before returning them to the pool.
  22. * @param {Number} [opts.acquireTimeoutMillis=null]
  23. * Delay in milliseconds after which the an `acquire` call will fail. optional.
  24. * Default: undefined. Should be positive and non-zero
  25. * @param {Number} [opts.destroyTimeoutMillis=null]
  26. * Delay in milliseconds after which the an `destroy` call will fail, causing it to emit a factoryDestroyError event. optional.
  27. * Default: undefined. Should be positive and non-zero
  28. * @param {Number} [opts.priorityRange=1]
  29. * The range from 1 to be treated as a valid priority
  30. * @param {Boolean} [opts.fifo=true]
  31. * Sets whether the pool has LIFO (last in, first out) behaviour with respect to idle objects.
  32. * if false then pool has FIFO behaviour
  33. * @param {Boolean} [opts.autostart=true]
  34. * Should the pool start creating resources etc once the constructor is called
  35. * @param {Number} [opts.evictionRunIntervalMillis=0]
  36. * How often to run eviction checks. Default: 0 (does not run).
  37. * @param {Number} [opts.numTestsPerEvictionRun=3]
  38. * Number of resources to check each eviction run. Default: 3.
  39. * @param {Number} [opts.softIdleTimeoutMillis=-1]
  40. * amount of time an object may sit idle in the pool before it is eligible
  41. * for eviction by the idle object evictor (if any), with the extra condition
  42. * that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted)
  43. * @param {Number} [opts.idleTimeoutMillis=30000]
  44. * the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction
  45. * due to idle time. Supercedes "softIdleTimeoutMillis" Default: 30000
  46. * @param {typeof Promise} [opts.Promise=Promise]
  47. * What promise implementation should the pool use, defaults to native promises.
  48. */
  49. constructor(opts) {
  50. const poolDefaults = new PoolDefaults();
  51. opts = opts || {};
  52. this.fifo = typeof opts.fifo === "boolean" ? opts.fifo : poolDefaults.fifo;
  53. this.priorityRange = opts.priorityRange || poolDefaults.priorityRange;
  54. this.testOnBorrow =
  55. typeof opts.testOnBorrow === "boolean"
  56. ? opts.testOnBorrow
  57. : poolDefaults.testOnBorrow;
  58. this.testOnReturn =
  59. typeof opts.testOnReturn === "boolean"
  60. ? opts.testOnReturn
  61. : poolDefaults.testOnReturn;
  62. this.autostart =
  63. typeof opts.autostart === "boolean"
  64. ? opts.autostart
  65. : poolDefaults.autostart;
  66. if (opts.acquireTimeoutMillis) {
  67. // @ts-ignore
  68. this.acquireTimeoutMillis = parseInt(opts.acquireTimeoutMillis, 10);
  69. }
  70. if (opts.destroyTimeoutMillis) {
  71. // @ts-ignore
  72. this.destroyTimeoutMillis = parseInt(opts.destroyTimeoutMillis, 10);
  73. }
  74. if (opts.maxWaitingClients !== undefined) {
  75. // @ts-ignore
  76. this.maxWaitingClients = parseInt(opts.maxWaitingClients, 10);
  77. }
  78. // @ts-ignore
  79. this.max = parseInt(opts.max, 10);
  80. // @ts-ignore
  81. this.min = parseInt(opts.min, 10);
  82. this.max = Math.max(isNaN(this.max) ? 1 : this.max, 1);
  83. this.min = Math.min(isNaN(this.min) ? 0 : this.min, this.max);
  84. this.evictionRunIntervalMillis =
  85. opts.evictionRunIntervalMillis || poolDefaults.evictionRunIntervalMillis;
  86. this.numTestsPerEvictionRun =
  87. opts.numTestsPerEvictionRun || poolDefaults.numTestsPerEvictionRun;
  88. this.softIdleTimeoutMillis =
  89. opts.softIdleTimeoutMillis || poolDefaults.softIdleTimeoutMillis;
  90. this.idleTimeoutMillis =
  91. opts.idleTimeoutMillis || poolDefaults.idleTimeoutMillis;
  92. this.Promise = opts.Promise != null ? opts.Promise : poolDefaults.Promise;
  93. }
  94. }
  95. module.exports = PoolOptions;