Skip to content

Commit 464bcc8

Browse files
committed
Change the while(complexCondition) { await } style in ConcurrentPool`
The change is similar to one done in https://jira.mongodb.org/browse/JAVA-4451, but this one is done for stylistic reasons only.
1 parent 8ecee9a commit 464bcc8

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

config/spotbugs/exclude.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@
230230
</Match>
231231

232232
<!-- Non-short-circuit operators are used intentionally. -->
233+
<Match>
234+
<Class name="com.mongodb.internal.connection.ConcurrentPool$StateAndPermits"/>
235+
<Method name="acquirePermitFair"/>
236+
<Bug pattern="NS_DANGEROUS_NON_SHORT_CIRCUIT"/>
237+
</Match>
233238
<Match>
234239
<Class name="com.mongodb.internal.connection.DefaultConnectionPool$OpenConcurrencyLimiter"/>
235240
<Method name="acquirePermitOrGetAvailableOpenedConnection"/>

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,9 @@ boolean acquirePermitFair(final long timeout, final TimeUnit unit) throws MongoI
384384
throw new MongoInterruptedException(null, e);
385385
}
386386
try {
387-
while (permits == 0) {
388-
throwIfClosedOrPaused();
387+
while (permits == 0
388+
// the absence of short-circuiting is of importance
389+
& !throwIfClosedOrPaused()) {
389390
try {
390391
if (timeout < 0 || remainingNanos == Long.MAX_VALUE) {
391392
permitAvailableOrClosedOrPausedCondition.await();
@@ -399,7 +400,6 @@ boolean acquirePermitFair(final long timeout, final TimeUnit unit) throws MongoI
399400
}
400401
}
401402
assertTrue(permits > 0);
402-
throwIfClosedOrPaused();
403403
//noinspection NonAtomicOperationOnVolatileField
404404
permits--;
405405
return true;
@@ -465,12 +465,15 @@ boolean close() {
465465
}
466466

467467
/**
468+
* @return {@code false} which means that the method did not throw.
469+
* The method returns to allow using it conveniently as part of a condition check when waiting on a {@link Condition}.
470+
* Short-circuiting operators {@code &&} and {@code ||} must not be used with this method to ensure that it is called.
468471
* @throws MongoServerUnavailableException If and only if {@linkplain #close() closed}.
469472
* @throws MongoException If and only if {@linkplain #pause(Supplier) paused}
470473
* and not {@linkplain #close() closed}. The exception is specified via the {@link #pause(Supplier)} method
471474
* and may be a subtype of {@link MongoException}.
472475
*/
473-
void throwIfClosedOrPaused() {
476+
boolean throwIfClosedOrPaused() {
474477
if (closed) {
475478
throw poolClosedExceptionSupplier.get();
476479
}
@@ -484,6 +487,7 @@ void throwIfClosedOrPaused() {
484487
lock.readLock().unlock();
485488
}
486489
}
490+
return false;
487491
}
488492

489493
boolean closed() {

0 commit comments

Comments
 (0)