lab5: устойчивость очереди, восстановление job и права broker
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user