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

timeout exceeded when trying to connect #2276

Closed
PsyTae opened this issue Jul 14, 2020 · 1 comment
Closed

timeout exceeded when trying to connect #2276

PsyTae opened this issue Jul 14, 2020 · 1 comment

Comments

@PsyTae
Copy link

PsyTae commented Jul 14, 2020

We are running a VERY recent version of pg

admin@ip-10-0-0-58:~/nodeCode/webProd$ npm ls pg
[email protected] /home/admin/nodeCode/webProd
└── [email protected]

I have the following db connection file for both MySQL connection and Postgres connections.

const mysql = require('mysql'); // mysql
const pg = require('pg'); // postgres
//--- My SQL Connection -----------------------------------------------------------------------------------------------------------------

const mysqlConfig = {
    connectionLimit: 10,
    waitForConnections: true,
    host: process.env.MYSQL_HOST,
    port: process.env.MYSQL_PORT,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASS,
    database: process.env.MYSQL_DATABASE
};

const postgisConfig = {
    host: process.env.POSTGIS_HOST,
    user: process.env.POSTGIS_USER,
    password: process.env.POSTGIS_PASS,
    database: process.env.POSTGIS_DATABASE,
    port: process.env.POSTGIS_PORT,
    max: 20,
    idleTimeoutMillis: 30000,
    connectionTimeoutMillis: 2000
};

const mysqlConn = mysql.createPool(mysqlConfig);
const postGisConn = new pg.Pool(postgisConfig);

mysqlConn.on('error', err => {
    console.error('Mysql Error:', err);
});

postGisConn.on('error', (err, client) => {
    console.error('PostGis Error', err);
});

module.exports.mysql = mysqlConn;
module.exports.postGis = postGisConn;

We use the exported pool connection from this file for queries to the db throughout the rest of the project.

const postGisConn = require('./dbConn').postGis;
...

    let gisSelect = {
        text: `SELECT
            <COLUMN1>,
            <COLUMN2>,
            <COLUMN3>,
            <COLUMN4>
        FROM <TABLE>
        WHERE <COLUMN5>= '${req.body.<VARIABLE>}'`
    };
    // console.log(gisSelect.text);
    // cb();
    postGisConn.query(gisSelect, cb);

the documentation found at https://node-postgres.com/api/pool seems to suggest this is perfectly acceptable, however, we are constantly getting this error, all over when running our code.

Error: timeout exceeded when trying to connect
    at Timeout.setTimeout [as _onTimeout] (/home/admin/nodeCode/ParticleRoutes/node_modules/pg-pool/index.js:188:27)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10)

I can't figure out why especially since the documentation specifically says pool.query doesn't need a releaseCallback

Notice in the example above no releaseCallback was necessary. The pool is doing the acquiring and releasing internally. I find pool.query to be a handy shortcut in a lot of situations.

All my research seems to suggest though that this error was because I was exhausting the available idle connections and new ones are not being acquired, if so what are we doing wrong?

@PsyTae
Copy link
Author

PsyTae commented Jul 14, 2020

Duplicate of #2262

@PsyTae PsyTae closed this as completed Jul 14, 2020
@PsyTae PsyTae reopened this Jul 14, 2020
@PsyTae PsyTae closed this as completed Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant