Skip to content

Commit

Permalink
fix(pool): getConnection callback should be called only once
Browse files Browse the repository at this point in the history
  • Loading branch information
fenying committed Apr 10, 2024
1 parent a97ff42 commit b32b227
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
19 changes: 11 additions & 8 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 17 additions & 17 deletions test/integration/test-pool-acquire-timeout.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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();
});
});
});
Expand Down

0 comments on commit b32b227

Please sign in to comment.