Skip to content

Commit

Permalink
Add BaseGenericObjectPool.{get|set}MaxWaitDuration(Duration) and
Browse files Browse the repository at this point in the history
deprecate {get|set}MaxWaitMillis(long).

Add BaseObjectPoolConfig.{get|set}MaxWaitDuration(Duration) and
deprecate {get|set}MaxWaitMillis(long).
  • Loading branch information
Gary Gregory committed Jun 6, 2021
1 parent b93003a commit e405182
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 26 deletions.
8 changes: 7 additions & 1 deletion src/changes/changes.xml
Expand Up @@ -57,7 +57,13 @@ The <action> type attribute can be add,update,fix,remove.
- TrackedUse#getLastUsedInstant()
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BaseObjectPoolConfig.setEvictorShutdownTimeout(Duration), setEvictorShutdownTimeoutMillis(Duration).
Add BaseObjectPoolConfig.{get|set}EvictorShutdownTimeout(Duration), {get|set}EvictorShutdownTimeoutMillis(Duration).
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BaseGenericObjectPool.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long).
</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BaseObjectPoolConfig.{get|set}MaxWaitDuration(Duration) and deprecate {get|set}MaxWaitMillis(long).
</action>
<!-- FIXES -->
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Expand Down
Expand Up @@ -316,7 +316,7 @@ public String toString() {
// Configuration attributes
private volatile int maxTotal = GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL;
private volatile boolean blockWhenExhausted = BaseObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED;
private volatile Duration maxWait = BaseObjectPoolConfig.DEFAULT_MAX_WAIT;
private volatile Duration maxWaitDuration = BaseObjectPoolConfig.DEFAULT_MAX_WAIT;
private volatile boolean lifo = BaseObjectPoolConfig.DEFAULT_LIFO;
private final boolean fairness;
private volatile boolean testOnCreate = BaseObjectPoolConfig.DEFAULT_TEST_ON_CREATE;
Expand Down Expand Up @@ -592,7 +592,7 @@ public final boolean getLifo() {
}

/**
* The maximum time a thread has waited to borrow objects from the pool.
* Gets the maximum time a thread has waited to borrow objects from the pool.
* @return maximum wait time in milliseconds since the pool was created
*/
public final long getMaxBorrowWaitTimeMillis() {
Expand All @@ -614,6 +614,24 @@ public final int getMaxTotal() {
return maxTotal;
}

/**
* Gets the maximum duration the
* {@code borrowObject()} method should block before throwing an
* exception when the pool is exhausted and
* {@link #getBlockWhenExhausted} is true. When less than 0, the
* {@code borrowObject()} method may block indefinitely.
*
* @return the maximum number of milliseconds {@code borrowObject()}
* will block.
*
* @see #setMaxWaitDuration
* @see #setBlockWhenExhausted
* @since 2.11.0
*/
public final Duration getMaxWaitDuration() {
return maxWaitDuration;
}

/**
* Gets the maximum amount of time (in milliseconds) the
* {@code borrowObject()} method should block before throwing an
Expand All @@ -624,11 +642,12 @@ public final int getMaxTotal() {
* @return the maximum number of milliseconds {@code borrowObject()}
* will block.
*
* @see #setMaxWaitMillis
* @see #setMaxWaitDuration
* @see #setBlockWhenExhausted
* @deprecated Use {@link #getMaxWaitDuration()}.
*/
public final long getMaxWaitMillis() {
return maxWait.toMillis();
return maxWaitDuration.toMillis();
}

/**
Expand Down Expand Up @@ -1009,7 +1028,7 @@ public final void setBlockWhenExhausted(final boolean blockWhenExhausted) {
*/
protected void setConfig(final BaseObjectPoolConfig<T> config) {
setLifo(config.getLifo());
setMaxWaitMillis(config.getMaxWaitMillis());
setMaxWaitDuration(config.getMaxWaitDuration());
setBlockWhenExhausted(config.getBlockWhenExhausted());
setTestOnCreate(config.getTestOnCreate());
setTestOnBorrow(config.getTestOnBorrow());
Expand Down Expand Up @@ -1165,6 +1184,25 @@ public final void setMaxTotal(final int maxTotal) {
this.maxTotal = maxTotal;
}

/**
* Sets the maximum duration the
* {@code borrowObject()} method should block before throwing an
* exception when the pool is exhausted and
* {@link #getBlockWhenExhausted} is true. When less than 0, the
* {@code borrowObject()} method may block indefinitely.
*
* @param maxWaitDuration the maximum duration
* {@code borrowObject()} will block or negative
* for indefinitely.
*
* @see #getMaxWaitDuration
* @see #setBlockWhenExhausted
* @since 2.11.0
*/
public final void setMaxWaitDuration(final Duration maxWaitDuration) {
this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, BaseObjectPoolConfig.DEFAULT_MAX_WAIT);
}

/**
* Sets the maximum amount of time (in milliseconds) the
* {@code borrowObject()} method should block before throwing an
Expand All @@ -1176,11 +1214,13 @@ public final void setMaxTotal(final int maxTotal) {
* {@code borrowObject()} will block or negative
* for indefinitely.
*
* @see #getMaxWaitMillis
* @see #getMaxWaitDuration
* @see #setBlockWhenExhausted
* @deprecated Use {@link #setMaxWaitDuration}.
*/
@Deprecated
public final void setMaxWaitMillis(final long maxWaitMillis) {
this.maxWait = Duration.ofMillis(maxWaitMillis);
setMaxWaitDuration(Duration.ofMillis(maxWaitMillis));
}

/**
Expand Down Expand Up @@ -1481,7 +1521,7 @@ protected void toStringAppendFields(final StringBuilder builder) {
builder.append(", blockWhenExhausted=");
builder.append(blockWhenExhausted);
builder.append(", maxWaitMillis=");
builder.append(maxWait);
builder.append(maxWaitDuration);
builder.append(", lifo=");
builder.append(lifo);
builder.append(", fairness=");
Expand Down Expand Up @@ -1548,13 +1588,13 @@ protected void toStringAppendFields(final StringBuilder builder) {
* Updates statistics after an object is borrowed from the pool.
*
* @param p object borrowed from the pool
* @param waitTime time (in milliseconds) that the borrowing thread had to wait
* @param waitDuration time (in milliseconds) that the borrowing thread had to wait
*/
final void updateStatsBorrow(final PooledObject<T> p, final Duration waitTime) {
final void updateStatsBorrow(final PooledObject<T> p, final Duration waitDuration) {
borrowedCount.incrementAndGet();
idleTimes.add(p.getIdleTime());
waitTimes.add(waitTime);
final long waitTimeMillis = waitTime.toMillis();
waitTimes.add(waitDuration);
final long waitTimeMillis = waitDuration.toMillis();

// lock-free optimistic-locking maximum
long currentMaxMillis;
Expand Down
Expand Up @@ -49,15 +49,15 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon

/**
* The default value for the {@code maxWait} configuration attribute.
* @see GenericObjectPool#getMaxWaitMillis()
* @see GenericKeyedObjectPool#getMaxWaitMillis()
* @see GenericObjectPool#getMaxWaitDuration()
* @see GenericKeyedObjectPool#getMaxWaitDuration()
*/
public static final long DEFAULT_MAX_WAIT_MILLIS = -1L;

/**
* The default value for the {@code maxWait} configuration attribute.
* @see GenericObjectPool#getMaxWaitMillis()
* @see GenericKeyedObjectPool#getMaxWaitMillis()
* @see GenericObjectPool#getMaxWaitDuration()
* @see GenericKeyedObjectPool#getMaxWaitDuration()
* @since 2.10.0
*/
public static final Duration DEFAULT_MAX_WAIT = Duration.ofMillis(DEFAULT_MAX_WAIT_MILLIS);
Expand Down Expand Up @@ -222,7 +222,7 @@ public abstract class BaseObjectPoolConfig<T> extends BaseObject implements Clon

private boolean fairness = DEFAULT_FAIRNESS;

private Duration maxWaitMillis = DEFAULT_MAX_WAIT;
private Duration maxWaitDuration = DEFAULT_MAX_WAIT;

private Duration minEvictableIdleTime = DEFAULT_MIN_EVICTABLE_IDLE_TIME;

Expand Down Expand Up @@ -394,6 +394,21 @@ public boolean getLifo() {
return lifo;
}

/**
* Gets the value for the {@code maxWait} configuration attribute for pools
* created with this configuration instance.
*
* @return The current setting of {@code maxWait} for this
* configuration instance
*
* @see GenericObjectPool#getMaxWaitDuration()
* @see GenericKeyedObjectPool#getMaxWaitDuration()
* @since 2.11.0
*/
public Duration getMaxWaitDuration() {
return maxWaitDuration;
}

/**
* Gets the value for the {@code maxWait} configuration attribute for pools
* created with this configuration instance.
Expand All @@ -403,9 +418,10 @@ public boolean getLifo() {
*
* @see GenericObjectPool#getMaxWaitMillis()
* @see GenericKeyedObjectPool#getMaxWaitMillis()
* @deprecated Use {@link #getMaxWaitDuration()}.
*/
public long getMaxWaitMillis() {
return maxWaitMillis.toMillis();
return maxWaitDuration.toMillis();
}

/**
Expand Down Expand Up @@ -740,11 +756,25 @@ public void setLifo(final boolean lifo) {
* @param maxWaitMillis The new setting of {@code maxWaitMillis}
* for this configuration instance
*
* @see GenericObjectPool#getMaxWaitMillis()
* @see GenericKeyedObjectPool#getMaxWaitMillis()
* @see GenericObjectPool#getMaxWaitDuration()
* @see GenericKeyedObjectPool#getMaxWaitDuration()
*/
public void setMaxWaitMillis(final long maxWaitMillis) {
this.maxWaitMillis = Duration.ofMillis(maxWaitMillis);
setMaxWaitDuration(Duration.ofMillis(maxWaitMillis));
}

/**
* Sets the value for the {@code maxWait} configuration attribute for pools
* created with this configuration instance.
*
* @param maxWaitDuration The new setting of {@code maxWaitDuration}
* for this configuration instance
*
* @see GenericObjectPool#getMaxWaitDuration()
* @see GenericKeyedObjectPool#getMaxWaitDuration()
*/
public void setMaxWaitDuration(final Duration maxWaitDuration) {
this.maxWaitDuration = PoolImplUtils.nonNull(maxWaitDuration, DEFAULT_MAX_WAIT);;
}

/**
Expand Down Expand Up @@ -926,7 +956,7 @@ protected void toStringAppendFields(final StringBuilder builder) {
builder.append(", fairness=");
builder.append(fairness);
builder.append(", maxWaitMillis=");
builder.append(maxWaitMillis);
builder.append(maxWaitDuration);
builder.append(", minEvictableIdleTime=");
builder.append(minEvictableIdleTime);
builder.append(", softMinEvictableIdleTime=");
Expand Down
Expand Up @@ -344,13 +344,13 @@ public void addObject(final K key) throws Exception {

/**
* Equivalent to <code>{@link #borrowObject(Object, long) borrowObject}(key,
* {@link #getMaxWaitMillis()})</code>.
* {@link #getMaxWaitDuration()})</code>.
* <p>
* {@inheritDoc}
*/
@Override
public T borrowObject(final K key) throws Exception {
return borrowObject(key, getMaxWaitMillis());
return borrowObject(key, getMaxWaitDuration().toMillis());
}


Expand Down
Expand Up @@ -607,7 +607,8 @@ private void assertConfiguration(final GenericObjectPoolConfig<?> expected, fina
"whenExhaustedAction");
assertEquals(expected.getMaxTotal(), actual.getMaxTotal(), "maxTotal");
assertEquals(expected.getMaxIdle(), actual.getMaxIdle(), "maxIdle");
assertEquals(expected.getMaxWaitMillis(), actual.getMaxWaitMillis(), "maxWait");
assertEquals(expected.getMaxWaitMillis(), actual.getMaxWaitMillis(), "maxWaitDuration");
assertEquals(expected.getMaxWaitDuration(), actual.getMaxWaitDuration(), "maxWaitDuration");
assertEquals(expected.getMinEvictableIdleTimeMillis(), actual.getMinEvictableIdleTimeMillis(),
"minEvictableIdleTimeMillis");
assertEquals(expected.getMinEvictableIdleTime(), actual.getMinEvictableIdleTime(),
Expand Down

0 comments on commit e405182

Please sign in to comment.