Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBS initialisation and timeouts #1017

Merged
merged 10 commits into from
Dec 4, 2023
2 changes: 1 addition & 1 deletion mod/query.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const dbs_connections = require('./utils/dbs')()
const dbs_connections = require('./utils/dbs')

const sqlFilter = require('./utils/sqlFilter')

Expand Down
20 changes: 12 additions & 8 deletions mod/user/acl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { Pool } = require('pg');

module.exports = () => {
const connection = process.env.PRIVATE?.split('|') || process.env.PUBLIC?.split('|')

let pool = null

const connection = process.env.PRIVATE && process.env.PRIVATE.split('|') || process.env.PUBLIC && process.env.PUBLIC.split('|')
module.exports = () => {

if(!connection || !connection[1]) return

Expand All @@ -11,24 +13,26 @@ module.exports = () => {
const acl_schema = connection[1].split('.')[0] === acl_table ? 'public' : connection[1].split('.')[0]

// Create PostgreSQL connection pool for ACL table.
const pool = new Pool({
connectionString: connection[0],
statement_timeout: 1000
pool ??= new Pool({
connectionString: connection[0]
})

// Method to query ACL. arr must be empty array by default.
return async (q, arr) => {

try {

const { rows } = await pool.query(q.replace(/acl_table/g, acl_table).replace(/acl_schema/g, acl_schema), arr)
const client = await pool.connect()

const { rows } = await client.query(q.replace(/acl_table/g, acl_table).replace(/acl_schema/g, acl_schema), arr)

client.release()

return rows

} catch (err) {
console.error(err)
return err
}

}

}
77 changes: 34 additions & 43 deletions mod/utils/dbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,40 @@ const logger = require('./logger');

const dbs = {};

module.exports = () => {

Object.keys(process.env)

// Filter keys which start with DBS
.filter(key => key.split('_')[0] === 'DBS')

// Filter keys which are not yet assigned to dbs object.
.filter(key => !dbs[key.split('_')[1]])

.forEach(key => {

const pool = new Pool({
connectionString: process.env[key],
statement_timeout: parseInt(process.env.STATEMENT_TIMEOUT) || 10000
});

dbs[key.split('_')[1]] = async (q, arr, timeout) => {

// Request which accepts q and arr and will return rows or rows.err.
try {

const client = await pool.connect()

timeout && await client.query(`SET statement_timeout = ${parseInt(timeout)}`)

const { rows } = await client.query(q, arr)

timeout && await client.query(`SET statement_timeout = ${parseInt(process.env.STATEMENT_TIMEOUT) || 10000}`)

client.release()

return rows

} catch (err) {

logger({err, q, arr})
return err;
Object.keys(process.env)

// Filter keys which start with DBS
.filter(key => key.startsWith('DBS_'))

.forEach(key => {

const pool = new Pool({
connectionString: process.env[key],
keepAlive: true
});

dbs[key.split('_')[1]] = async (query, variables, timeout) => {

try {

const client = await pool.connect()

if (timeout || process.env.STATEMENT_TIMEOUT) {
await client.query(`SET statement_timeout = ${parseInt(timeout) || parseInt(process.env.STATEMENT_TIMEOUT)}`)
}

}

})
const { rows } = await client.query(query, variables)

client.release()

return rows

} catch (err) {

logger({ err, query, variables })
return err;
}
}
})

return dbs
};
module.exports = dbs