Skip to content

Commit

Permalink
[POOL-418] The maximum wait time for GenericObjectPool.borrowObject(*)
Browse files Browse the repository at this point in the history
may exceed expectations due to a spurious thread wakeup

- Revisit this issue with 2 changes
- The remaining duration was incorrectly calculated and the method did
not end up waiting long enough
- Recompute the remaining duration an additional time when we block when
exhausted
  • Loading branch information
garydgregory committed Feb 8, 2025
1 parent 2fd1364 commit e200ab6
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public T borrowObject(final Duration maxWaitDuration) throws E {
final boolean blockWhenExhausted = getBlockWhenExhausted();
boolean create;
while (p == null) {
remainingWaitDuration = remainingWaitDuration.minus(durationSince(startInstant));
remainingWaitDuration = maxWaitDuration.minus(durationSince(startInstant));
create = false;
p = idleObjects.pollFirst();
if (p == null) {
Expand All @@ -306,15 +306,15 @@ public T borrowObject(final Duration maxWaitDuration) throws E {
if (blockWhenExhausted) {
if (PooledObject.isNull(p)) {
try {
remainingWaitDuration = maxWaitDuration.minus(durationSince(startInstant));
p = negativeDuration ? idleObjects.takeFirst() : idleObjects.pollFirst(maxWaitDuration);
} catch (final InterruptedException e) {
// Don't surface exception type of internal locking mechanism.
throw cast(e);
}
}
if (PooledObject.isNull(p)) {
throw new NoSuchElementException(appendStats(
"Timeout waiting for idle object, borrowMaxWaitDuration=" + remainingWaitDuration));
throw new NoSuchElementException(appendStats("Timeout waiting for idle object, borrowMaxWaitDuration=" + remainingWaitDuration));
}
} else if (PooledObject.isNull(p)) {
throw new NoSuchElementException(appendStats("Pool exhausted"));
Expand All @@ -333,8 +333,7 @@ public T borrowObject(final Duration maxWaitDuration) throws E {
}
p = null;
if (create) {
final NoSuchElementException nsee = new NoSuchElementException(
appendStats("Unable to activate object"));
final NoSuchElementException nsee = new NoSuchElementException(appendStats("Unable to activate object"));
nsee.initCause(e);
throw nsee;
}
Expand All @@ -357,8 +356,7 @@ public T borrowObject(final Duration maxWaitDuration) throws E {
}
p = null;
if (create) {
final NoSuchElementException nsee = new NoSuchElementException(
appendStats("Unable to validate object"));
final NoSuchElementException nsee = new NoSuchElementException(appendStats("Unable to validate object"));
nsee.initCause(validationThrowable);
throw nsee;
}
Expand Down

0 comments on commit e200ab6

Please sign in to comment.