nodejs pg-pool 似乎没有池化

nodejs pg-pool doesn't seem to be pooling

我一直试图让 nodejs 在我的应用程序中加入 postgresql 连接,但没有成功。这是我做的独立测试:

const config = require('../config/project_config.json');
const Pool = require('pg-pool');

var pool = new Pool({
    user: config.DB_USER,
    host: config.DB_HOST,
    database: config.DB_DB,
    password: config.DB_PW,
    port: 5432,
    max: 500,
    min: 200,
    idleTimeoutMillis: 0,
    connectionTimeoutMillis: 10000
});

for (var i=0; i<100; i++){
    pool.query(
        "SELECT id, email FROM players WHERE email ~ 'ltUser'",
        [],
        (err, res) => {
            if (err !== null && err != undefined){
                console.log(`err: ${err}`);
            }
            else{
                console.log(`num rows: ${res.rows.length}`);
            }
        });
}

我得到的结果是:

num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
err: Error: Connection terminated due to connection timeout
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400
num rows: 400

如您所见,它抛出连接超时,这意味着它在我创建池时没有创建连接。我在创建池时尝试了各种参数组合,包括 keepalive: true 和 none 似乎使 pg-pool 实际上成为池连接。我也尝试过 pg 而不是 pg-pool。但是得到了完全相同的结果,虽然我后来发现它基本上是相同的代码。

如果我 运行 使用更长的 运行ning 查询,我可以在 psql 和 运行

中连接到数据库
SELECT datname,usename, ssl, client_addr, count(*
FROM pg_stat_ssl
  JOIN pg_stat_activity
    ON pg_stat_ssl.pid = pg_stat_activity.pid
where usename != 'azure_superuser'
group by datname,usename, ssl, client_addr;

然后观察我的 IP 地址的连接数上升然后再次下降。

是我做错了什么还是 pg-pool 坏了?

我在 ubuntu xenial 服务器上使用 nodejs v10.22.1。

事实证明,pg-pool 正在运行,只是根据我在其他编程语言(如 Java 和 Erlang 中的经验)所期望的方式不同。 Nodejs 不会提前创建连接,而是在从池中签出连接时创建连接。

基于此,Nodejs 中池化的主要优点是程序员不必处理打开和关闭连接,并且可以重复使用连接。

如果你想打开一定数量的后端连接,比如说 200(这个数字太大了,你可能想要 64 个左右)

然后您可以通过创建池来完成此操作,然后立即发出 200 个查询,而不释放客户端

然后释放所有 200 个客户端。

从那时起,如果 idleTimeoutMillis === 0,该池将永远保持 200 个到数据库的打开连接,您可以将其用作初始化池

var pool = new Pool({
    user: config.DB_USER,
    host: config.DB_HOST,
    database: config.DB_DB,
    password: config.DB_PW,
    port: 5432,
    max: 200,   // set max to 200 connections to the db
    //min: 200, // min is not a configuration option
    idleTimeoutMillis: 0,
    connectionTimeoutMillis: 10000
});

// create a list to hold all the "done" functions 
//   that release clients
let releaseClientDoneList = [];

// open 200 clients, without releasing them
for (var i=0; i<200; i++){
  // callback - checkout a client
  pool.connect((err, client, done) => {
      // asyncronously store the "done" function once the client 
      //   has connected to the db
      connectedNewClient(done);
    }
  )
}

let connectedNewClient = function (doneFunction) {
  // store the "done" function in the list
  releaseClientDoneList.push(doneFunction)

  // if the list has 200 "done" functions inside, 
  //   then we know we have initialised all the clients we want
  if (releaseClientDoneList.length >= 200) {
    
    // call all the "done" functions to release all the clients
    for (let doneFunctionReleasesClient of releaseClientDoneList) {
      doneFunctionReleasesClient();
    }

    // now we know there are 200 available, 
    //   initialised db connections in the pool
    databasePoolInitialised();
  }
}

let databasePoolInitialised = function () {
  // ... rest of your application code goes here
}