node postgres (pg) 池在随后的连接请求中挂起,除非释放所有先前的客户端

问题描述 投票:0回答:0

使用 [node-postgres][1] 池,我正在尝试创建客户端来运行可以并行运行的独立查询。

但是,如果第一个客户端尚未释放,则第二个客户端池请求挂起并且永远不会返回第二个客户端。

Q1。池不应该返回可以并行运行的客户端吗? Q2。我叫错了吗?我已经沸腾了这个例子:

设置

const config = {
    user: '*********',
    password: '*****',
    database: '*****',
    max: 20,
    ssl: {
        checkServerIdentity: () => {},
        rejectUnauthorized: true
    },
    sslfactory: "org.postgresql.ssl.NonValidatingFactory",
    stream: socksConnection,
    rejectUnauthorized: true
}

const pg = require('pg');
const pool = new pg.Pool(config);

系列作品

// this works
async function inSeries(pool) {
    console.log("Requesting first client...")
    const client1 = await pool.connect()
    console.log("...Got first client...")
    client1.release()

    console.log("Requesting second client...")
    const client2 = await pool.connect()
    console.log("...Got second client...")
    client2.release()

    console.log("Requesting third client...")
    const client3 = await pool.connect()
    console.log("...Got third client...")
    client3.release()

    console.log("Requesting fourth client...")
    const client4 = await pool.connect()
    console.log("...Got fourth client...")
    client4.release()
}

并行失败

// this hangs at 2nd client
async function inParallel(pool) {
    console.log("Requesting first client...")
    const client1 = await pool.connect()
    console.log("...Got first client...")

    console.log("Requesting second client...") // reaches here
    const client2 = await pool.connect()
    console.log("...Got second client...")  // never reaches here

    console.log("Requesting third client...")
    const client3 = await pool.connect()
    console.log("...Got third client...")

    console.log("Requesting fourth client...")
    const client4 = await pool.connect()
    console.log("...Got fourth client...")

    client1.release()
    client2.release()
    client3.release()
    client4.release()
}

inSeries(pool).then(()=>pool.end())     // this works
inParallel(pool).then(()=>pool.end())   // this hangs at 2nd client

我在 Node.js v18 上运行最新版本的 node-postgres (v8.10.0)

(如果重要的话,我正在通过 Socks 代理连接到 RedShift 数据库,在单个客户端或 pool.query 查询中一切正常)。

https://github.com/brianc/node-postgres/issues/2275 [1]:https://github.com/brianc/node-postgres

pool node-postgres
© www.soinside.com 2019 - 2024. All rights reserved.