Skip to content

Commit

Permalink
eslint indent to 4 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-contreras-deel committed Apr 24, 2020
1 parent 63050e6 commit 6455ed2
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 303 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
ecmaVersion: 2018
},
rules: {
"semi": ["error", "always"]
indent: ["error", 4],
semi: ["error", "always"]
}
};
266 changes: 133 additions & 133 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ const { createPool } = require('generic-pool');

const FLUSH_CONNECTION = true;
const DEFAULTS = {
host: '127.0.0.1',
port: '6379',
max: 50,
idleTimeoutMillis: 10000,
reapIntervalMillis: 1000,
noReadyCheck: false,
returnToHead: false,
unwatchOnRelease: true,
name: 'default',
slowPool: {
log: false,
elapsedThreshold: 25
},
emitter: {
statusInterval: 60000
},
commands: []
host: '127.0.0.1',
port: '6379',
max: 50,
idleTimeoutMillis: 10000,
reapIntervalMillis: 1000,
noReadyCheck: false,
returnToHead: false,
unwatchOnRelease: true,
name: 'default',
slowPool: {
log: false,
elapsedThreshold: 25
},
emitter: {
statusInterval: 60000
},
commands: []
};

/**
Expand All @@ -34,76 +34,76 @@ const DEFAULTS = {
* @constructor
*/
module.exports = class RedisPool extends EventEmitter {
constructor (options = {}) {
super();
constructor (options = {}) {
super();

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

this._addCommands()
this._emitStatus()
}
this._addCommands();
this._emitStatus();
}

/**
/**
* Acquire Redis client
*
* @param {String|Number} database redis database name
* @returns {Promise} with the Redis client
*/
async acquire (database) {
let pool = this.pools[database];
if (!pool) {
pool = this.pools[database] = makePool(this.options, database);
}
async acquire (database) {
let pool = this.pools[database];
if (!pool) {
pool = this.pools[database] = makePool(this.options, database);
}

const startTime = Date.now();
const client = await pool.acquire()
const elapsedTime = Date.now() - startTime;
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 });
}
if (elapsedTime > this.options.slowPool.elapsedThreshold) {
log(this.options, { db: database, action: 'acquire', elapsed: elapsedTime, waiting: pool.pending });
}

return client;
}
return client;
}

/**
/**
* Release resource.
*
* @param {String|Number} database redis database name
* @param {Object} resource resource object to release
*/
async release (database, resource) {
if (this.options.unwatchOnRelease) {
resource.UNWATCH();
}
async release (database, resource) {
if (this.options.unwatchOnRelease) {
resource.UNWATCH();
}

const pool = this.pools[database];

const pool = this.pools[database];
if (pool) {
await pool.release(resource);
}
}

if (pool) {
await pool.release(resource);
_addCommands () {
if (this.options.commands.length) {
this.options.commands.forEach(newCommand => redis.add_command(newCommand));
}
}
}

_addCommands () {
if (this.options.commands.length) {
this.options.commands.forEach(newCommand => redis.add_command(newCommand));
_emitStatus () {
setInterval(() => {
for (const [poolKey, pool] of Object.entries(this.pools)) {
this.emit('status', {
name: this.options.name,
db: poolKey,
count: pool.size,
unused: pool.available,
waiting: pool.pending
});
}
}, this.options.emitter.statusInterval);
}
}

_emitStatus() {
setInterval(() => {
for (const [poolKey, pool] of Object.entries(this.pools)) {
this.emit('status', {
name: this.options.name,
db: poolKey,
count: pool.size,
unused: pool.available,
waiting: pool.pending
});
}
}, this.options.emitter.statusInterval);
}
};

/**
Expand All @@ -113,75 +113,75 @@ module.exports = class RedisPool extends EventEmitter {
* @returns {Pool}
*/
function makePool (options, database) {
const factory = {
create () {
return new Promise((resolve, reject) => {
let settled = false;

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

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

if (!settled) {
settled = true;
client.end(FLUSH_CONNECTION);

if (err) {
return reject(err);
}
return resolve(client);
}
});

client.on('ready', function () {
client.select(database, err => {
if (!settled) {
settled = true;

if (err) {
return reject(err);
}
return resolve(client);
}
});
});
})
},

destroy (client) {
return new Promise((resolve, reject) => {
client.quit(err => {
client.end(FLUSH_CONNECTION);
if (err) {
return reject(err);
}
return resolve();
});
})
},

validate (client) {
return new Promise(resolve => {
return resolve(client && client.connected)
})
}
}

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

return createPool(factory, config);
const factory = {
create () {
return new Promise((resolve, reject) => {
let settled = false;

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

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

if (!settled) {
settled = true;
client.end(FLUSH_CONNECTION);

if (err) {
return reject(err);
}
return resolve(client);
}
});

client.on('ready', function () {
client.select(database, err => {
if (!settled) {
settled = true;

if (err) {
return reject(err);
}
return resolve(client);
}
});
});
});
},

destroy (client) {
return new Promise((resolve, reject) => {
client.quit(err => {
client.end(FLUSH_CONNECTION);
if (err) {
return reject(err);
}
return resolve();
});
});
},

validate (client) {
return new Promise(resolve => {
return resolve(client && client.connected);
});
}
};

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

return createPool(factory, config);
}

function log (options, what) {
if (options.slowPool.log) {
console.log(JSON.stringify(Object.assign({ name: options.name }, what)));
}
if (options.slowPool.log) {
console.log(JSON.stringify(Object.assign({ name: options.name }, what)));
}
}
Loading

0 comments on commit 6455ed2

Please sign in to comment.