From 46ccf0640de0c42e5fd71795521992fdfdc8d293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Thu, 13 Oct 2022 12:39:07 -0500 Subject: [PATCH] feat: configurable pg connection lifetime and idle timeouts (#1355) * feat: configurable pg connection lifetime and idle timeouts * chore: increase defaults --- .env | 7 ++++++- src/datastore/connection.ts | 4 ++++ src/tests/datastore-tests.ts | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 75f6af1e..afd41054 100644 --- a/.env +++ b/.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: diff --git a/src/datastore/connection.ts b/src/datastore/connection.ts index 0837dc52..6c58d3a5 100644 --- a/src/datastore/connection.ts +++ b/src/datastore/connection.ts @@ -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: { diff --git a/src/tests/datastore-tests.ts b/src/tests/datastore-tests.ts index 1c6b1065..a1fc0a98 100644 --- a/src/tests/datastore-tests.ts +++ b/src/tests/datastore-tests.ts @@ -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'); }