Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions postgres/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions postgres/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions services/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down