diff --git a/src/config.ts b/src/config.ts index f6d6a8b..7dd065b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -83,7 +83,8 @@ addDefaults(config, { maxTries: 3, maxActiveRequests: 0, timeout: 60000, - highLoadThreshold: 10 + highLoadThreshold: 10, + redisTimeoutThreshold: 1000 }, postgresReadOnly: { enabled: false, diff --git a/src/databases/IDatabase.ts b/src/databases/IDatabase.ts index 717c700..5aaceda 100644 --- a/src/databases/IDatabase.ts +++ b/src/databases/IDatabase.ts @@ -9,6 +9,8 @@ export interface IDatabase { prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise; highLoad(): boolean; + + shouldUseRedisTimeout(): boolean; } export type QueryType = "get" | "all" | "run"; \ No newline at end of file diff --git a/src/databases/Postgres.ts b/src/databases/Postgres.ts index 5665f71..b6409c2 100644 --- a/src/databases/Postgres.ts +++ b/src/databases/Postgres.ts @@ -283,4 +283,8 @@ export class Postgres implements IDatabase { highLoad() { return this.activePostgresRequests > this.config.postgres.highLoadThreshold; } + + shouldUseRedisTimeout() { + return this.activePostgresRequests < this.config.postgres.redisTimeoutThreshold; + } } diff --git a/src/databases/Sqlite.ts b/src/databases/Sqlite.ts index 1ff67d7..7220b5c 100644 --- a/src/databases/Sqlite.ts +++ b/src/databases/Sqlite.ts @@ -108,6 +108,10 @@ export class Sqlite implements IDatabase { highLoad() { return false; } + + shouldUseRedisTimeout() { + return false; + } } export interface SqliteConfig { diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 94a9f00..fd3cd35 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -27,6 +27,7 @@ export interface CustomWritePostgresConfig extends CustomPostgresConfig { maxActiveRequests: number; timeout: number; highLoadThreshold: number; + redisTimeoutThreshold: number; } export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig { diff --git a/src/utils/redis.ts b/src/utils/redis.ts index c884a60..3a495e7 100644 --- a/src/utils/redis.ts +++ b/src/utils/redis.ts @@ -70,7 +70,8 @@ if (config.redis?.enabled) { const start = Date.now(); activeRequests++; - const timeout = config.redis.getTimeout ? setTimeout(() => reject(), config.redis.getTimeout) : null; + const shouldUseTimeout = config.redis.getTimeout && db.shouldUseRedisTimeout(); + const timeout = shouldUseTimeout ? setTimeout(() => reject(), config.redis.getTimeout) : null; const chosenGet = pickChoice(get, getRead); chosenGet(key).then((reply) => { if (timeout !== null) clearTimeout(timeout);