lab5: устойчивость очереди, восстановление job и права broker

This commit is contained in:
2026-11-05 04:05:07 +03:00
parent 2638e1c2c4
commit 0790599d2f
44 changed files with 1122 additions and 343 deletions
+13 -1
View File
@@ -23,6 +23,9 @@ export class DatabaseClient implements OnModuleDestroy {
statement_timeout: this.config.getOrThrow(
'DATABASE_STATEMENT_TIMEOUT_MS',
),
// A relay that waits on the broker must not leave the transaction
// idle forever if confirm handling stalls.
options: `-c idle_in_transaction_session_timeout=${this.config.getOrThrow('DATABASE_IDLE_TX_TIMEOUT_MS')}`,
});
// Idle clients emit 'error' on the pool when the database drops; without a
// listener Node treats it as uncaught and terminates the process.
@@ -66,6 +69,14 @@ export class DatabaseClient implements OnModuleDestroy {
work: (client: pg.PoolClient) => Promise<T>,
): Promise<T> {
const client = await this.pool.connect();
// A server-side idle_in_transaction_session_timeout or a network drop
// can emit 'error' on a checked-out client after pg-pool removed its
// idle listener. Without this listener Node treats it as uncaught.
let fatal: Error | undefined;
const onError = (error: Error): void => {
fatal = error;
};
client.on('error', onError);
try {
await client.query('BEGIN');
const result = await work(client);
@@ -83,7 +94,8 @@ export class DatabaseClient implements OnModuleDestroy {
// Callers must see the operation error, not the rollback error.
throw error;
} finally {
client.release();
client.removeListener('error', onError);
client.release(fatal);
}
}