From b32b227deb0a1277f194b5d2ca9a9cb5671bd3f1 Mon Sep 17 00:00:00 2001 From: Angus ZENG Date: Wed, 10 Apr 2024 10:22:08 +0800 Subject: [PATCH] fix(pool): getConnection callback should be called only once --- lib/pool.js | 19 ++++++----- test/integration/test-pool-acquire-timeout.js | 34 +++++++++---------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/pool.js b/lib/pool.js index 4fef6e53fb5..359a9c1d170 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -81,20 +81,23 @@ class Pool extends EventEmitter { if (this.config.acquireTimeout > 0) { - const timeout = setTimeout(() => { + // eslint-disable-next-line prefer-const + let timer; - spliceConnection(this._connectionQueue, cb); - cb(new Error('Timeout acquiring connection')); - - }, this.config.acquireTimeout); + const wrappedCb = (err, connection) => { - return this._connectionQueue.push((err, connection) => { + clearTimeout(timer); + cb(err, connection); + }; - clearTimeout(timeout); + timer = setTimeout(() => { - cb(err, connection); + spliceConnection(this._connectionQueue, wrappedCb); + cb(new Error('Timeout acquiring connection')); }, this.config.acquireTimeout); + + return this._connectionQueue.push(wrappedCb); } return this._connectionQueue.push(cb); diff --git a/test/integration/test-pool-acquire-timeout.js b/test/integration/test-pool-acquire-timeout.js index 0a03d4e70cf..6a04d1d0c5c 100644 --- a/test/integration/test-pool-acquire-timeout.js +++ b/test/integration/test-pool-acquire-timeout.js @@ -1,9 +1,8 @@ 'use strict'; const mysql = require('../..'); -const test = require('utest'); const assert = require('assert'); -const common = require('../common'); +const common = require('../common.test.cjs'); const poolConfig = common.getConfig(); @@ -15,33 +14,34 @@ const poolWithAcquireTimeout = new mysql.createPool({ }); poolWithAcquireTimeout.getConnection((err, c1) => { + assert.equal(!!c1, true); assert.ifError(err); + poolWithAcquireTimeout.getConnection((err, c2) => { + assert.ifError(err); assert.equal(!!c2, true); + const C3_STARTED_AT = Date.now(); + poolWithAcquireTimeout.getConnection((e3, c3) => { const C3_DONE_AT = Date.now(); + assert.equal(C3_DONE_AT - C3_STARTED_AT >= ACQUIRE_TIMEOUT, true); + assert.equal(C3_DONE_AT - C3_STARTED_AT < ACQUIRE_TIMEOUT * 2, true); - poolWithAcquireTimeout.releaseConnection(c1); + assert.notEqual(e3, null); + assert.equal(!c3, true); + c1.release(); poolWithAcquireTimeout.getConnection((e4, c4) => { - test('Pool With Acquire Timeout', { - 'timeout of pool is full': () => { - assert.equal(e3 !== null, true); - assert.equal(!c3, true); - assert.equal(C3_DONE_AT - C3_STARTED_AT >= ACQUIRE_TIMEOUT, true); - assert.equal(C3_DONE_AT - C3_STARTED_AT < ACQUIRE_TIMEOUT * 2, true); - }, - 'ok if pool is not full': () => { - assert.equal(e4 === null, true); - assert.equal(!!c4, true); - } - }); - - poolWithAcquireTimeout.releaseConnection(c4); + assert.equal(e4, null); + assert.equal(!!c4, true); + + c4.release(); + c2.release(); + poolWithAcquireTimeout.end(); }); }); });