Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[misc] small performance enhancement to handle active connection counter
  • Loading branch information
rusher committed May 31, 2022
1 parent e20e08b commit 94be5e8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/pool.js
Expand Up @@ -119,7 +119,7 @@ class Pool extends EventEmitter {

release(conn) {
this._endLeak(conn);
delete this.#activeConnections[conn.threadId];
this.#activeConnections[conn.threadId] = undefined;

conn.lastUse = Date.now();
if (this.#closed) {
Expand Down Expand Up @@ -351,11 +351,16 @@ class Pool extends EventEmitter {
let info = null;
let conn = this.#idleConnections.get(0);

if (conn == null) {
conn = Object.keys(this.#activeConnections)[0];
if (!conn) {
for (const threadId in Object.keys(this.#activeConnections)) {
conn = this.#activeConnections[threadId];
if (!conn) {
break;
}
}
}

if (conn != null) {
if (conn) {
info = conn.info;
}
return info;
Expand Down Expand Up @@ -425,7 +430,11 @@ class Pool extends EventEmitter {
* @return {number}
*/
activeConnections() {
return Object.keys(this.#activeConnections).length;
let counter = 0;
for (const [key, value] of Object.entries(this.#activeConnections)) {
if (value) counter++;
}
return counter;
}

/**
Expand Down

0 comments on commit 94be5e8

Please sign in to comment.