feat(pool): emit 'connectionQueueAcquired' event on connection dequeue fixes #3844#3929
feat(pool): emit 'connectionQueueAcquired' event on connection dequeue fixes #3844#3929shubhanshu2103 wants to merge 1 commit intosidorares:masterfrom
Conversation
|
@wellwelwel please let me know the improvements needed. |
wellwelwel
left a comment
There was a problem hiding this comment.
Hey, @shubhanshu2103! I've suggested some ideas in the added test, but I'd suggest implementing the feature itself before moving on to the tests (or discussing the feature implementation in issue #3844) 🤝
| @@ -0,0 +1,116 @@ | |||
| 'use strict'; | |||
|
|
|||
| const assert = require('assert'); | |||
There was a problem hiding this comment.
Are there a specific limitation or motivation for using Node.js assert in this test? For example:
const { assert, test } = require('poku');| function queuedConnectionCallback(err, conn) { | ||
| if (err) throw err | ||
| } |
There was a problem hiding this comment.
I recommend declaring functions/variables before they are used 🙋🏻♂️
| function createTestPool(options) { | ||
| const config = { | ||
| host: process.env.MYSQL_HOST, | ||
| user: process.env.MYSQL_USER, | ||
| password: process.env.MYSQL_PASSWORD, | ||
| database: process.env.MYSQL_DATABASE, | ||
| ...options, | ||
| }; | ||
| return mysql.createPool(config); | ||
| } |
There was a problem hiding this comment.
You can use the connection from const { createPool } = require('../common.test.cjs'), for example:
node-mysql2/test/integration/test-pool-end.test.cjs
Lines 3 to 6 in eba0c2b
|
|
||
| // --- Test Case --- | ||
|
|
||
| test('Pool emits connectionQueueAcquired event and reports correct queue depth', function (done) { |
There was a problem hiding this comment.
There is no done parameter in the tests, I think this is due to the callbacks. If you have any questions about how to wait the callbacks, please feel free to ask 🙋🏻♂️
| return new Promise(r => setTimeout(r, 100)); | ||
| }) | ||
| .then(() => { | ||
| // Final Assertions and Cleanup (after all acquisitions are complete) |
There was a problem hiding this comment.
An interesting approach to testing is to use the test descriptions themselves to "comment" (describe) on what is happening. Not as a rule, but by reserving comments within the code for specific or complex behaviors.
You can, for example, combine describe and it to make it easier to describe the tests, as well as the messages in the assertions themselves 💡
|
Note I'm marking this PR as a draft until the feature is implemented 🚧 |
This PR introduces a new event, 'connectionQueueAcquired', on the connection pool. This event fires whenever a waiting request is pulled from the internal queue (_connectionQueue) and successfully acquires a free connection.
This addresses the request for better observability of connection pool bottlenecks by providing real-time data on the queue state at the exact moment a waiting request is served.
🌟 Key Changes
New Event Emission in lib/base/pool.js:
The BasePool.releaseConnection() method is modified to emit 'connectionQueueAcquired' when this._connectionQueue.length is greater than 0, after shifting the waiting callback but before calling it.
Event Payload:
The event payload provides the essential information needed for diagnostics:
{
connection: connection, // The Connection object being handed to the request
queueDepth: this._connectionQueue.length // The actual size of the queue after the request was dequeued
}
New Integration Test:
A new integration test file, test/integration/test-pool-queue-event.js, was added to fully validate the feature.
The test saturates the pool (limit 2), queues two additional requests (depth 2), and then releases the active connections, asserting that the event fires exactly twice with the correct descending queueDepth values of 1 and 0.
Implementation Details
The implementation in releaseConnection is minimal and targeted to avoid side effects:
} else if (this._connectionQueue.length) {
cb = this._connectionQueue.shift();
} else {
// ...