Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use options pattern in RedisPool constructor #468

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class PreAggregations {
this.logger = logger;
this.queryCache = queryCache;
this.cacheDriver = options.cacheAndQueueDriver === 'redis' ?
new RedisCacheDriver(options.redisPool) :
new RedisCacheDriver({ pool: options.redisPool }) :
new LocalCacheDriver();
this.externalDriverFactory = options.externalDriverFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class QueryCache {
this.externalDriverFactory = options.externalDriverFactory;
this.logger = logger;
this.cacheDriver = options.cacheAndQueueDriver === 'redis' ?
new RedisCacheDriver(options.redisPool) :
new RedisCacheDriver({ pool: options.redisPool }) :
new LocalCacheDriver();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class RedisCacheDriver {
constructor(pool) {
constructor({ pool }) {
this.redisPool = pool;
}

async getClient() {
return await this.redisPool.getClient();
return this.redisPool.getClient();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of interest, does removing the await change the semantics of getClient()? It seems like it now returns a promise that needs to be await-ed, whereas before the result of the promise was returned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hassankhan await required if you have try/catch/finally block inside a method. Otherwise it works the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, it was the linting that led me to remove that, this rule violation was triggered (not sure why I didn't notice it originally): https://eslint.org/docs/rules/no-return-await

}

async get(key) {
Expand Down
11 changes: 6 additions & 5 deletions packages/cubejs-query-orchestrator/orchestrator/RedisPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const genericPool = require('generic-pool');
const createRedisClient = require('./RedisFactory');

class RedisPool {
constructor(poolMin, poolMax, createClient, destroyClient) {
constructor(options) {
options = options || {};
const defaultMin = process.env.CUBEJS_REDIS_POOL_MIN ? parseInt(process.env.CUBEJS_REDIS_POOL_MIN, 10) : 2;
const defaultMax = process.env.CUBEJS_REDIS_POOL_MAX ? parseInt(process.env.CUBEJS_REDIS_POOL_MAX, 10) : 1000;
const min = (typeof poolMin !== 'undefined') ? poolMin : defaultMin;
const max = (typeof poolMax !== 'undefined') ? poolMax : defaultMax;
const create = createClient || (() => createRedisClient(process.env.REDIS_URL));
const destroy = destroyClient || (client => client.end(true));
const min = (typeof options.poolMin !== 'undefined') ? options.poolMin : defaultMin;
const max = (typeof options.poolMax !== 'undefined') ? options.poolMax : defaultMax;
const create = options.createClient || (() => createRedisClient(process.env.REDIS_URL));
const destroy = options.destroyClient || (client => client.end(true));
const opts = {
min,
max,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ const QueryQueueTest = (name, options) => {

QueryQueueTest('Local');
QueryQueueTest('RedisPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool() });
QueryQueueTest('RedisNoPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool(0, 0) });
QueryQueueTest('RedisNoPool', { cacheAndQueueDriver: 'redis', redisPool: new RedisPool({ poolMin: 0, poolMax: 0 }) });