-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Open
Description
Code is running in a lambda. When we run the function below it runs fine. But if we run it and then wait 10 minutes then run it again, we get an error every time. We are querying Aurora Postgres on AWS.
Code:
const {Pool} = require('pg');
// Global Connection, can be re-used!!
const pgPool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT,
max: process.env.MAX_CLIENTS
});
pgPool.on('error', (err, client) => {
console.error('Unexpected error in Postgress connection pool', err);
});
async function performQuery(event) {
let queryString = null;
let args = null;
try {
// some setup code here...
const client = await pgPool.connect();
try {
const res = await client.query(queryString, args);
return res.rows;
} finally {
client.release();
}
} catch (err) {
console.error('Problem executing export query:');
console.error(err); // <-- this line is in the log below
throw err;
}
}
This is what I see in the cloudwatch logs:
{
"errorType": "Error",
"errorMessage": "Connection terminated unexpectedly",
"stack": [
"Error: Connection terminated unexpectedly",
" at Connection.<anonymous> (/var/task/node_modules/pg/lib/client.js:255:9)",
" at Object.onceWrapper (events.js:312:28)",
" at Connection.emit (events.js:223:5)",
" at Connection.EventEmitter.emit (domain.js:475:20)",
" at Socket.<anonymous> (/var/task/node_modules/pg/lib/connection.js:78:10)",
" at Socket.emit (events.js:223:5)",
" at Socket.EventEmitter.emit (domain.js:475:20)",
" at TCP.<anonymous> (net.js:664:12)"
]
}
I've tried a few variations on this but the constant is the 10 minutes and the use of the Pool. To me this code is almost identical to the code in https://node-postgres.com/features/pooling.
So far it looks like the problem has been solved by using a Client instead:
const client = new Client({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT
});
await client.connect();
const res = await client.query(queryString, args);
await client.end();
return res.rows;
LoicVerrall, dgobaud, rdsedmundo, IAmMonmoy, Ulisseus and 12 morejacdx, gentlemanjohn, ameer2468, NAlexPear and ajhool
Metadata
Metadata
Assignees
Labels
No labels