diff --git a/postgres/client.js b/postgres/client.js index 5ce779a..48bb939 100644 --- a/postgres/client.js +++ b/postgres/client.js @@ -9,6 +9,8 @@ const { CONNECTION_POOL_MIN, CONNECTION_POOL_MAX, CONNECTION_TIMEOUT, + STATEMENT_TIMEOUT, + QUERY_TIMEOUT, } = require('../services/constants'), { notFoundError } = require('../services/errors'), { parseOrNot, wrapInObject, decode } = require('../services/utils'), @@ -65,9 +67,15 @@ function connect() { user: POSTGRES_USER, password: POSTGRES_PASSWORD, database: POSTGRES_DB, - port: POSTGRES_PORT + port: POSTGRES_PORT, + keepAlive: true, + // query_timeout stays above statement_timeout so the server cancels + // first and the error names the statement that overran + statement_timeout: STATEMENT_TIMEOUT, + query_timeout: QUERY_TIMEOUT }, - pool: { min: CONNECTION_POOL_MIN, max: CONNECTION_POOL_MAX } + pool: { min: CONNECTION_POOL_MIN, max: CONNECTION_POOL_MAX }, + acquireConnectionTimeout: CONNECTION_TIMEOUT }); // TODO: improve error catch! https://github.com/clay/amphora-storage-postgres/pull/7/files/16d3429767943a593ad9667b0d471fefc15088d3#diff-6a1e11a6146d3a5a01f955a44a2ac07a diff --git a/postgres/client.test.js b/postgres/client.test.js index 0ce693b..9ed587e 100644 --- a/postgres/client.test.js +++ b/postgres/client.test.js @@ -44,6 +44,13 @@ describe('postgres/client', () => { expect(knex.mock.calls.length).toBe(1); expect(table.mock.calls.length).toBe(1); expect(table.mock.calls[0][0]).toBe('information_schema.tables'); + + const config = knex.mock.calls[0][0]; + + expect(config.connection.keepAlive).toBe(true); + expect(config.connection.statement_timeout).toBe(300000); + expect(config.connection.query_timeout).toBe(330000); + expect(config.acquireConnectionTimeout).toBe(60000); }); }); }); diff --git a/services/constants.js b/services/constants.js index e3ca46d..fad52d2 100644 --- a/services/constants.js +++ b/services/constants.js @@ -11,6 +11,8 @@ module.exports.POSTGRES_DB = process.env.CLAY_STORAGE_POSTGRES_DB || module.exports.CONNECTION_POOL_MIN = parseInt(process.env.CLAY_STORAGE_CONNECTION_POOL_MIN, 10) || 2; module.exports.CONNECTION_POOL_MAX = parseInt(process.env.CLAY_STORAGE_CONNECTION_POOL_MAX, 10) || 10; module.exports.CONNECTION_TIMEOUT = parseInt(process.env.CLAY_STORAGE_CONNECTION_TIMEOUT, 10) || 60000; // 60000 is Knex default +module.exports.STATEMENT_TIMEOUT = parseInt(process.env.CLAY_STORAGE_STATEMENT_TIMEOUT, 10) || 300000; +module.exports.QUERY_TIMEOUT = parseInt(process.env.CLAY_STORAGE_QUERY_TIMEOUT, 10) || 330000; // Redis module.exports.CACHE_ENABLED = process.env.CLAY_STORAGE_POSTGRES_CACHE_ENABLED || false;