Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow event loop to process during wait queue processing #2537

Merged
merged 4 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions lib/cmap/connection_pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ class ConnectionPool extends EventEmitter {
return;
}

// add this request to the wait queue
const waitQueueMember = { callback };

const pool = this;
Expand All @@ -233,11 +232,8 @@ class ConnectionPool extends EventEmitter {
}, waitQueueTimeoutMS);
}

// place the member at the end of the wait queue
this[kWaitQueue].push(waitQueueMember);

// process the wait queue
processWaitQueue(this);
process.nextTick(() => processWaitQueue(this));
}

/**
Expand All @@ -250,10 +246,8 @@ class ConnectionPool extends EventEmitter {
const stale = connectionIsStale(this, connection);
const willDestroy = !!(poolClosed || stale || connection.closed);

// Properly adjust state of connection
if (!willDestroy) {
connection.markAvailable();

this[kConnections].push(connection);
}

Expand All @@ -264,7 +258,7 @@ class ConnectionPool extends EventEmitter {
destroyConnection(this, connection, reason);
}

processWaitQueue(this);
process.nextTick(() => processWaitQueue(this));
}

/**
Expand Down Expand Up @@ -434,7 +428,7 @@ function createConnection(pool, callback) {

// otherwise add it to the pool for later acquisition, and try to process the wait queue
pool[kConnections].push(connection);
processWaitQueue(pool);
process.nextTick(() => processWaitQueue(pool));
});
}

Expand Down
10 changes: 6 additions & 4 deletions test/unit/cmap/connection_pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ describe('Connection Pool', function() {
sinon.stub(pool, 'availableConnectionCount').get(() => 0);
pool.checkIn(conn);

expect(pool)
.property('waitQueueSize')
.to.equal(0);
process.nextTick(() => {
expect(pool)
.property('waitQueueSize')
.to.equal(0);

done();
done();
});
});
});
});
Expand Down