[英]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
,但似乎都沒有使 pg-pool 真正成為池連接。 我也嘗試過 pg 而不是 pg-pool。 但是得到了完全相同的結果,盡管我后來發現它基本上是相同的代碼。
如果我使用運行時間較長的查詢運行它,我可以連接到 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 地址 go 的連接數,然后再向下。
我做錯了什么還是 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.