Skip to content
Permalink
Browse files
Reimplement DefaultPooledObject.getIdleDuration() using Duration
computation.

The default implementation of TrackedUse.getLastUsedInstant() uses
seconds instead of milliseconds. This interface is not implemented
within Commons Pool.

Local build ok, still looking at weird GH build failures.
  • Loading branch information
Gary Gregory committed Aug 13, 2021
1 parent 84ac062 commit 53c72e917c0ddc060745ba4edf1b4fff056c1bda
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
@@ -51,6 +51,9 @@ The <action> type attribute can be add,update,fix,remove.
- PooledObject.getActiveTime()
- PooledObject.getActiveTimeMillis()
</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">
The default implementation of TrackedUse.getLastUsedInstant() uses seconds instead of milliseconds. This interface is not implemented within Commons Pool.
</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Fix field label in BaseGenericObjectPool toString() builder: From timeBetweenEvictionRunsMillis to durationBetweenEvictionRuns.
</action>
@@ -47,6 +47,6 @@ public interface TrackedUse {
* @since 2.11.0
*/
default Instant getLastUsedInstant() {
return Instant.ofEpochSecond(getLastUsed());
return Instant.ofEpochMilli(getLastUsed());
}
}
@@ -18,6 +18,7 @@

import java.io.PrintWriter;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Deque;

@@ -163,12 +164,17 @@ public long getCreateTime() {
}

@Override
public long getIdleTimeMillis() {
final long elapsedMillis = System.currentTimeMillis() - lastReturnInstant.toEpochMilli();
public Duration getIdleDuration() {
// elapsed may be negative if:
// - another thread updates lastReturnTime during the calculation window
// - another thread updates lastReturnInstant during the calculation window
// - System.currentTimeMillis() is not monotonic (e.g. system time is set back)
return elapsedMillis >= 0 ? elapsedMillis : 0;
final Duration elapsed = Duration.between(lastReturnInstant, now());
return elapsed.isNegative() ? Duration.ZERO : elapsed;
}

@Override
public long getIdleTimeMillis() {
return getIdleDuration().toMillis();
}

@Override
@@ -1037,6 +1037,9 @@ public void testBorrowTimings() throws Exception {
assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(lastBorrowInstant1));
assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(lastReturnInstant1));
assertThat(dpo.getCreateInstant(), lessThanOrEqualTo(lastUsedInstant1));
assertThat(dpo.getCreateTime(), lessThanOrEqualTo(lastBorrowInstant1.toEpochMilli()));
assertThat(dpo.getCreateTime(), lessThanOrEqualTo(lastReturnInstant1.toEpochMilli()));
assertThat(dpo.getCreateTime(), lessThanOrEqualTo(lastUsedInstant1.toEpochMilli()));

// Sleep MUST be "long enough" to detect that more than 0 milliseconds have elapsed.
// Need an API in Java 8 to get the clock granularity.

0 comments on commit 53c72e9

Please sign in to comment.