I'm trying to connect to a postgres database. If it takes more than 1/2 a second to connect(), I'd just like to timeout and assume it's unavailable. Unfortunately, if I change the config to an unreachable host/port, I'm seeing a hang time of about 15 seconds before I get an ETIMEDOUT. In the following code snippet example, I expect it to timeout 100% of the time because connecting to any database, no matter if it is reachable or not, is going to take longer than 1ms, specified in idleTimeoutMillis.
// npm install pg
var pg = require("pg");
var config = {
"host": "myAWS-RDSinstance",
"port": 5432,
"database": "my_db",
"user": "postgres",
"password": "foobar",
"idleTimeoutMillis": 1 // one millisecond = should timeout every time
};
var pool = new pg.Pool(config );
pool.connect(function(err, client, done){
if(err) {
return console.error('error fetching client from pool', err);
}
console.log("connected. attempting query.");
client.query("SELECT COUNT(*) FROM my_table", function(err, result){
done(); // release client to pool
if(err) throw err;
console.log(result.rows[0]);
});
});