【发布时间】:2016-11-18 16:41:31
【问题描述】:
我正在尝试连接到 postgres 数据库。如果连接()需要超过 1/2 秒,我只想超时并假设它不可用。不幸的是,如果我将配置更改为无法访问的主机/端口,我会在收到 ETIMEDOUT 之前看到大约 15 秒的挂起时间。在下面的代码 sn-p 示例中,我希望它在 100% 的时间内超时,因为连接到任何数据库,无论它是否可访问,都将花费超过 1 毫秒,在 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]);
});
});
【问题讨论】:
标签: node.js postgresql node-postgres