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

fix(@cubejs-backend/query-orchestrator): prevent generic pool infinite loop #1793

Merged
merged 1 commit into from
Jan 18, 2021
Merged
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 @@ -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