mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 22:17:14 +03:00
Only use redis timeout when db not under load
This commit is contained in:
@@ -83,7 +83,8 @@ addDefaults(config, {
|
|||||||
maxTries: 3,
|
maxTries: 3,
|
||||||
maxActiveRequests: 0,
|
maxActiveRequests: 0,
|
||||||
timeout: 60000,
|
timeout: 60000,
|
||||||
highLoadThreshold: 10
|
highLoadThreshold: 10,
|
||||||
|
redisTimeoutThreshold: 1000
|
||||||
},
|
},
|
||||||
postgresReadOnly: {
|
postgresReadOnly: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ export interface IDatabase {
|
|||||||
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any | any[] | void>;
|
prepare(type: QueryType, query: string, params?: any[], options?: QueryOption): Promise<any | any[] | void>;
|
||||||
|
|
||||||
highLoad(): boolean;
|
highLoad(): boolean;
|
||||||
|
|
||||||
|
shouldUseRedisTimeout(): boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type QueryType = "get" | "all" | "run";
|
export type QueryType = "get" | "all" | "run";
|
||||||
@@ -283,4 +283,8 @@ export class Postgres implements IDatabase {
|
|||||||
highLoad() {
|
highLoad() {
|
||||||
return this.activePostgresRequests > this.config.postgres.highLoadThreshold;
|
return this.activePostgresRequests > this.config.postgres.highLoadThreshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shouldUseRedisTimeout() {
|
||||||
|
return this.activePostgresRequests < this.config.postgres.redisTimeoutThreshold;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,6 +108,10 @@ export class Sqlite implements IDatabase {
|
|||||||
highLoad() {
|
highLoad() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
shouldUseRedisTimeout() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SqliteConfig {
|
export interface SqliteConfig {
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ export interface CustomWritePostgresConfig extends CustomPostgresConfig {
|
|||||||
maxActiveRequests: number;
|
maxActiveRequests: number;
|
||||||
timeout: number;
|
timeout: number;
|
||||||
highLoadThreshold: number;
|
highLoadThreshold: number;
|
||||||
|
redisTimeoutThreshold: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {
|
export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {
|
||||||
|
|||||||
@@ -70,7 +70,8 @@ if (config.redis?.enabled) {
|
|||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
activeRequests++;
|
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);
|
const chosenGet = pickChoice(get, getRead);
|
||||||
chosenGet(key).then((reply) => {
|
chosenGet(key).then((reply) => {
|
||||||
if (timeout !== null) clearTimeout(timeout);
|
if (timeout !== null) clearTimeout(timeout);
|
||||||
|
|||||||
Reference in New Issue
Block a user