PoolOptions.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.priorityRange=1]
  26. * The range from 1 to be treated as a valid priority
  27. * @param {Boolean} [opts.fifo=true]
  28. * Sets whether the pool has LIFO (last in, first out) behaviour with respect to idle objects.
  29. * if false then pool has FIFO behaviour
  30. * @param {Boolean} [opts.autostart=true]
  31. * Should the pool start creating resources etc once the constructor is called
  32. * @param {Number} [opts.evictionRunIntervalMillis=0]
  33. * How often to run eviction checks. Default: 0 (does not run).
  34. * @param {Number} [opts.numTestsPerEvictionRun=3]
  35. * Number of resources to check each eviction run. Default: 3.
  36. * @param {Number} [opts.softIdleTimeoutMillis=-1]
  37. * amount of time an object may sit idle in the pool before it is eligible
  38. * for eviction by the idle object evictor (if any), with the extra condition
  39. * that at least "min idle" object instances remain in the pool. Default -1 (nothing can get evicted)
  40. * @param {Number} [opts.idleTimeoutMillis=30000]
  41. * the minimum amount of time that an object may sit idle in the pool before it is eligible for eviction
  42. * due to idle time. Supercedes "softIdleTimeoutMillis" Default: 30000
  43. * @param {typeof Promise} [opts.Promise=Promise]
  44. * What promise implementation should the pool use, defaults to native promises.
  45. */
  46. constructor(opts) {
  47. const poolDefaults = new PoolDefaults();
  48. opts = opts || {};
  49. this.fifo = typeof opts.fifo === "boolean" ? opts.fifo : poolDefaults.fifo;
  50. this.priorityRange = opts.priorityRange || poolDefaults.priorityRange;
  51. this.testOnBorrow =
  52. typeof opts.testOnBorrow === "boolean"
  53. ? opts.testOnBorrow
  54. : poolDefaults.testOnBorrow;
  55. this.testOnReturn =
  56. typeof opts.testOnReturn === "boolean"
  57. ? opts.testOnReturn
  58. : poolDefaults.testOnReturn;
  59. this.autostart =
  60. typeof opts.autostart === "boolean"
  61. ? opts.autostart
  62. : poolDefaults.autostart;
  63. if (opts.acquireTimeoutMillis) {
  64. // @ts-ignore
  65. this.acquireTimeoutMillis = parseInt(opts.acquireTimeoutMillis, 10);
  66. }
  67. if (opts.maxWaitingClients !== undefined) {
  68. // @ts-ignore
  69. this.maxWaitingClients = parseInt(opts.maxWaitingClients, 10);
  70. }
  71. // @ts-ignore
  72. this.max = parseInt(opts.max, 10);
  73. // @ts-ignore
  74. this.min = parseInt(opts.min, 10);
  75. this.max = Math.max(isNaN(this.max) ? 1 : this.max, 1);
  76. this.min = Math.min(isNaN(this.min) ? 0 : this.min, this.max);
  77. this.evictionRunIntervalMillis =
  78. opts.evictionRunIntervalMillis || poolDefaults.evictionRunIntervalMillis;
  79. this.numTestsPerEvictionRun =
  80. opts.numTestsPerEvictionRun || poolDefaults.numTestsPerEvictionRun;
  81. this.softIdleTimeoutMillis =
  82. opts.softIdleTimeoutMillis || poolDefaults.softIdleTimeoutMillis;
  83. this.idleTimeoutMillis =
  84. opts.idleTimeoutMillis || poolDefaults.idleTimeoutMillis;
  85. this.Promise = opts.Promise != null ? opts.Promise : poolDefaults.Promise;
  86. }
  87. }
  88. module.exports = PoolOptions;