This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-pool.git
The following commit(s) were added to refs/heads/master by this push:
new 14e5e73 Redo EvictorConfig using Durations.
14e5e73 is described below
commit 14e5e736937c8a2ec6dc81d4799e80c6e3b2d95a
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Feb 15 17:56:09 2021 -0500
Redo EvictorConfig using Durations.
---
.../commons/pool2/impl/BaseGenericObjectPool.java | 4 +-
.../apache/commons/pool2/impl/EvictionConfig.java | 77 ++++++++++++++++++----
.../commons/pool2/impl/GenericKeyedObjectPool.java | 4 +-
.../commons/pool2/impl/GenericObjectPool.java | 4 +-
.../apache/commons/pool2/impl/PoolImplUtils.java | 4 ++
.../commons/pool2/impl/TestEvictionConfig.java | 34 ++++++++--
6 files changed, 103 insertions(+), 24 deletions(-)
diff --git
a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
index 04c56e9..a0d05c9 100644
--- a/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/BaseGenericObjectPool.java
@@ -1417,7 +1417,7 @@ public abstract class BaseGenericObjectPool<T> extends
BaseObject {
*/
final void startEvictor(final Duration delay) {
synchronized (evictionLock) {
- final boolean isPositiverDelay = !delay.isNegative() &&
!delay.isZero();
+ final boolean isPositiverDelay = PoolImplUtils.isPositive(delay);
if (evictor == null) { // Starting evictor for the first time or
after a cancel
if (isPositiverDelay) { // Starting new evictor
evictor = new Evictor();
@@ -1439,6 +1439,8 @@ public abstract class BaseGenericObjectPool<T> extends
BaseObject {
}
}
+
+
// Inner classes
/**
diff --git a/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
b/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
index 7592e53..3f11b4c 100644
--- a/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
+++ b/src/main/java/org/apache/commons/pool2/impl/EvictionConfig.java
@@ -16,6 +16,8 @@
*/
package org.apache.commons.pool2.impl;
+import java.time.Duration;
+
/**
* This class is used by pool implementations to pass configuration information
* to {@link EvictionPolicy} instances. The {@link EvictionPolicy} may also
have
@@ -28,12 +30,13 @@ package org.apache.commons.pool2.impl;
*/
public class EvictionConfig {
- private final long idleEvictTime;
- private final long idleSoftEvictTime;
+ private static final Duration MAX_MILLIS_DURATION =
Duration.ofMillis(Long.MAX_VALUE);
+ private final Duration idleEvictTime;
+ private final Duration idleSoftEvictTime;
private final int minIdle;
/**
- * Create a new eviction configuration with the specified parameters.
+ * Creates a new eviction configuration with the specified parameters.
* Instances are immutable.
*
* @param poolIdleEvictTime Expected to be provided by
@@ -43,53 +46,103 @@ public class EvictionConfig {
* @param minIdle Expected to be provided by
* {@link GenericObjectPool#getMinIdle()} or
* {@link GenericKeyedObjectPool#getMinIdlePerKey()}
+ * @since 2.10.0
*/
- public EvictionConfig(final long poolIdleEvictTime, final long
poolIdleSoftEvictTime,
- final int minIdle) {
- if (poolIdleEvictTime > 0) {
+ public EvictionConfig(final Duration poolIdleEvictTime, final Duration
poolIdleSoftEvictTime, final int minIdle) {
+ if (PoolImplUtils.isPositive(poolIdleEvictTime)) {
idleEvictTime = poolIdleEvictTime;
} else {
- idleEvictTime = Long.MAX_VALUE;
+ idleEvictTime = MAX_MILLIS_DURATION;
}
- if (poolIdleSoftEvictTime > 0) {
+ if (PoolImplUtils.isPositive(poolIdleSoftEvictTime)) {
idleSoftEvictTime = poolIdleSoftEvictTime;
} else {
- idleSoftEvictTime = Long.MAX_VALUE;
+ idleSoftEvictTime = MAX_MILLIS_DURATION;
}
this.minIdle = minIdle;
}
/**
- * Obtain the {@code idleEvictTime} for this eviction configuration
+ * Creates a new eviction configuration with the specified parameters.
+ * Instances are immutable.
+ *
+ * @param poolIdleEvictTime Expected to be provided by
+ * {@link BaseGenericObjectPool#getMinEvictableIdleTimeMillis()}
+ * @param poolIdleSoftEvictTime Expected to be provided by
+ * {@link BaseGenericObjectPool#getSoftMinEvictableIdleTimeMillis()}
+ * @param minIdle Expected to be provided by
+ * {@link GenericObjectPool#getMinIdle()} or
+ * {@link GenericKeyedObjectPool#getMinIdlePerKey()}
+ * @deprecated Use {@link #EvictionConfig(Duration, Duration, int)}.
+ */
+ @Deprecated
+ public EvictionConfig(final long poolIdleEvictTime, final long
poolIdleSoftEvictTime, final int minIdle) {
+ this(Duration.ofMillis(poolIdleEvictTime),
Duration.ofMillis(poolIdleSoftEvictTime), minIdle);
+ }
+
+ /**
+ * Gets the {@code idleEvictTime} for this eviction configuration
* instance.
* <p>
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
+ * </p>
*
* @return The {@code idleEvictTime} in milliseconds
*/
public long getIdleEvictTime() {
+ return idleEvictTime.toMillis();
+ }
+
+ /**
+ * Gets the {@code idleEvictTime} for this eviction configuration
+ * instance.
+ * <p>
+ * How the evictor behaves based on this value will be determined by the
+ * configured {@link EvictionPolicy}.
+ * </p>
+ *
+ * @return The {@code idleEvictTime}.
+ * @since 2.10.0
+ */
+ public Duration getIdleEvictTimeDuration() {
return idleEvictTime;
}
/**
- * Obtain the {@code idleSoftEvictTime} for this eviction configuration
+ * Gets the {@code idleSoftEvictTime} for this eviction configuration
* instance.
* <p>
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
+ * </p>
*
* @return The (@code idleSoftEvictTime} in milliseconds
*/
public long getIdleSoftEvictTime() {
+ return idleSoftEvictTime.toMillis();
+ }
+
+ /**
+ * Gets the {@code idleSoftEvictTime} for this eviction configuration
+ * instance.
+ * <p>
+ * How the evictor behaves based on this value will be determined by the
+ * configured {@link EvictionPolicy}.
+ * </p>
+ *
+ * @return The (@code idleSoftEvictTime} in milliseconds
+ */
+ public Duration getIdleSoftEvictTimeDuration() {
return idleSoftEvictTime;
}
/**
- * Obtain the {@code minIdle} for this eviction configuration instance.
+ * Gets the {@code minIdle} for this eviction configuration instance.
* <p>
* How the evictor behaves based on this value will be determined by the
* configured {@link EvictionPolicy}.
+ * </p>
*
* @return The {@code minIdle}
*/
diff --git
a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
index 3907fc7..e4098c4 100644
--- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
@@ -917,8 +917,8 @@ public class GenericKeyedObjectPool<K, T> extends
BaseGenericObjectPool<T>
synchronized (evictionLock) {
final EvictionConfig evictionConfig = new EvictionConfig(
- getMinEvictableIdleTimeMillis(),
- getSoftMinEvictableIdleTimeMillis(),
+ getMinEvictableIdleTime(),
+ getSoftMinEvictableIdleTime(),
getMinIdlePerKey());
final boolean testWhileIdle = getTestWhileIdle();
diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
index 4bbcae1..183bcc1 100644
--- a/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/GenericObjectPool.java
@@ -625,8 +625,8 @@ public class GenericObjectPool<T> extends
BaseGenericObjectPool<T>
synchronized (evictionLock) {
final EvictionConfig evictionConfig = new EvictionConfig(
- getMinEvictableIdleTimeMillis(),
- getSoftMinEvictableIdleTimeMillis(),
+ getMinEvictableIdleTime(),
+ getSoftMinEvictableIdleTime(),
getMinIdle());
final boolean testWhileIdle = getTestWhileIdle();
diff --git a/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java
b/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java
index 6d6123e..1233e08 100644
--- a/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java
+++ b/src/main/java/org/apache/commons/pool2/impl/PoolImplUtils.java
@@ -196,4 +196,8 @@ class PoolImplUtils {
static Duration toDuration(long amount, TimeUnit timeUnit) {
return Duration.of(amount, PoolImplUtils.toChronoUnit(timeUnit));
}
+
+ static boolean isPositive(final Duration delay) {
+ return !delay.isNegative() && !delay.isZero();
+ }
}
diff --git
a/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java
b/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java
index b403a81..49cbdc4 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestEvictionConfig.java
@@ -16,29 +16,49 @@
*/
package org.apache.commons.pool2.impl;
-
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.time.Duration;
+
/**
* Tests for {@link EvictionConfig}.
*/
public class TestEvictionConfig {
@Test
- public void testConstructor() {
- EvictionConfig config = new EvictionConfig(0, 0, 0);
+ public void testConstructor1s() {
+ EvictionConfig config = new EvictionConfig(Duration.ofMillis(1),
Duration.ofMillis(1), 1);
+
+ assertEquals(1, config.getIdleEvictTime());
+ assertEquals(1, config.getIdleEvictTimeDuration().toMillis());
+ assertEquals(1, config.getIdleSoftEvictTime());
+ assertEquals(1, config.getIdleSoftEvictTimeDuration().toMillis());
+ assertEquals(1, config.getMinIdle());
+ }
+
+ @Test
+ public void testConstructorZerosDurations() {
+ EvictionConfig config = new EvictionConfig(Duration.ZERO,
Duration.ZERO, 0);
assertEquals(Long.MAX_VALUE, config.getIdleEvictTime());
+ assertEquals(Long.MAX_VALUE,
config.getIdleEvictTimeDuration().toMillis());
assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictTime());
+ assertEquals(Long.MAX_VALUE,
config.getIdleSoftEvictTimeDuration().toMillis());
assertEquals(0, config.getMinIdle());
+ }
- config = new EvictionConfig(1, 1, 1);
+ @Test
+ public void testConstructorZerosMillis() {
+ @SuppressWarnings("deprecation")
+ EvictionConfig config = new EvictionConfig(0, 0, 0);
- assertEquals(1, config.getIdleEvictTime());
- assertEquals(1, config.getIdleSoftEvictTime());
- assertEquals(1, config.getMinIdle());
+ assertEquals(Long.MAX_VALUE, config.getIdleEvictTime());
+ assertEquals(Long.MAX_VALUE,
config.getIdleEvictTimeDuration().toMillis());
+ assertEquals(Long.MAX_VALUE, config.getIdleSoftEvictTime());
+ assertEquals(Long.MAX_VALUE,
config.getIdleSoftEvictTimeDuration().toMillis());
+ assertEquals(0, config.getMinIdle());
}
}