Skip to content

Commit

Permalink
receive external logger
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-contreras-deel committed May 8, 2020
1 parent 31dd3df commit 6224247
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
40 changes: 22 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ const DEFAULTS = {
* @constructor
*/
module.exports = class RedisPool extends EventEmitter {
constructor (options = {}) {
constructor (options = {}, logger = console) {
super();

this.pools = {};
this.options = Object.assign({}, DEFAULTS, options);
this.logger = logger

this._addCommands();
this._emitStatus();
Expand All @@ -54,15 +55,15 @@ module.exports = class RedisPool extends EventEmitter {
async acquire (database) {
let pool = this.pools[database];
if (!pool) {
pool = this.pools[database] = makePool(this.options, database);
pool = this.pools[database] = makePool(this, database);
}

const startTime = Date.now();
const client = await pool.acquire();
const elapsedTime = Date.now() - startTime;

if (elapsedTime > this.options.slowPool.elapsedThreshold) {
log(this.options, { db: database, action: 'acquire', elapsed: elapsedTime, waiting: pool.pending });
this._log({ db: database, action: 'acquire', elapsed: elapsedTime, waiting: pool.pending });
}

return client;
Expand Down Expand Up @@ -109,28 +110,37 @@ module.exports = class RedisPool extends EventEmitter {
_getStatusDelay() {
return (this.options.emitter && this.options.emitter.statusInterval) || DEFAULT_STATUS_INTERVAL;
}

_log(info) {
if (!this.options.slowPool.log) {
return;
}

info = Object.assign({ name: this.options.name }, info)
this.logger.error(JSON.stringify(info));
}
};

/**
* Factory to create new Redis pools for a given Redis database
* @param options
* @param redisPool
* @param database
* @returns {Pool}
*/
function makePool (options, database) {
function makePool (redisPool, database) {
const factory = {
// create function will loop forever if reject is called or exception is thrown
// https://github.com/coopernurse/node-pool/issues/175
create () {
return new Promise(resolve => {
let settled = false;

const client = redis.createClient(options.port, options.host, {
no_ready_check: options.noReadyCheck
const client = redis.createClient(redisPool.options.port, redisPool.options.host, {
no_ready_check: redisPool.options.noReadyCheck
});

client.on('error', function (err) {
log(options, { db: database, action: 'error', err: err.message });
redisPool._log({ db: database, action: 'error', err: err.message });

if (!settled) {
settled = true;
Expand Down Expand Up @@ -178,17 +188,11 @@ function makePool (options, database) {
};

const config = {
max: options.max,
idleTimeoutMillis: options.idleTimeoutMillis,
reapIntervalMillis: options.reapIntervalMillis,
returnToHead: options.returnToHead
max: redisPool.options.max,
idleTimeoutMillis: redisPool.options.idleTimeoutMillis,
reapIntervalMillis: redisPool.options.reapIntervalMillis,
returnToHead: redisPool.options.returnToHead
};

return createPool(factory, config);
}

function log (options, what) {
if (options.slowPool.log) {
console.log(JSON.stringify(Object.assign({ name: options.name }, what)));
}
}
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ describe('RedisPool', function () {
return times++ * elapsedThreshold * 2;
};

const consoleLogFunc = console.log;
console.log = function (what) {
const consoleLogFunc = console.error;
console.error = function (what) {
const whatObj = JSON.parse(what);
logWasCalled = whatObj && whatObj.action && whatObj.action === 'acquire';
consoleLogFunc.apply(console, arguments);
Expand All @@ -171,7 +171,7 @@ describe('RedisPool', function () {
const client = await redisPool.acquire(0);

// restore functions
console.log = consoleLogFunc;
console.error = consoleLogFunc;
Date.now = dateNowFunc;

redisPool.release(0, client);
Expand Down

0 comments on commit 6224247

Please sign in to comment.