feat: configurable pg connection lifetime and idle timeouts (#1355)

* feat: configurable pg connection lifetime and idle timeouts

* chore: increase defaults
This commit is contained in:
Rafael Cárdenas
2022-10-13 12:39:07 -05:00
committed by GitHub
parent 9433d3c9c2
commit 46ccf0640d
3 changed files with 14 additions and 1 deletions

7
.env
View File

@@ -5,6 +5,10 @@ PG_PASSWORD=postgres
PG_DATABASE=stacks_blockchain_api
PG_SCHEMA=public
PG_SSL=false
# Idle connection timeout in seconds, defaults to 30
# PG_IDLE_TIMEOUT=30
# Max connection lifetime in seconds, defaults to 60
# PG_MAX_LIFETIME=60
# Can be any string, use to specify a use case specific to a deployment
PG_APPLICATION_NAME=stacks-blockchain-api
@@ -27,12 +31,13 @@ PG_APPLICATION_NAME=stacks-blockchain-api
# PG_PRIMARY_DATABASE=
# PG_PRIMARY_SCHEMA=
# PG_PRIMARY_SSL=
# PG_PRIMARY_IDLE_TIMEOUT=
# PG_PRIMARY_MAX_LIFETIME=
# The connection URI below can be used in place of the PG variables above,
# but if enabled it must be defined without others or omitted.
# PG_PRIMARY_CONNECTION_URI=
# Limit to how many concurrent connections can be created, defaults to 10
# See https://node-postgres.com/api/pool
# PG_CONNECTION_POOL_MAX=10
# If specified, controls the Stacks Blockchain API mode. The possible values are:

View File

@@ -138,6 +138,8 @@ export function getPostgres({
ssl: pgEnvValue('SSL'),
schema: pgEnvValue('SCHEMA'),
applicationName: pgEnvValue('APPLICATION_NAME'),
idleTimeout: parseInt(pgEnvValue('IDLE_TIMEOUT') ?? '30'),
maxLifetime: parseInt(pgEnvValue('MAX_LIFETIME') ?? '60'),
poolMax: parseInt(process.env['PG_CONNECTION_POOL_MAX'] ?? '10'),
};
const defaultAppName = 'stacks-blockchain-api';
@@ -180,6 +182,8 @@ export function getPostgres({
host: pgEnvVars.host,
port: parsePort(pgEnvVars.port),
ssl: parseArgBoolean(pgEnvVars.ssl),
idle_timeout: pgEnvVars.idleTimeout,
max_lifetime: pgEnvVars.maxLifetime,
max: pgEnvVars.poolMax,
types: PG_TYPE_MAPPINGS,
connection: {

View File

@@ -196,6 +196,8 @@ describe('postgres datastore', () => {
PG_SSL: 'true',
PG_SCHEMA: 'pg_schema_schema1',
PG_APPLICATION_NAME: 'test-env-vars',
PG_MAX_LIFETIME: '5',
PG_IDLE_TIMEOUT: '1',
},
() => {
const sql = getPostgres({ usageName: 'tests' });
@@ -205,6 +207,8 @@ describe('postgres datastore', () => {
expect(sql.options.host).toStrictEqual(['pg_host_host1']);
expect(sql.options.port).toStrictEqual([9876]);
expect(sql.options.ssl).toBe(true);
expect(sql.options.max_lifetime).toBe(5);
expect(sql.options.idle_timeout).toBe(1);
expect(sql.options.connection.search_path).toBe('pg_schema_schema1');
expect(sql.options.connection.application_name).toBe('test-env-vars:tests');
}