mirror of
https://github.com/alexgo-io/stacks-blockchain-api.git
synced 2026-01-12 22:43:34 +08:00
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:
7
.env
7
.env
@@ -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:
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user