Skip to content

Commit

Permalink
Merge pull request #1 from themost-framework/add-generic-pool-adapter…
Browse files Browse the repository at this point in the history
…-events

add acquired and released events
  • Loading branch information
kbarbounakis authored Mar 19, 2024
2 parents 08973c5 + c0e484b commit 5fae96d
Show file tree
Hide file tree
Showing 3 changed files with 1,489 additions and 2,632 deletions.
29 changes: 24 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { createPool } = require('generic-pool');
const { Args, TraceUtils } = require('@themost/common');
const { AsyncEventEmitter } = require('@themost/events');

const getConfigurationMethod = Symbol('getConfiguration');
const pools = Symbol('pools');
Expand Down Expand Up @@ -75,6 +76,11 @@ class GenericPoolFactory {
* @class
*/
class GenericPoolAdapter {

static [pools] = {};
static acquired = new AsyncEventEmitter();
static released = new AsyncEventEmitter();

/**
* @constructor
* @property {*} base
Expand Down Expand Up @@ -112,7 +118,13 @@ class GenericPoolAdapter {
}
// get object from pool
self.pool.acquire().then(result => {
TraceUtils.debug(`GenericPoolAdapter: acquire() => borrowed: ${self.pool.borrowed}, pending: ${self.pool.pending}`);
const { borrowed, pending } = self.pool;
GenericPoolAdapter.acquired.emit({
target: self.pool,
name: self.options.pool,
borrowed,
pending
});
// set base adapter
self.base = result;
//add lastIdentity() method by assigning base.lastIdentity
Expand Down Expand Up @@ -175,7 +187,13 @@ class GenericPoolAdapter {
if (this.base) {
// return object to pool
this.pool.release(this.base);
TraceUtils.debug(`GenericPoolAdapter: release() => borrowed: ${this.pool.borrowed}, pending: ${this.pool.pending}`);
const { borrowed, pending } = this.pool;
GenericPoolAdapter.released.emit({
target: this.pool,
name: this.options.pool,
borrowed,
pending
});
// remove lastIdentity() method
if (typeof this.lastIdentity === 'function') {
delete this.lastIdentity;
Expand Down Expand Up @@ -347,8 +365,6 @@ class GenericPoolAdapter {
*/
function createInstance(options) {
Args.check(options.adapter != null, 'Invalid argument. The target data adapter is missing.');
//init pool collection
GenericPoolAdapter[pools] = GenericPoolAdapter[pools] || {};
//get adapter's name
let name;
if (typeof options.adapter === 'string') {
Expand All @@ -365,9 +381,12 @@ function createInstance(options) {
if (typeof options.timeout === 'number') {
options.acquireTimeoutMillis = options.timeout
}

let pool = GenericPoolAdapter[pools][name];
if (pool == null) {
if (typeof options.min === 'number') {
TraceUtils.warn('GenericPoolAdapter: The min property is not supported and will be ignored.');
delete options.min;
}
//create new pool with the name specified in options
pool = createPool(new GenericPoolFactory(options), Object.assign({
// set default max size to 25
Expand Down
Loading

0 comments on commit 5fae96d

Please sign in to comment.