Skip to content

Commit 46ccf06

Browse files
authored
feat: configurable pg connection lifetime and idle timeouts (#1355)
* feat: configurable pg connection lifetime and idle timeouts * chore: increase defaults
1 parent 9433d3c commit 46ccf06

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

.env

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ PG_PASSWORD=postgres
55
PG_DATABASE=stacks_blockchain_api
66
PG_SCHEMA=public
77
PG_SSL=false
8+
# Idle connection timeout in seconds, defaults to 30
9+
# PG_IDLE_TIMEOUT=30
10+
# Max connection lifetime in seconds, defaults to 60
11+
# PG_MAX_LIFETIME=60
812

913
# Can be any string, use to specify a use case specific to a deployment
1014
PG_APPLICATION_NAME=stacks-blockchain-api
@@ -27,12 +31,13 @@ PG_APPLICATION_NAME=stacks-blockchain-api
2731
# PG_PRIMARY_DATABASE=
2832
# PG_PRIMARY_SCHEMA=
2933
# PG_PRIMARY_SSL=
34+
# PG_PRIMARY_IDLE_TIMEOUT=
35+
# PG_PRIMARY_MAX_LIFETIME=
3036
# The connection URI below can be used in place of the PG variables above,
3137
# but if enabled it must be defined without others or omitted.
3238
# PG_PRIMARY_CONNECTION_URI=
3339

3440
# Limit to how many concurrent connections can be created, defaults to 10
35-
# See https://node-postgres.com/api/pool
3641
# PG_CONNECTION_POOL_MAX=10
3742

3843
# If specified, controls the Stacks Blockchain API mode. The possible values are:

src/datastore/connection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ export function getPostgres({
138138
ssl: pgEnvValue('SSL'),
139139
schema: pgEnvValue('SCHEMA'),
140140
applicationName: pgEnvValue('APPLICATION_NAME'),
141+
idleTimeout: parseInt(pgEnvValue('IDLE_TIMEOUT') ?? '30'),
142+
maxLifetime: parseInt(pgEnvValue('MAX_LIFETIME') ?? '60'),
141143
poolMax: parseInt(process.env['PG_CONNECTION_POOL_MAX'] ?? '10'),
142144
};
143145
const defaultAppName = 'stacks-blockchain-api';
@@ -180,6 +182,8 @@ export function getPostgres({
180182
host: pgEnvVars.host,
181183
port: parsePort(pgEnvVars.port),
182184
ssl: parseArgBoolean(pgEnvVars.ssl),
185+
idle_timeout: pgEnvVars.idleTimeout,
186+
max_lifetime: pgEnvVars.maxLifetime,
183187
max: pgEnvVars.poolMax,
184188
types: PG_TYPE_MAPPINGS,
185189
connection: {

src/tests/datastore-tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ describe('postgres datastore', () => {
196196
PG_SSL: 'true',
197197
PG_SCHEMA: 'pg_schema_schema1',
198198
PG_APPLICATION_NAME: 'test-env-vars',
199+
PG_MAX_LIFETIME: '5',
200+
PG_IDLE_TIMEOUT: '1',
199201
},
200202
() => {
201203
const sql = getPostgres({ usageName: 'tests' });
@@ -205,6 +207,8 @@ describe('postgres datastore', () => {
205207
expect(sql.options.host).toStrictEqual(['pg_host_host1']);
206208
expect(sql.options.port).toStrictEqual([9876]);
207209
expect(sql.options.ssl).toBe(true);
210+
expect(sql.options.max_lifetime).toBe(5);
211+
expect(sql.options.idle_timeout).toBe(1);
208212
expect(sql.options.connection.search_path).toBe('pg_schema_schema1');
209213
expect(sql.options.connection.application_name).toBe('test-env-vars:tests');
210214
}

0 commit comments

Comments
 (0)