Skip to content

Commit

Permalink
refactor: decrement pending connections only in createConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
dariakp committed Aug 31, 2022
1 parent 525c29f commit 88ef8a5
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/cmap/connection_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
connect(connectOptions, (err, connection) => {
if (err || !connection) {
this[kLogger].debug(`connection attempt failed with error [${JSON.stringify(err)}]`);
this[kPending]--;
callback(err);
return;
}
Expand Down Expand Up @@ -622,6 +623,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
connection.markAvailable();
this.emit(ConnectionPool.CONNECTION_READY, new ConnectionReadyEvent(this, connection));

this[kPending]--;
callback(undefined, connection);
return;
});
Expand All @@ -648,14 +650,14 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
// connection permits because that potentially delays the availability of
// the connection to a checkout request
this.createConnection((err, connection) => {
this[kPending]--;
if (!err && connection) {
this[kConnections].push(connection);
process.nextTick(() => this.processWaitQueue());
}
if (this[kPoolState] === PoolState.ready) {
this[kMinPoolSizeTimer] = setTimeout(() => this.ensureMinPoolSize(), 10);
}
// TODO: destroy connection if pool paused? => this.connectionIsPerished(...)
});
} else {
this[kMinPoolSizeTimer] = setTimeout(() => this.ensureMinPoolSize(), 100);
Expand Down Expand Up @@ -730,7 +732,6 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
continue;
}
this.createConnection((err, connection) => {
this[kPending]--;
if (waitQueueMember[kCancelled]) {
if (!err && connection) {
this[kConnections].push(connection);
Expand Down

0 comments on commit 88ef8a5

Please sign in to comment.