Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reorder configuration properties. Add author and since tags.

[#175][resolves #176]

Signed-off-by: Mark Paluch <mpaluch@vmware.com>
  • Loading branch information
mp911de committed Aug 31, 2022
1 parent c7b56af commit d0953e7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 71 deletions.
8 changes: 5 additions & 3 deletions src/main/java/io/r2dbc/pool/ConnectionPool.java
Expand Up @@ -57,6 +57,7 @@
* @author Mark Paluch
* @author Tadaya Tsuyukubo
* @author Petromir Dzhunev
* @author Gabriel Calin
*/
public class ConnectionPool implements ConnectionFactory, Disposable, Closeable, Wrapped<ConnectionFactory> {

Expand Down Expand Up @@ -178,13 +179,14 @@ private Mono<Connection> getValidConnection(Function<Connection, Mono<Void>> all

private Function<Connection, Mono<Void>> getValidationFunction(ConnectionPoolConfiguration configuration) {

String timeoutMessage = String.format("Validation timed out after %dms", this.maxValidationTime.toMillis());
Function<Connection, Mono<Void>> validation = getValidation(configuration);

if (!this.maxValidationTime.isNegative()) {
return getValidation(configuration).andThen(mono -> mono.timeout(this.maxValidationTime).onErrorMap(TimeoutException.class, e -> new R2dbcTimeoutException(timeoutMessage, e)));
String timeoutMessage = String.format("Validation timed out after %dms", this.maxValidationTime.toMillis());
return validation.andThen(mono -> mono.timeout(this.maxValidationTime).onErrorMap(TimeoutException.class, e -> new R2dbcTimeoutException(timeoutMessage, e)));
}

return getValidation(configuration);
return validation;
}

private Function<Connection, Mono<Void>> getValidation(ConnectionPoolConfiguration configuration) {
Expand Down
113 changes: 59 additions & 54 deletions src/main/java/io/r2dbc/pool/ConnectionPoolConfiguration.java
Expand Up @@ -41,6 +41,7 @@
* @author Steffen Kreutz
* @author Rodolfo Beletatti
* @author Petromir Dzhunev
* @author Gabriel Calin
*/
public final class ConnectionPoolConfiguration {

Expand All @@ -65,14 +66,16 @@ public final class ConnectionPoolConfiguration {

private final int minIdle;

private final Duration maxIdleTime;
private final Duration maxAcquireTime;

private final Duration maxCreateConnectionTime;

private final Duration maxAcquireTime;
private final Duration maxIdleTime;

private final Duration maxLifeTime;

private final Duration maxValidationTime;

private final PoolMetricsRecorder metricsRecorder;

@Nullable
Expand All @@ -91,14 +94,12 @@ public final class ConnectionPoolConfiguration {
@Nullable
private final String validationQuery;

private final Duration maxValidationTime;

private ConnectionPoolConfiguration(int acquireRetry, @Nullable Duration backgroundEvictionInterval, ConnectionFactory connectionFactory, Clock clock, Consumer<PoolBuilder<Connection, ?
extends PoolConfig<? extends Connection>>> customizer, int initialSize, int maxSize, int minIdle, Duration maxIdleTime, Duration maxCreateConnectionTime, Duration maxAcquireTime,
Duration maxLifeTime,
PoolMetricsRecorder metricsRecorder, @Nullable String name, @Nullable Function<? super Connection, ? extends Publisher<Void>> postAllocate,
extends PoolConfig<? extends Connection>>> customizer, int initialSize, int maxSize, int minIdle, Duration maxAcquireTime, Duration maxCreateConnectionTime, Duration maxIdleTime,
Duration maxLifeTime, Duration maxValidationTime, PoolMetricsRecorder metricsRecorder, @Nullable String name,
@Nullable Function<? super Connection, ? extends Publisher<Void>> postAllocate,
@Nullable Function<? super Connection, ? extends Publisher<Void>> preRelease, boolean registerJmx, ValidationDepth validationDepth,
@Nullable String validationQuery, Duration maxValidationTime) {
@Nullable String validationQuery) {
this.acquireRetry = acquireRetry;
this.connectionFactory = Assert.requireNonNull(connectionFactory, "ConnectionFactory must not be null");
this.clock = clock;
Expand All @@ -107,9 +108,10 @@ private ConnectionPoolConfiguration(int acquireRetry, @Nullable Duration backgro
this.maxSize = maxSize;
this.minIdle = minIdle;
this.maxIdleTime = maxIdleTime;
this.maxCreateConnectionTime = maxCreateConnectionTime;
this.maxAcquireTime = maxAcquireTime;
this.maxCreateConnectionTime = maxCreateConnectionTime;
this.maxLifeTime = maxLifeTime;
this.maxValidationTime = maxValidationTime;
this.metricsRecorder = metricsRecorder;
this.name = name;
this.registerJmx = registerJmx;
Expand All @@ -118,7 +120,6 @@ private ConnectionPoolConfiguration(int acquireRetry, @Nullable Duration backgro
this.validationDepth = validationDepth;
this.validationQuery = validationQuery;
this.backgroundEvictionInterval = backgroundEvictionInterval;
this.maxValidationTime = maxValidationTime;
}

/**
Expand Down Expand Up @@ -173,22 +174,26 @@ int getMaxSize() {
return this.maxSize;
}

Duration getMaxIdleTime() {
return this.maxIdleTime;
Duration getMaxAcquireTime() {
return this.maxAcquireTime;
}

Duration getMaxCreateConnectionTime() {
return this.maxCreateConnectionTime;
}

Duration getMaxAcquireTime() {
return this.maxAcquireTime;
Duration getMaxIdleTime() {
return this.maxIdleTime;
}

Duration getMaxLifeTime() {
return this.maxLifeTime;
}

Duration getMaxValidationTime() {
return this.maxValidationTime;
}

PoolMetricsRecorder getMetricsRecorder() {
return this.metricsRecorder;
}
Expand Down Expand Up @@ -221,11 +226,6 @@ String getValidationQuery() {
return this.validationQuery;
}

@Nullable
Duration getMaxValidationTime() {
return this.maxValidationTime;
}

/**
* A builder for {@link ConnectionPoolConfiguration} instances.
* <p>
Expand All @@ -252,14 +252,16 @@ public static final class Builder {

private int minIdle;

private Duration maxIdleTime = Duration.ofMinutes(30);
private Duration maxAcquireTime = NO_TIMEOUT; // negative value indicates no-timeout

private Duration maxCreateConnectionTime = NO_TIMEOUT; // negative value indicates no-timeout

private Duration maxAcquireTime = NO_TIMEOUT; // negative value indicates no-timeout
private Duration maxIdleTime = Duration.ofMinutes(30);

private Duration maxLifeTime = NO_TIMEOUT; // negative value indicates no-timeout

private Duration maxValidationTime = NO_TIMEOUT; // negative value indicates no-timeout

private PoolMetricsRecorder metricsRecorder = new SimplePoolMetricsRecorder();

@Nullable
Expand All @@ -278,8 +280,6 @@ public static final class Builder {

private ValidationDepth validationDepth = ValidationDepth.LOCAL;

private Duration maxValidationTime = NO_TIMEOUT; // negative value indicates no-timeout

private Builder() {
}

Expand Down Expand Up @@ -382,14 +382,17 @@ public Builder maxSize(int maxSize) {
}

/**
* Configure a idle {@link Duration timeout}. Defaults to 30 minutes. Configuring {@code maxIdleTime} enables background eviction using the configured idle time as interval unless
* {@link #backgroundEvictionInterval(Duration)} is configured.
* Configure {@link Duration timeout} for acquiring a {@link Connection} from pool. Default is no timeout.
* <p>
* When acquiring a {@link Connection} requires obtaining a new {@link Connection} from underlying {@link ConnectionFactory}, this timeout
* also applies to get the new one.
*
* @param maxIdleTime the maximum idle time. {@link Duration#ZERO} means immediate connection disposal. A negative or a {@code null} value results in not applying a timeout.
* @param maxAcquireTime the maximum time to acquire connection from pool. {@link Duration#ZERO} indicates that the connection must be immediately available
* otherwise acquisition fails. A negative or a {@code null} value results in not applying a timeout.
* @return this {@link Builder}
*/
public Builder maxIdleTime(@Nullable Duration maxIdleTime) {
this.maxIdleTime = applyDefault(maxIdleTime);
public Builder maxAcquireTime(@Nullable Duration maxAcquireTime) {
this.maxAcquireTime = applyDefault(maxAcquireTime);
return this;
}

Expand All @@ -407,17 +410,14 @@ public Builder maxCreateConnectionTime(@Nullable Duration maxCreateConnectionTim
}

/**
* Configure {@link Duration timeout} for acquiring a {@link Connection} from pool. Default is no timeout.
* <p>
* When acquiring a {@link Connection} requires obtaining a new {@link Connection} from underlying {@link ConnectionFactory}, this timeout
* also applies to get the new one.
* Configure a idle {@link Duration timeout}. Defaults to 30 minutes. Configuring {@code maxIdleTime} enables background eviction using the configured idle time as interval unless
* {@link #backgroundEvictionInterval(Duration)} is configured.
*
* @param maxAcquireTime the maximum time to acquire connection from pool. {@link Duration#ZERO} indicates that the connection must be immediately available
* otherwise acquisition fails. A negative or a {@code null} value results in not applying a timeout.
* @param maxIdleTime the maximum idle time. {@link Duration#ZERO} means immediate connection disposal. A negative or a {@code null} value results in not applying a timeout.
* @return this {@link Builder}
*/
public Builder maxAcquireTime(@Nullable Duration maxAcquireTime) {
this.maxAcquireTime = applyDefault(maxAcquireTime);
public Builder maxIdleTime(@Nullable Duration maxIdleTime) {
this.maxIdleTime = applyDefault(maxIdleTime);
return this;
}

Expand All @@ -433,6 +433,22 @@ public Builder maxLifeTime(Duration maxLifeTime) {
return this;
}

/**
* Configure {@link Duration timeout} for validating a {@link Connection} from pool. Default is no timeout.
*
* @param maxValidationTime the maximum time to validate connection from pool. {@link Duration#ZERO} indicates that the connection must be immediately validated
* otherwise validation fails. A negative or a {@code null} value results in not applying a timeout.
* @return this {@link Builder}
* @see Connection#validate(ValidationDepth)
* @see #validationQuery(String)
* @see #validationDepth(ValidationDepth)
* @since 0.9.2
*/
public Builder maxValidationTime(Duration maxValidationTime) {
this.maxValidationTime = applyDefault(maxValidationTime);
return this;
}

/**
* Configure {@link PoolMetricsRecorder} to calculate elapsed time and instrumentation data
*
Expand Down Expand Up @@ -539,18 +555,6 @@ public Builder validationQuery(String validationQuery) {
return this;
}

/**
* Configure {@link Duration timeout} for validating a {@link Connection} from pool. Default is no timeout.
*
* @param maxValidationTime the maximum time to validate connection from pool. {@link Duration#ZERO} indicates that the connection must be immediately validated
* otherwise validation fails. A negative or a {@code null} value results in not applying a timeout.
* @return this {@link Builder}
*/
public Builder maxValidationTime(Duration maxValidationTime) {
this.maxValidationTime = applyDefault(maxValidationTime);
return this;
}

/**
* Returns a configured {@link ConnectionPoolConfiguration}.
*
Expand All @@ -562,8 +566,9 @@ public ConnectionPoolConfiguration build() {
validate();
return new ConnectionPoolConfiguration(this.acquireRetry, this.backgroundEvictionInterval, this.connectionFactory, this.clock, this.customizer, this.initialSize, this.maxSize,
this.minIdle,
this.maxIdleTime, this.maxCreateConnectionTime, this.maxAcquireTime, this.maxLifeTime, this.metricsRecorder, this.name, this.postAllocate, this.preRelease, this.registerJmx,
this.validationDepth, this.validationQuery, this.maxValidationTime
this.maxAcquireTime, this.maxCreateConnectionTime, this.maxIdleTime, this.maxLifeTime, this.maxValidationTime, this.metricsRecorder, this.name, this.postAllocate, this.preRelease,
this.registerJmx,
this.validationDepth, this.validationQuery
);
}

Expand Down Expand Up @@ -605,20 +610,20 @@ public String toString() {
", connectionFactory='" + this.connectionFactory + '\'' +
", clock='" + this.clock + '\'' +
", initialSize='" + this.initialSize + '\'' +
", maxSize='" + this.maxSize + '\'' +
", minIdle='" + this.minIdle + '\'' +
", maxIdleTime='" + this.maxIdleTime + '\'' +
", maxCreateConnectionTime='" + this.maxCreateConnectionTime + '\'' +
", maxSize='" + this.maxSize + '\'' +
", maxAcquireTime='" + this.maxAcquireTime + '\'' +
", maxCreateConnectionTime='" + this.maxCreateConnectionTime + '\'' +
", maxIdleTime='" + this.maxIdleTime + '\'' +
", maxLifeTime='" + this.maxLifeTime + '\'' +
", maxValidationTime='" + this.maxValidationTime + '\'' +
", metricsRecorder='" + this.metricsRecorder + '\'' +
", name='" + this.name + '\'' +
", postAllocate='" + this.postAllocate + '\'' +
", preRelease='" + this.preRelease + '\'' +
", registerJmx='" + this.registerJmx + '\'' +
", validationDepth='" + this.validationDepth + '\'' +
", validationQuery='" + this.validationQuery + '\'' +
", maxValidationTime='" + this.maxValidationTime + '\'' +
'}';
}

Expand Down
31 changes: 17 additions & 14 deletions src/main/java/io/r2dbc/pool/PoolingConnectionFactoryProvider.java
Expand Up @@ -38,6 +38,7 @@
* @author Rodolfo Beletatti
* @author Rodolpho S. Couto
* @author Todd Ginsberg
* @author Gabriel Calin
*/
public class PoolingConnectionFactoryProvider implements ConnectionFactoryProvider {

Expand Down Expand Up @@ -76,18 +77,18 @@ public class PoolingConnectionFactoryProvider implements ConnectionFactoryProvid
public static final Option<Integer> MIN_IDLE = Option.valueOf("minIdle");

/**
* MaxLifeTime {@link Option}.
* MaxAcquireTime {@link Option}.
*
* @since 0.8.3
*/
public static final Option<Duration> MAX_LIFE_TIME = Option.valueOf("maxLifeTime");
public static final Option<Duration> MAX_ACQUIRE_TIME = Option.valueOf("maxAcquireTime");

/**
* MaxAcquireTime {@link Option}.
* MaxCreateConnectionTime {@link Option}.
*
* @since 0.8.3
*/
public static final Option<Duration> MAX_ACQUIRE_TIME = Option.valueOf("maxAcquireTime");
public static final Option<Duration> MAX_CREATE_CONNECTION_TIME = Option.valueOf("maxCreateConnectionTime");

/**
* MaxIdleTime {@link Option}.
Expand All @@ -97,11 +98,18 @@ public class PoolingConnectionFactoryProvider implements ConnectionFactoryProvid
public static final Option<Duration> MAX_IDLE_TIME = Option.valueOf("maxIdleTime");

/**
* MaxCreateConnectionTime {@link Option}.
* MaxLifeTime {@link Option}.
*
* @since 0.8.3
*/
public static final Option<Duration> MAX_CREATE_CONNECTION_TIME = Option.valueOf("maxCreateConnectionTime");
public static final Option<Duration> MAX_LIFE_TIME = Option.valueOf("maxLifeTime");

/**
* MaxValidationTime {@link Option}.
*
* @since 0.9.2
*/
public static final Option<Duration> MAX_VALIDATION_TIME = Option.valueOf("maxValidationTime");

/**
* Name of the Connection Pool {@link Option}
Expand Down Expand Up @@ -141,11 +149,6 @@ public class PoolingConnectionFactoryProvider implements ConnectionFactoryProvid
*/
public static final Option<ValidationDepth> VALIDATION_DEPTH = Option.valueOf("validationDepth");

/**
* MaxValidationTime {@link Option}.
*/
public static final Option<Duration> MAX_VALIDATION_TIME = Option.valueOf("maxValidationTime");

private static final String COLON = ":";

/**
Expand Down Expand Up @@ -196,17 +199,17 @@ static ConnectionPoolConfiguration buildConfiguration(ConnectionFactoryOptions c
mapper.from(MAX_SIZE).as(OptionMapper::toInteger).to(builder::maxSize);
mapper.from(MIN_IDLE).as(OptionMapper::toInteger).to(builder::minIdle);
mapper.from(ACQUIRE_RETRY).as(OptionMapper::toInteger).to(builder::acquireRetry);
mapper.from(MAX_LIFE_TIME).as(OptionMapper::toDuration).to(builder::maxLifeTime);
mapper.from(MAX_ACQUIRE_TIME).as(OptionMapper::toDuration).to(builder::maxAcquireTime);
mapper.from(MAX_IDLE_TIME).as(OptionMapper::toDuration).to(builder::maxIdleTime);
mapper.from(MAX_CREATE_CONNECTION_TIME).as(OptionMapper::toDuration).to(builder::maxCreateConnectionTime);
mapper.from(MAX_LIFE_TIME).as(OptionMapper::toDuration).to(builder::maxLifeTime);
mapper.from(MAX_IDLE_TIME).as(OptionMapper::toDuration).to(builder::maxIdleTime);
mapper.from(MAX_VALIDATION_TIME).as(OptionMapper::toDuration).to(builder::maxValidationTime);
mapper.fromExact(POOL_NAME).to(builder::name);
mapper.fromExact(POST_ALLOCATE).to(builder::postAllocate);
mapper.fromExact(PRE_RELEASE).to(builder::preRelease);
mapper.from(REGISTER_JMX).as(OptionMapper::toBoolean).to(builder::registerJmx);
mapper.fromExact(VALIDATION_QUERY).to(builder::validationQuery);
mapper.from(VALIDATION_DEPTH).as(validationDepth -> OptionMapper.toEnum(validationDepth, ValidationDepth.class)).to(builder::validationDepth);
mapper.from(MAX_VALIDATION_TIME).as(OptionMapper::toDuration).to(builder::maxValidationTime);

return builder.build();
}
Expand Down
1 change: 1 addition & 0 deletions src/test/java/io/r2dbc/pool/ConnectionPoolUnitTests.java
Expand Up @@ -73,6 +73,7 @@
* @author Mark Paluch
* @author Tadaya Tsuyukubo
* @author Petromir Dzhunev
* @author Gabriel Calin
*/
@SuppressWarnings("unchecked")
final class ConnectionPoolUnitTests {
Expand Down
Expand Up @@ -40,6 +40,7 @@
* @author Mark Paluch
* @author Rodolpho S. Couto
* @author Todd Ginsberg
* @author Gabriel Calin
*/
final class PoolingConnectionFactoryProviderUnitTests {

Expand Down

0 comments on commit d0953e7

Please sign in to comment.