Skip to content

Commit ddefdb0

Browse files
authored
Simplify DefaultConnectionPool.OpenConcurrencyLimiter.tryLock (mongodb#702)
When locking for the purpose of using the wait/signal approach, one does not have to account the time spent acquiring the lock towards the timeout. While doing so may reduce the non-deterministic duration by which a timeout is exceeded by any implementation of a timeout, such overtime is usually negligible, unless the lock is highly contended and/or checking the condition before starting to wait is time-consuming. The Java SE example for `java.util.concurrent.locks.Condition.awaitNanos` ignores the time spent acquiring the lock. Doing so potentially reduces responsiveness to timeouts, but does not affect other performance characteristics, so I believe such simplification is worth it and I should have taken this path initially. JAVA-3927
1 parent 73e3085 commit ddefdb0

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

driver-core/src/main/com/mongodb/internal/connection/DefaultConnectionPool.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ private PooledConnection acquirePermitOrGetAvailableOpenedConnection(final boole
827827
throws MongoTimeoutException {
828828
PooledConnection availableConnection = null;
829829
boolean expressedDesireToGetAvailableConnection = false;
830-
tryLock(timeout);
830+
lockInterruptibly(lock);
831831
try {
832832
if (tryGetAvailable) {
833833
/* An attempt to get an available opened connection from the pool (must be done while holding the lock)
@@ -935,26 +935,13 @@ void tryHandOverOrRelease(final UsageTrackingInternalConnection openConnection)
935935
}
936936
}
937937

938-
/**
939-
* @param timeout If {@linkplain Timeout#isInfinite() infinite},
940-
* then the lock is {@linkplain Lock#lockInterruptibly() acquired interruptibly}.
941-
*/
942-
private void tryLock(final Timeout timeout) throws MongoTimeoutException {
943-
boolean success;
938+
private void lockInterruptibly(final Lock lock) throws MongoInterruptedException {
944939
try {
945-
if (timeout.isInfinite()) {
946-
lock.lockInterruptibly();
947-
success = true;
948-
} else {
949-
success = lock.tryLock(timeout.remaining(NANOSECONDS), NANOSECONDS);
950-
}
940+
lock.lockInterruptibly();
951941
} catch (InterruptedException e) {
952942
Thread.currentThread().interrupt();
953943
throw new MongoInterruptedException(null, e);
954944
}
955-
if (!success) {
956-
throw createTimeoutException(timeout);
957-
}
958945
}
959946

960947
/**

0 commit comments

Comments
 (0)