forked from mtusi2026/mtusi2026lab3
lab3: проба готовности на отдельном соединении, серия роста на блоке why#
This commit is contained in:
@@ -7,6 +7,7 @@ import type { AppConfig } from '../config/app-config.schema.js';
|
||||
@Injectable()
|
||||
export class DatabaseClient implements OnModuleDestroy {
|
||||
readonly pool: pg.Pool;
|
||||
private readonly healthPool: pg.Pool;
|
||||
private readonly logger = new Logger(DatabaseClient.name);
|
||||
|
||||
constructor(
|
||||
@@ -29,6 +30,19 @@ export class DatabaseClient implements OnModuleDestroy {
|
||||
this.metrics.increment('database.pool_error');
|
||||
this.logger.warn(`PostgreSQL pool error: ${error.message}`);
|
||||
});
|
||||
// Проба готовности не должна стоять в очереди за бизнес-запросами: занятый
|
||||
// общий пул — это перегрузка, а не потеря зависимости.
|
||||
this.healthPool = new pg.Pool({
|
||||
connectionString: this.config.getOrThrow('DATABASE_URL'),
|
||||
max: 1,
|
||||
idleTimeoutMillis: 0,
|
||||
connectionTimeoutMillis: this.config.getOrThrow('HEALTH_TIMEOUT_MS'),
|
||||
query_timeout: this.config.getOrThrow('HEALTH_TIMEOUT_MS'),
|
||||
});
|
||||
this.healthPool.on('error', (error: Error) => {
|
||||
this.metrics.increment('database.health_pool_error');
|
||||
this.logger.warn(`PostgreSQL health connection error: ${error.message}`);
|
||||
});
|
||||
}
|
||||
|
||||
query<T extends pg.QueryResultRow = pg.QueryResultRow>(
|
||||
@@ -41,7 +55,8 @@ export class DatabaseClient implements OnModuleDestroy {
|
||||
}
|
||||
|
||||
async health(): Promise<void> {
|
||||
await this.query('health.database', 'SELECT 1');
|
||||
this.metrics.increment('health.database');
|
||||
await this.healthPool.query('SELECT 1');
|
||||
}
|
||||
|
||||
async withTransaction<T>(
|
||||
@@ -70,6 +85,6 @@ export class DatabaseClient implements OnModuleDestroy {
|
||||
}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.pool.end();
|
||||
await Promise.all([this.pool.end(), this.healthPool.end()]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user