Skip to content

Commit

Permalink
fix(@cubejs-backend/query-orchestrator): prevent generic pool infinit…
Browse files Browse the repository at this point in the history
…e loop (#1793)
  • Loading branch information
vasilev-alex committed Jan 18, 2021
1 parent 5ee21ec commit d4129c4
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/cubejs-query-orchestrator/src/orchestrator/RedisPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ export interface RedisPoolOptions {
destroyClient?: (client: AsyncRedisClient) => PromiseLike<void>;
}

const MAX_ALLOWED_POOL_ERRORS = 100;

export class RedisPool {
protected readonly pool: Pool<AsyncRedisClient>|null = null;

protected readonly create: CreateRedisClientFn|null = null;

protected poolErrors: number = 0;

public constructor(options: RedisPoolOptions = {}) {
const defaultMin = process.env.CUBEJS_REDIS_POOL_MIN ? parseInt(process.env.CUBEJS_REDIS_POOL_MIN, 10) : 2;
Expand All @@ -29,13 +33,23 @@ export class RedisPool {
idleTimeoutMillis: 5000,
evictionRunIntervalMillis: 5000
};

const create = options.createClient || (async () => createRedisClient(process.env.REDIS_URL));

if (max > 0) {
const destroy = options.destroyClient || (async (client) => client.end(true));

this.pool = genericPool.createPool<AsyncRedisClient>({ create, destroy }, opts);

this.pool.on('factoryCreateError', (error) => {
this.poolErrors++;
// prevent the infinite loop when pool creation fails too many times
if (this.poolErrors > MAX_ALLOWED_POOL_ERRORS) {
// @ts-ignore
// eslint-disable-next-line
this.pool._waitingClientsQueue.dequeue().reject(error);
}
});
} else {
// fallback to un-pooled behavior if pool max is 0
this.create = create;
Expand Down

0 comments on commit d4129c4

Please sign in to comment.