mirror of
https://github.com/Cameri/nostream.git
synced 2025-06-01 10:39:22 +02:00
chore: add support for multiple read replicas
This commit is contained in:
parent
830a782db3
commit
769a3f6821
@ -15,15 +15,26 @@ services:
|
|||||||
DB_MAX_POOL_SIZE: 64
|
DB_MAX_POOL_SIZE: 64
|
||||||
DB_ACQUIRE_CONNECTION_TIMEOUT: 60000
|
DB_ACQUIRE_CONNECTION_TIMEOUT: 60000
|
||||||
# Read Replica
|
# Read Replica
|
||||||
|
READ_REPLICAS: 1
|
||||||
READ_REPLICA_ENABLED: 'false'
|
READ_REPLICA_ENABLED: 'false'
|
||||||
RR_DB_HOST: db
|
# Read Replica No. 1
|
||||||
RR_DB_PORT: 5432
|
RR0_DB_HOST: db
|
||||||
RR_DB_USER: nostr_ts_relay
|
RR0_DB_PORT: 5432
|
||||||
RR_DB_PASSWORD: nostr_ts_relay
|
RR0_DB_USER: nostr_ts_relay
|
||||||
RR_DB_NAME: nostr_ts_relay
|
RR0_DB_PASSWORD: nostr_ts_relay
|
||||||
RR_DB_MIN_POOL_SIZE: 16
|
RR0_DB_NAME: nostr_ts_relay
|
||||||
RR_DB_MAX_POOL_SIZE: 64
|
RR0_DB_MIN_POOL_SIZE: 16
|
||||||
RR_DB_ACQUIRE_CONNECTION_TIMEOUT: 10000
|
RR0_DB_MAX_POOL_SIZE: 64
|
||||||
|
RR0_DB_ACQUIRE_CONNECTION_TIMEOUT: 10000
|
||||||
|
# Read Replica No. 2
|
||||||
|
RR1_DB_HOST: db
|
||||||
|
RR1_DB_PORT: 5432
|
||||||
|
RR1_DB_USER: nostr_ts_relay
|
||||||
|
RR1_DB_PASSWORD: nostr_ts_relay
|
||||||
|
RR1_DB_NAME: nostr_ts_relay
|
||||||
|
RR1_DB_MIN_POOL_SIZE: 16
|
||||||
|
RR1_DB_MAX_POOL_SIZE: 64
|
||||||
|
RR1_DB_ACQUIRE_CONNECTION_TIMEOUT: 10000
|
||||||
# Redis
|
# Redis
|
||||||
REDIS_HOST: nostream-cache
|
REDIS_HOST: nostream-cache
|
||||||
REDIS_PORT: 6379
|
REDIS_PORT: 6379
|
||||||
|
@ -83,6 +83,7 @@ export class App implements IRunnable {
|
|||||||
debug('starting worker')
|
debug('starting worker')
|
||||||
createWorker({
|
createWorker({
|
||||||
WORKER_TYPE: 'worker',
|
WORKER_TYPE: 'worker',
|
||||||
|
WORKER_INDEX: i.toString(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
logCentered(`${workerCount} client workers started`, width)
|
logCentered(`${workerCount} client workers started`, width)
|
||||||
|
@ -46,27 +46,32 @@ const getMasterConfig = (): Knex.Config => ({
|
|||||||
: 60000,
|
: 60000,
|
||||||
} as any)
|
} as any)
|
||||||
|
|
||||||
const getReadReplicaConfig = (): Knex.Config => ({
|
const getReadReplicaConfigByIndex = (index: number): Knex.Config => ({
|
||||||
tag: 'read-replica',
|
tag: 'read-replica',
|
||||||
client: 'pg',
|
client: 'pg',
|
||||||
connection: {
|
connection: {
|
||||||
host: process.env.RR_DB_HOST,
|
host: process.env[`RR${index}_DB_HOST`],
|
||||||
port: Number(process.env.RR_DB_PORT),
|
port: Number(process.env[`RR${index}_DB_PORT`]),
|
||||||
user: process.env.RR_DB_USER,
|
user: process.env[`RR${index}_DB_USER`],
|
||||||
password: process.env.RR_DB_PASSWORD,
|
password: process.env[`RR${index}_DB_PASSWORD`],
|
||||||
database: process.env.RR_DB_NAME,
|
database: process.env[`RR${index}_DB_NAME`],
|
||||||
},
|
},
|
||||||
pool: {
|
pool: {
|
||||||
min: process.env.RR_DB_MIN_POOL_SIZE ? Number(process.env.RR_DB_MIN_POOL_SIZE) : 0,
|
min: process.env[`RR${index}_DB_MIN_POOL_SIZE`] ? Number(process.env[`RR${index}_DB_MIN_POOL_SIZE`]) : 0,
|
||||||
max: process.env.RR_DB_MAX_POOL_SIZE ? Number(process.env.RR_DB_MAX_POOL_SIZE) : 3,
|
max: process.env[`RR${index}_DB_MAX_POOL_SIZE`] ? Number(process.env[`RR${index}_DB_MAX_POOL_SIZE`]) : 3,
|
||||||
idleTimeoutMillis: 60000,
|
idleTimeoutMillis: 60000,
|
||||||
propagateCreateError: false,
|
propagateCreateError: false,
|
||||||
acquireTimeoutMillis: process.env.RR_DB_ACQUIRE_CONNECTION_TIMEOUT
|
acquireTimeoutMillis: process.env[`RR${index}_DB_ACQUIRE_CONNECTION_TIMEOUT`]
|
||||||
? Number(process.env.RR_DB_ACQUIRE_CONNECTION_TIMEOUT)
|
? Number(process.env[`RR${index}_DB_ACQUIRE_CONNECTION_TIMEOUT`])
|
||||||
: 60000,
|
: 60000,
|
||||||
},
|
},
|
||||||
} as any)
|
} as any)
|
||||||
|
|
||||||
|
const getReadReplicaConfig = (): Knex.Config => {
|
||||||
|
const readReplicaIndex = Number(process.env.WORKER_INDEX) % Number(process.env.READ_REPLICAS)
|
||||||
|
return getReadReplicaConfigByIndex(readReplicaIndex)
|
||||||
|
}
|
||||||
|
|
||||||
let writeClient: Knex
|
let writeClient: Knex
|
||||||
|
|
||||||
export const getMasterDbClient = () => {
|
export const getMasterDbClient = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user