fix: retry pg connection on new library code (#1326)

* fix: retry pg connection on new library code

* fix: add ECONNRESET error code
This commit is contained in:
Rafael Cárdenas
2022-09-21 10:50:23 -05:00
committed by GitHub
parent 20b284fa38
commit 35db939199
3 changed files with 36 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
import { Client, ClientConfig, Pool, PoolClient, PoolConfig } from 'pg';
import { logError, logger, parseArgBoolean, parsePort, stopwatch, timeout } from '../helpers';
import { PgServer } from './connection';
import { isPgConnectionError } from './helpers';
export type PgClientConfig = ClientConfig & { schema?: string };
type PgPoolConfig = PoolConfig & { schema?: string };
@@ -167,33 +168,3 @@ export async function connectWithRetry(pool: Pool): Promise<PoolClient> {
}
}
}
/**
* Checks if a given error from the pg lib is a connection error (i.e. the query is retryable).
* If true then returns a normalized error message, otherwise returns false.
*/
function isPgConnectionError(error: any): string | false {
if (error.code === 'ECONNREFUSED') {
return 'Postgres connection ECONNREFUSED';
} else if (error.code === 'ETIMEDOUT') {
return 'Postgres connection ETIMEDOUT';
} else if (error.code === 'ENOTFOUND') {
return 'Postgres connection ENOTFOUND';
} else if (error.message) {
const msg = (error as Error).message.toLowerCase();
if (msg.includes('database system is starting up')) {
return 'Postgres connection failed while database system is starting up';
} else if (msg.includes('database system is shutting down')) {
return 'Postgres connection failed while database system is shutting down';
} else if (msg.includes('connection terminated unexpectedly')) {
return 'Postgres connection terminated unexpectedly';
} else if (msg.includes('connection terminated')) {
return 'Postgres connection terminated';
} else if (msg.includes('connection error')) {
return 'Postgres client has encountered a connection error and is not queryable';
} else if (msg.includes('terminating connection due to unexpected postmaster exit')) {
return 'Postgres connection terminating due to unexpected postmaster exit';
}
}
return false;
}

View File

@@ -1,5 +1,6 @@
import { has0xPrefix, logError, parseArgBoolean, parsePort, stopwatch, timeout } from '../helpers';
import { logError, parseArgBoolean, parsePort, stopwatch, timeout } from '../helpers';
import * as postgres from 'postgres';
import { isPgConnectionError } from './helpers';
export type PgSqlClient = postgres.Sql<any>;
@@ -87,7 +88,7 @@ export async function connectPostgres({
connectionOkay = true;
break;
} catch (error: any) {
if (error instanceof postgres.PostgresError) {
if (isPgConnectionError(error) || error instanceof postgres.PostgresError) {
const timeElapsed = initTimer.getElapsed();
if (timeElapsed - lastElapsedLog > 2000) {
lastElapsedLog = timeElapsed;

View File

@@ -171,6 +171,38 @@ export const TX_METADATA_TABLES = [
'subdomains',
] as const;
/**
* Checks if a given error from the pg lib is a connection error (i.e. the query is retryable).
* If true then returns a normalized error message, otherwise returns false.
*/
export function isPgConnectionError(error: any): string | false {
if (error.code === 'ECONNREFUSED') {
return 'Postgres connection ECONNREFUSED';
} else if (error.code === 'ETIMEDOUT') {
return 'Postgres connection ETIMEDOUT';
} else if (error.code === 'ENOTFOUND') {
return 'Postgres connection ENOTFOUND';
} else if (error.code === 'ECONNRESET') {
return 'Postgres connection ECONNRESET';
} else if (error.message) {
const msg = (error as Error).message.toLowerCase();
if (msg.includes('database system is starting up')) {
return 'Postgres connection failed while database system is starting up';
} else if (msg.includes('database system is shutting down')) {
return 'Postgres connection failed while database system is shutting down';
} else if (msg.includes('connection terminated unexpectedly')) {
return 'Postgres connection terminated unexpectedly';
} else if (msg.includes('connection terminated')) {
return 'Postgres connection terminated';
} else if (msg.includes('connection error')) {
return 'Postgres client has encountered a connection error and is not queryable';
} else if (msg.includes('terminating connection due to unexpected postmaster exit')) {
return 'Postgres connection terminating due to unexpected postmaster exit';
}
}
return false;
}
/**
* Adds a table name prefix to an array of column names.
* @param columns - array of column names