.
This commit is contained in:
22
qwen/nodejs/node_modules/sequelize-pool/CHANGELOG.md
generated
vendored
Normal file
22
qwen/nodejs/node_modules/sequelize-pool/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
# Changelog
|
||||
|
||||
## 7.x
|
||||
|
||||
- breaking: Support only `Node >= 10`
|
||||
- fix: `acquire` not resolving after destroying available resources
|
||||
|
||||
## 6.0.0
|
||||
|
||||
- change: `destory` (and `destoryAllNow`) are async now, they wait for `factory.destory`
|
||||
|
||||
## 5.0.0
|
||||
|
||||
- Typescript conversion. API is unchanged.
|
||||
|
||||
## 4.0.0
|
||||
|
||||
- Flow typed code. API is unchanged.
|
||||
|
||||
## v3.1.0
|
||||
|
||||
- added `maxUses` options [#18](https://github.com/sequelize/sequelize-pool/pull/18)
|
||||
50
qwen/nodejs/node_modules/sequelize-pool/LICENSE
generated
vendored
Normal file
50
qwen/nodejs/node_modules/sequelize-pool/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2018-present Sushant <sushantdhiman@outlook.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
--------------------------------
|
||||
|
||||
(Original Fork License)
|
||||
|
||||
[generic-pool@2.5]
|
||||
|
||||
Copyright (c) 2010-2016 James Cooper <james@bitmechanic.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
149
qwen/nodejs/node_modules/sequelize-pool/README.md
generated
vendored
Normal file
149
qwen/nodejs/node_modules/sequelize-pool/README.md
generated
vendored
Normal file
@@ -0,0 +1,149 @@
|
||||
# Sequelize Pool
|
||||
|
||||
[](https://www.npmjs.com/package/sequelize-pool)
|
||||
[](https://github.com/sequelize/sequelize-pool/actions)
|
||||
|
||||
Resource pool implementation. It can be used to throttle expensive resources.
|
||||
|
||||
**Note:**
|
||||
This is a fork from [generic-pool@v2.5](https://github.com/coopernurse/node-pool/tree/v2.5).
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm i sequelize-pool
|
||||
```
|
||||
|
||||
## API Documentation
|
||||
|
||||
You can find full API documentation in [docs/README.md](docs/README.md)
|
||||
|
||||
## Example
|
||||
|
||||
### Step 1 - Create pool using a factory object
|
||||
|
||||
```js
|
||||
// Create a MySQL connection pool
|
||||
var Pool = require('sequelize-pool').Pool;
|
||||
var mysql2 = require('mysql2/promise');
|
||||
|
||||
var pool = new Pool({
|
||||
name: 'mysql',
|
||||
create: async () => {
|
||||
// create a new connection
|
||||
// return as a promise
|
||||
return mysql2.createConnection({
|
||||
user: 'scott',
|
||||
password: 'tiger',
|
||||
database: 'mydb',
|
||||
});
|
||||
},
|
||||
destroy: (connection) => {
|
||||
// this function should destroy connection. Pool waits for promise (if returned).
|
||||
// connection is removed from pool and this method is called and awaited for.
|
||||
connection.end();
|
||||
},
|
||||
validate: (connection) => connection.closed !== true,
|
||||
max: 5,
|
||||
min: 0,
|
||||
});
|
||||
```
|
||||
|
||||
### Step 2 - Use pool in your code to acquire/release resources
|
||||
|
||||
```js
|
||||
// acquire connection
|
||||
(async () => {
|
||||
// Get new connection from pool.
|
||||
// This method can throw TimeoutError if connection was not created in
|
||||
// specified `factory.acquireTimeoutMillis` time.
|
||||
const connection = await pool.acquire();
|
||||
|
||||
const result = connection.query('select * from foo');
|
||||
|
||||
// return connection back to pool so it can be reused
|
||||
pool.release(connection);
|
||||
})();
|
||||
```
|
||||
|
||||
### Step 3 - Drain pool during shutdown (optional)
|
||||
|
||||
If you are shutting down a long-lived process, you may notice
|
||||
that node fails to exit for 30 seconds or so. This is a side
|
||||
effect of the `idleTimeoutMillis` behaviour -- the pool has a
|
||||
`setTimeout()` call registered that is in the event loop queue, so
|
||||
node won't terminate until all resources have timed out, and the pool
|
||||
stops trying to manage them.
|
||||
|
||||
This behavior will be more problematic when you set `factory.min > 0`,
|
||||
as the pool will never become empty, and the `setTimeout` calls will
|
||||
never end.
|
||||
|
||||
In these cases, use the `pool.drain()` function. This sets the pool
|
||||
into a "draining" state which will gracefully wait until all
|
||||
idle resources have timed out. For example, you can call:
|
||||
|
||||
```js
|
||||
// Only call this once in your application -- at the point you want
|
||||
// to shutdown and stop using this pool.
|
||||
pool.drain().then(() => pool.destroyAllNow());
|
||||
```
|
||||
|
||||
If you do this, your node process will exit gracefully.
|
||||
|
||||
## Draining
|
||||
|
||||
If you know would like to terminate all the resources in your pool before
|
||||
their timeouts have been reached, you can use `destroyAllNow()` in conjunction
|
||||
with `drain()`:
|
||||
|
||||
```js
|
||||
pool.drain().then(() => pool.destroyAllNow());
|
||||
```
|
||||
|
||||
One side-effect of calling `drain()` is that subsequent calls to `acquire()`
|
||||
will throw an Error.
|
||||
|
||||
## Using `maxUses` option
|
||||
|
||||
Imagine a scenario where you have 10 app servers (hosting an API) that each connect to a read-replica set of 3 members, accessible behind a DNS name that round-robins IPs for the 3 replicas. Each app server rus a connection pool of 25 connections.
|
||||
|
||||
You start your app servers with an ambient traffic load of 50 http requests per second, and the connection pools likely fill up in a minute or two. Everything is great at this point.
|
||||
|
||||
But when you hit weekly traffic peaks, you might reach up to 1,000 http requests per second. If you have a DB with elastic read replicas, you might quickly add 10 more read replicas during this peak time and scale them back down during slower times of the week in order to reduce cost and avoid the additional replication lag you might see with larger numbers or read replicas.
|
||||
|
||||
When you add these 10 read replicas, assuming the first 3 remain healthy, the connection pool with not inherently adopt these new replicas because the pools are full and the connections are healthy, so connections are continuously reused with no need to create new ones. Some level of intervention is needed to fill the connection pool with connections that are balanced between all the replicas.
|
||||
|
||||
If you set the `maxUses` configuration option, the pool will proactively retire a resource (connection) once it has been acquired and released `maxUses` number of times, which over a period of time will eventually lead to a relatively balanced pool.
|
||||
|
||||
One way to calculate a reasonable value for `maxUses` is to identify an acceptable window for rebalancing and then solve for `maxUses`:
|
||||
|
||||
```sh
|
||||
maxUses = rebalanceWindowSeconds * totalRequestsPerSecond / numAppInstances / poolSize
|
||||
```
|
||||
|
||||
In the example above, assuming we acquire and release 1 connection per request and we are aiming for a 30 minute rebalancing window:
|
||||
|
||||
```sh
|
||||
maxUses = rebalanceWindowSeconds * totalRequestsPerSecond / numAppInstances / poolSize
|
||||
7200 = 1800 * 1000 / 10 / 25
|
||||
```
|
||||
|
||||
...in other words we would retire and replace a connection after every 7200 uses, which we expect to be around 30 minutes under peak load.
|
||||
|
||||
Of course, you'll want to test scenarios for your own application since every app and every traffic pattern is different.
|
||||
|
||||
## Contributing
|
||||
|
||||
We use [Node Tap](https://node-tap.org/) for testing.
|
||||
|
||||
```sh
|
||||
npm install
|
||||
npm test
|
||||
```
|
||||
|
||||
Documentation is generated with `typedoc`
|
||||
|
||||
```sh
|
||||
npm run docs
|
||||
```
|
||||
22
qwen/nodejs/node_modules/sequelize-pool/lib/AggregateError.js
generated
vendored
Normal file
22
qwen/nodejs/node_modules/sequelize-pool/lib/AggregateError.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AggregateError = void 0;
|
||||
class AggregateError extends Error {
|
||||
constructor(errors) {
|
||||
super();
|
||||
this.errors = errors;
|
||||
this.name = 'AggregateError';
|
||||
}
|
||||
toString() {
|
||||
const message = `AggregateError of:\n${this.errors
|
||||
.map((error) => error === this
|
||||
? '[Circular AggregateError]'
|
||||
: error instanceof AggregateError
|
||||
? String(error).replace(/\n$/, '').replace(/^/gm, ' ')
|
||||
: String(error).replace(/^/gm, ' ').substring(2))
|
||||
.join('\n')}\n`;
|
||||
return message;
|
||||
}
|
||||
}
|
||||
exports.AggregateError = AggregateError;
|
||||
//# sourceMappingURL=AggregateError.js.map
|
||||
1
qwen/nodejs/node_modules/sequelize-pool/lib/AggregateError.js.map
generated
vendored
Normal file
1
qwen/nodejs/node_modules/sequelize-pool/lib/AggregateError.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"AggregateError.js","sourceRoot":"","sources":["../src/AggregateError.ts"],"names":[],"mappings":";;;AAGA,MAAa,cAAe,SAAQ,KAAK;IAGvC,YAAY,MAAe;QACzB,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;IAED,QAAQ;QACN,MAAM,OAAO,GAAG,uBAAuB,IAAI,CAAC,MAAM;aAC/C,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACb,KAAK,KAAK,IAAI;YACZ,CAAC,CAAC,2BAA2B;YAC7B,CAAC,CAAC,KAAK,YAAY,cAAc;gBACjC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC;gBACvD,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CACtD;aACA,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAClB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AArBD,wCAqBC"}
|
||||
38
qwen/nodejs/node_modules/sequelize-pool/lib/Deferred.js
generated
vendored
Normal file
38
qwen/nodejs/node_modules/sequelize-pool/lib/Deferred.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Deferred = void 0;
|
||||
const TimeoutError_1 = require("./TimeoutError");
|
||||
class Deferred {
|
||||
constructor() {
|
||||
this._promise = new Promise((resolve, reject) => {
|
||||
this._reject = reject;
|
||||
this._resolve = resolve;
|
||||
});
|
||||
}
|
||||
registerTimeout(timeoutInMillis, callback) {
|
||||
if (this._timeout)
|
||||
return;
|
||||
this._timeout = setTimeout(() => {
|
||||
callback();
|
||||
this.reject(new TimeoutError_1.TimeoutError('Operation timeout'));
|
||||
}, timeoutInMillis);
|
||||
}
|
||||
_clearTimeout() {
|
||||
if (!this._timeout)
|
||||
return;
|
||||
clearTimeout(this._timeout);
|
||||
}
|
||||
resolve(value) {
|
||||
this._clearTimeout();
|
||||
this._resolve(value);
|
||||
}
|
||||
reject(error) {
|
||||
this._clearTimeout();
|
||||
this._reject(error);
|
||||
}
|
||||
promise() {
|
||||
return this._promise;
|
||||
}
|
||||
}
|
||||
exports.Deferred = Deferred;
|
||||
//# sourceMappingURL=Deferred.js.map
|
||||
1
qwen/nodejs/node_modules/sequelize-pool/lib/Deferred.js.map
generated
vendored
Normal file
1
qwen/nodejs/node_modules/sequelize-pool/lib/Deferred.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Deferred.js","sourceRoot":"","sources":["../src/Deferred.ts"],"names":[],"mappings":";;;AAAA,iDAA8C;AAO9C,MAAa,QAAQ;IAMnB;QACE,IAAI,CAAC,QAAQ,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC9C,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;YACtB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QAC1B,CAAC,CAAC,CAAC;IACL,CAAC;IAED,eAAe,CAAC,eAAuB,EAAE,QAAkB;QACzD,IAAI,IAAI,CAAC,QAAQ;YAAE,OAAO;QAE1B,IAAI,CAAC,QAAQ,GAAG,UAAU,CAAC,GAAG,EAAE;YAC9B,QAAQ,EAAE,CAAC;YACX,IAAI,CAAC,MAAM,CAAC,IAAI,2BAAY,CAAC,mBAAmB,CAAC,CAAC,CAAC;QACrD,CAAC,EAAE,eAAe,CAAC,CAAC;IACtB,CAAC;IAES,aAAa;QACrB,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,OAAO;QAC3B,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAED,OAAO,CAAC,KAAQ;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAED,MAAM,CAAC,KAAY;QACjB,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;IAED,OAAO;QACL,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF;AAxCD,4BAwCC"}
|
||||
291
qwen/nodejs/node_modules/sequelize-pool/lib/Pool.js
generated
vendored
Normal file
291
qwen/nodejs/node_modules/sequelize-pool/lib/Pool.js
generated
vendored
Normal file
@@ -0,0 +1,291 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Pool = void 0;
|
||||
const Deferred_1 = require("./Deferred");
|
||||
const AggregateError_1 = require("./AggregateError");
|
||||
class Pool {
|
||||
constructor(factory) {
|
||||
this.log = false;
|
||||
if (!factory.create) {
|
||||
throw new Error('create function is required');
|
||||
}
|
||||
if (!factory.destroy) {
|
||||
throw new Error('destroy function is required');
|
||||
}
|
||||
if (!factory.validate) {
|
||||
throw new Error('validate function is required');
|
||||
}
|
||||
if (typeof factory.min !== 'number' ||
|
||||
factory.min < 0 ||
|
||||
factory.min !== Math.round(factory.min)) {
|
||||
throw new Error('min must be an integer >= 0');
|
||||
}
|
||||
if (typeof factory.max !== 'number' ||
|
||||
factory.max <= 0 ||
|
||||
factory.max !== Math.round(factory.max)) {
|
||||
throw new Error('max must be an integer > 0');
|
||||
}
|
||||
if (factory.min > factory.max) {
|
||||
throw new Error('max is smaller than min');
|
||||
}
|
||||
if (factory.maxUses !== undefined &&
|
||||
(typeof factory.maxUses !== 'number' || factory.maxUses < 0)) {
|
||||
throw new Error('maxUses must be an integer >= 0');
|
||||
}
|
||||
this.idleTimeoutMillis = factory.idleTimeoutMillis || 30000;
|
||||
this.acquireTimeoutMillis = factory.acquireTimeoutMillis || 30000;
|
||||
this.reapIntervalMillis = factory.reapIntervalMillis || 1000;
|
||||
this.maxUsesPerResource = factory.maxUses || Infinity;
|
||||
this.log = factory.log || false;
|
||||
this._factory = factory;
|
||||
this._count = 0;
|
||||
this._draining = false;
|
||||
this._pendingAcquires = [];
|
||||
this._inUseObjects = [];
|
||||
this._availableObjects = [];
|
||||
this._removeIdleScheduled = false;
|
||||
}
|
||||
get size() {
|
||||
return this._count;
|
||||
}
|
||||
get name() {
|
||||
return this._factory.name;
|
||||
}
|
||||
get available() {
|
||||
return this._availableObjects.length;
|
||||
}
|
||||
get using() {
|
||||
return this._inUseObjects.length;
|
||||
}
|
||||
get waiting() {
|
||||
return this._pendingAcquires.length;
|
||||
}
|
||||
get maxSize() {
|
||||
return this._factory.max;
|
||||
}
|
||||
get minSize() {
|
||||
return this._factory.min;
|
||||
}
|
||||
_log(message, level) {
|
||||
if (typeof this.log === 'function') {
|
||||
this.log(message, level);
|
||||
}
|
||||
else if (this.log) {
|
||||
console.log(`${level.toUpperCase()} pool ${this.name || ''} - ${message}`);
|
||||
}
|
||||
}
|
||||
_removeIdle() {
|
||||
const toRemove = [];
|
||||
const now = Date.now();
|
||||
let i;
|
||||
let available = this._availableObjects.length;
|
||||
const maxRemovable = this.size - this.minSize;
|
||||
let timeout;
|
||||
this._removeIdleScheduled = false;
|
||||
for (i = 0; i < available && maxRemovable > toRemove.length; i++) {
|
||||
timeout = this._availableObjects[i].timeout;
|
||||
if (now >= timeout) {
|
||||
this._log('removeIdle() destroying obj - now:' + now + ' timeout:' + timeout, 'verbose');
|
||||
toRemove.push(this._availableObjects[i].resource);
|
||||
}
|
||||
}
|
||||
toRemove.forEach(this.destroy, this);
|
||||
available = this._availableObjects.length;
|
||||
if (available > 0) {
|
||||
this._log('this._availableObjects.length=' + available, 'verbose');
|
||||
this._scheduleRemoveIdle();
|
||||
}
|
||||
else {
|
||||
this._log('removeIdle() all objects removed', 'verbose');
|
||||
}
|
||||
}
|
||||
_scheduleRemoveIdle() {
|
||||
if (!this._removeIdleScheduled) {
|
||||
this._removeIdleScheduled = true;
|
||||
this._removeIdleTimer = setTimeout(() => {
|
||||
this._removeIdle();
|
||||
}, this.reapIntervalMillis);
|
||||
}
|
||||
}
|
||||
_dispense() {
|
||||
let wrappedResource = null;
|
||||
const waitingCount = this._pendingAcquires.length;
|
||||
this._log(`dispense() clients=${waitingCount} available=${this._availableObjects.length}`, 'info');
|
||||
if (waitingCount < 1) {
|
||||
return;
|
||||
}
|
||||
while (this._availableObjects.length > 0) {
|
||||
this._log('dispense() - reusing obj', 'verbose');
|
||||
wrappedResource = this._availableObjects[this._availableObjects.length - 1];
|
||||
if (!this._factory.validate(wrappedResource.resource)) {
|
||||
this.destroy(wrappedResource.resource);
|
||||
continue;
|
||||
}
|
||||
this._availableObjects.pop();
|
||||
this._addResourceToInUseObjects(wrappedResource.resource, wrappedResource.useCount);
|
||||
const deferred = this._pendingAcquires.shift();
|
||||
return deferred.resolve(wrappedResource.resource);
|
||||
}
|
||||
if (this.size < this.maxSize) {
|
||||
this._createResource();
|
||||
}
|
||||
}
|
||||
_createResource() {
|
||||
this._count += 1;
|
||||
this._log(`createResource() - creating obj - count=${this.size} min=${this.minSize} max=${this.maxSize}`, 'verbose');
|
||||
this._factory
|
||||
.create()
|
||||
.then((resource) => {
|
||||
const deferred = this._pendingAcquires.shift();
|
||||
if (deferred) {
|
||||
this._addResourceToInUseObjects(resource, 0);
|
||||
deferred.resolve(resource);
|
||||
}
|
||||
else {
|
||||
this._addResourceToAvailableObjects(resource, 0);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
const deferred = this._pendingAcquires.shift();
|
||||
this._count -= 1;
|
||||
if (this._count < 0)
|
||||
this._count = 0;
|
||||
if (deferred) {
|
||||
deferred.reject(error);
|
||||
}
|
||||
process.nextTick(() => {
|
||||
this._dispense();
|
||||
});
|
||||
});
|
||||
}
|
||||
_addResourceToAvailableObjects(resource, useCount) {
|
||||
const wrappedResource = {
|
||||
resource: resource,
|
||||
useCount: useCount,
|
||||
timeout: Date.now() + this.idleTimeoutMillis,
|
||||
};
|
||||
this._availableObjects.push(wrappedResource);
|
||||
this._dispense();
|
||||
this._scheduleRemoveIdle();
|
||||
}
|
||||
_addResourceToInUseObjects(resource, useCount) {
|
||||
const wrappedResource = {
|
||||
resource: resource,
|
||||
useCount: useCount,
|
||||
};
|
||||
this._inUseObjects.push(wrappedResource);
|
||||
}
|
||||
_ensureMinimum() {
|
||||
let i, diff;
|
||||
if (!this._draining && this.size < this.minSize) {
|
||||
diff = this.minSize - this.size;
|
||||
for (i = 0; i < diff; i++) {
|
||||
this._createResource();
|
||||
}
|
||||
}
|
||||
}
|
||||
acquire() {
|
||||
if (this._draining) {
|
||||
return Promise.reject(new Error('pool is draining and cannot accept work'));
|
||||
}
|
||||
const deferred = new Deferred_1.Deferred();
|
||||
deferred.registerTimeout(this.acquireTimeoutMillis, () => {
|
||||
this._pendingAcquires = this._pendingAcquires.filter((pending) => pending !== deferred);
|
||||
});
|
||||
this._pendingAcquires.push(deferred);
|
||||
this._dispense();
|
||||
return deferred.promise();
|
||||
}
|
||||
release(resource) {
|
||||
if (this._availableObjects.some((resourceWithTimeout) => resourceWithTimeout.resource === resource)) {
|
||||
this._log('release called twice for the same resource: ' + new Error().stack, 'error');
|
||||
return;
|
||||
}
|
||||
const index = this._inUseObjects.findIndex((wrappedResource) => wrappedResource.resource === resource);
|
||||
if (index < 0) {
|
||||
this._log('attempt to release an invalid resource: ' + new Error().stack, 'error');
|
||||
return;
|
||||
}
|
||||
const wrappedResource = this._inUseObjects[index];
|
||||
wrappedResource.useCount += 1;
|
||||
if (wrappedResource.useCount >= this.maxUsesPerResource) {
|
||||
this._log('release() destroying obj - useCount:' +
|
||||
wrappedResource.useCount +
|
||||
' maxUsesPerResource:' +
|
||||
this.maxUsesPerResource, 'verbose');
|
||||
this.destroy(wrappedResource.resource);
|
||||
this._dispense();
|
||||
}
|
||||
else {
|
||||
this._inUseObjects.splice(index, 1);
|
||||
this._addResourceToAvailableObjects(wrappedResource.resource, wrappedResource.useCount);
|
||||
}
|
||||
}
|
||||
async destroy(resource) {
|
||||
const available = this._availableObjects.length;
|
||||
const using = this._inUseObjects.length;
|
||||
this._availableObjects = this._availableObjects.filter((object) => object.resource !== resource);
|
||||
this._inUseObjects = this._inUseObjects.filter((object) => object.resource !== resource);
|
||||
if (available === this._availableObjects.length &&
|
||||
using === this._inUseObjects.length) {
|
||||
this._ensureMinimum();
|
||||
return;
|
||||
}
|
||||
this._count -= 1;
|
||||
if (this._count < 0)
|
||||
this._count = 0;
|
||||
try {
|
||||
await this._factory.destroy(resource);
|
||||
}
|
||||
finally {
|
||||
this._ensureMinimum();
|
||||
if (!this._draining) {
|
||||
process.nextTick(() => {
|
||||
this._dispense();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
drain() {
|
||||
this._log('draining', 'info');
|
||||
this._draining = true;
|
||||
const check = (callback) => {
|
||||
if (this._pendingAcquires.length > 0) {
|
||||
this._dispense();
|
||||
setTimeout(() => {
|
||||
check(callback);
|
||||
}, 100);
|
||||
return;
|
||||
}
|
||||
if (this._availableObjects.length !== this._count) {
|
||||
setTimeout(() => {
|
||||
check(callback);
|
||||
}, 100);
|
||||
return;
|
||||
}
|
||||
callback();
|
||||
};
|
||||
return new Promise((resolve) => check(resolve));
|
||||
}
|
||||
async destroyAllNow() {
|
||||
this._log('force destroying all objects', 'info');
|
||||
this._removeIdleScheduled = false;
|
||||
clearTimeout(this._removeIdleTimer);
|
||||
const resources = this._availableObjects.map((resource) => resource.resource);
|
||||
const errors = [];
|
||||
for (const resource of resources) {
|
||||
try {
|
||||
await this.destroy(resource);
|
||||
}
|
||||
catch (ex) {
|
||||
this._log('Error destroying resource: ' + ex.stack, 'error');
|
||||
errors.push(ex);
|
||||
}
|
||||
}
|
||||
if (errors.length > 0) {
|
||||
throw new AggregateError_1.AggregateError(errors);
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.Pool = Pool;
|
||||
//# sourceMappingURL=Pool.js.map
|
||||
1
qwen/nodejs/node_modules/sequelize-pool/lib/Pool.js.map
generated
vendored
Normal file
1
qwen/nodejs/node_modules/sequelize-pool/lib/Pool.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
7
qwen/nodejs/node_modules/sequelize-pool/lib/TimeoutError.js
generated
vendored
Normal file
7
qwen/nodejs/node_modules/sequelize-pool/lib/TimeoutError.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.TimeoutError = void 0;
|
||||
class TimeoutError extends Error {
|
||||
}
|
||||
exports.TimeoutError = TimeoutError;
|
||||
//# sourceMappingURL=TimeoutError.js.map
|
||||
1
qwen/nodejs/node_modules/sequelize-pool/lib/TimeoutError.js.map
generated
vendored
Normal file
1
qwen/nodejs/node_modules/sequelize-pool/lib/TimeoutError.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"TimeoutError.js","sourceRoot":"","sources":["../src/TimeoutError.ts"],"names":[],"mappings":";;;AAGA,MAAa,YAAa,SAAQ,KAAK;CAAG;AAA1C,oCAA0C"}
|
||||
10
qwen/nodejs/node_modules/sequelize-pool/lib/index.js
generated
vendored
Normal file
10
qwen/nodejs/node_modules/sequelize-pool/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Pool = exports.AggregateError = exports.TimeoutError = void 0;
|
||||
var TimeoutError_1 = require("./TimeoutError");
|
||||
Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return TimeoutError_1.TimeoutError; } });
|
||||
var AggregateError_1 = require("./AggregateError");
|
||||
Object.defineProperty(exports, "AggregateError", { enumerable: true, get: function () { return AggregateError_1.AggregateError; } });
|
||||
var Pool_1 = require("./Pool");
|
||||
Object.defineProperty(exports, "Pool", { enumerable: true, get: function () { return Pool_1.Pool; } });
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
qwen/nodejs/node_modules/sequelize-pool/lib/index.js.map
generated
vendored
Normal file
1
qwen/nodejs/node_modules/sequelize-pool/lib/index.js.map
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+CAA8C;AAArC,4GAAA,YAAY,OAAA;AACrB,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AACvB,+BAA8C;AAArC,4FAAA,IAAI,OAAA"}
|
||||
51
qwen/nodejs/node_modules/sequelize-pool/package.json
generated
vendored
Normal file
51
qwen/nodejs/node_modules/sequelize-pool/package.json
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
{
|
||||
"name": "sequelize-pool",
|
||||
"description": "Resource pooling for Node.JS",
|
||||
"version": "7.1.0",
|
||||
"author": "Sushant <sushantdhiman@outlook.com>",
|
||||
"keywords": [
|
||||
"pool",
|
||||
"pooling",
|
||||
"throttle",
|
||||
"sequelize"
|
||||
],
|
||||
"main": "lib/index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/sushantdhiman/sequelize-pool.git"
|
||||
},
|
||||
"files": [
|
||||
"lib",
|
||||
"types"
|
||||
],
|
||||
"types": "types",
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"@types/node": "^10.17.54",
|
||||
"@typescript-eslint/eslint-plugin": "^4.0.0",
|
||||
"@typescript-eslint/parser": "^4.0.0",
|
||||
"eslint": "^7.0.0",
|
||||
"eslint-config-prettier": "^7.0.0",
|
||||
"eslint-plugin-prettier": "^3.1.2",
|
||||
"prettier": "^2.0.2",
|
||||
"tap": "^14.10.7",
|
||||
"typedoc": "^0.20.30",
|
||||
"typedoc-plugin-markdown": "^3.6.0",
|
||||
"typescript": "~4.2.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 10.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "npm run lint && npm run test:raw",
|
||||
"lint": "eslint --ext .js,.ts src/**/* test/**/*",
|
||||
"pretty": "prettier src/**/*.ts test/**/*.js --write",
|
||||
"docs": "typedoc",
|
||||
"test:raw": "tap test/**/*-test.js"
|
||||
},
|
||||
"prettier": {
|
||||
"singleQuote": true
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
5
qwen/nodejs/node_modules/sequelize-pool/types/AggregateError.d.ts
generated
vendored
Normal file
5
qwen/nodejs/node_modules/sequelize-pool/types/AggregateError.d.ts
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export declare class AggregateError extends Error {
|
||||
errors: Error[];
|
||||
constructor(errors: Error[]);
|
||||
toString(): string;
|
||||
}
|
||||
13
qwen/nodejs/node_modules/sequelize-pool/types/Deferred.d.ts
generated
vendored
Normal file
13
qwen/nodejs/node_modules/sequelize-pool/types/Deferred.d.ts
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
/// <reference types="node" />
|
||||
export declare class Deferred<T> {
|
||||
protected _promise: Promise<T>;
|
||||
protected _resolve: (value: T) => void;
|
||||
protected _reject: (error: Error) => void;
|
||||
protected _timeout: NodeJS.Timer;
|
||||
constructor();
|
||||
registerTimeout(timeoutInMillis: number, callback: Function): void;
|
||||
protected _clearTimeout(): void;
|
||||
resolve(value: T): void;
|
||||
reject(error: Error): void;
|
||||
promise(): Promise<T>;
|
||||
}
|
||||
63
qwen/nodejs/node_modules/sequelize-pool/types/Pool.d.ts
generated
vendored
Normal file
63
qwen/nodejs/node_modules/sequelize-pool/types/Pool.d.ts
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
/// <reference types="node" />
|
||||
import { Deferred } from './Deferred';
|
||||
declare type LogLevel = 'verbose' | 'info' | 'error';
|
||||
declare type FactoryLogger = (message: string, level: LogLevel) => void;
|
||||
declare type PooledObject<T> = {
|
||||
resource: T;
|
||||
timeout: number;
|
||||
useCount: number;
|
||||
};
|
||||
declare type InUseObject<T> = {
|
||||
resource: T;
|
||||
useCount: number;
|
||||
};
|
||||
export interface FactoryOptions<T> {
|
||||
name?: string;
|
||||
create: () => Promise<T>;
|
||||
destroy: (resource: T) => void | Promise<void>;
|
||||
validate: (resource: T) => boolean;
|
||||
max: number;
|
||||
min: number;
|
||||
maxUses?: number;
|
||||
idleTimeoutMillis?: number;
|
||||
acquireTimeoutMillis?: number;
|
||||
reapIntervalMillis?: number;
|
||||
log?: FactoryLogger | boolean;
|
||||
}
|
||||
export declare class Pool<RawResource> {
|
||||
protected _factory: FactoryOptions<RawResource>;
|
||||
protected _count: number;
|
||||
protected _draining: boolean;
|
||||
protected idleTimeoutMillis: number;
|
||||
protected acquireTimeoutMillis: number;
|
||||
protected reapIntervalMillis: number;
|
||||
protected log: FactoryLogger | boolean;
|
||||
protected maxUsesPerResource: number;
|
||||
protected _pendingAcquires: Deferred<RawResource>[];
|
||||
protected _inUseObjects: InUseObject<RawResource>[];
|
||||
protected _availableObjects: PooledObject<RawResource>[];
|
||||
protected _removeIdleTimer: NodeJS.Timeout;
|
||||
protected _removeIdleScheduled: boolean;
|
||||
constructor(factory: FactoryOptions<RawResource>);
|
||||
get size(): number;
|
||||
get name(): string;
|
||||
get available(): number;
|
||||
get using(): number;
|
||||
get waiting(): number;
|
||||
get maxSize(): number;
|
||||
get minSize(): number;
|
||||
protected _log(message: string, level: LogLevel): void;
|
||||
protected _removeIdle(): void;
|
||||
protected _scheduleRemoveIdle(): void;
|
||||
protected _dispense(): void;
|
||||
protected _createResource(): void;
|
||||
protected _addResourceToAvailableObjects(resource: RawResource, useCount: number): void;
|
||||
protected _addResourceToInUseObjects(resource: RawResource, useCount: number): void;
|
||||
protected _ensureMinimum(): void;
|
||||
acquire(): Promise<RawResource>;
|
||||
release(resource: RawResource): void;
|
||||
destroy(resource: RawResource): Promise<void>;
|
||||
drain(): Promise<void>;
|
||||
destroyAllNow(): Promise<void>;
|
||||
}
|
||||
export {};
|
||||
2
qwen/nodejs/node_modules/sequelize-pool/types/TimeoutError.d.ts
generated
vendored
Normal file
2
qwen/nodejs/node_modules/sequelize-pool/types/TimeoutError.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export declare class TimeoutError extends Error {
|
||||
}
|
||||
3
qwen/nodejs/node_modules/sequelize-pool/types/index.d.ts
generated
vendored
Normal file
3
qwen/nodejs/node_modules/sequelize-pool/types/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export { TimeoutError } from './TimeoutError';
|
||||
export { AggregateError } from './AggregateError';
|
||||
export { Pool, FactoryOptions } from './Pool';
|
||||
Reference in New Issue
Block a user