Skip to content

Commit

Permalink
Map MinIdle and MaxValidationTime to R2DBC pools
Browse files Browse the repository at this point in the history
Closes gh-34724
  • Loading branch information
mhalbritter committed May 10, 2023
1 parent 52789cb commit 947ac8d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
Expand Up @@ -103,6 +103,8 @@ ConnectionPool connectionFactory(R2dbcProperties properties, ResourceLoader reso
map.from(pool.getMaxSize()).to(builder::maxSize);
map.from(pool.getValidationQuery()).whenHasText().to(builder::validationQuery);
map.from(pool.getValidationDepth()).to(builder::validationDepth);
map.from(pool.getMinIdle()).to(builder::minIdle);
map.from(pool.getMaxValidationTime()).to(builder::maxValidationTime);
return new ConnectionPool(builder.build());
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -136,6 +136,13 @@ public String determineUniqueName() {

public static class Pool {

/**
* Minimal number of idle connections.
*
* @since 2.7.12
*/
private int minIdle = 0;

/**
* Maximum amount of time that a connection is allowed to sit idle in the pool.
*/
Expand All @@ -153,6 +160,14 @@ public static class Pool {
*/
private Duration maxAcquireTime;

/**
* Maximum time to validate a connection from the pool. By default, wait
* indefinitely.
*
* @since 2.7.12
*/
private Duration maxValidationTime;

/**
* Maximum time to wait to create a new connection. By default, wait indefinitely.
*/
Expand Down Expand Up @@ -183,6 +198,14 @@ public static class Pool {
*/
private boolean enabled = true;

public int getMinIdle() {
return this.minIdle;
}

public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}

public Duration getMaxIdleTime() {
return this.maxIdleTime;
}
Expand All @@ -199,6 +222,14 @@ public void setMaxLifeTime(Duration maxLifeTime) {
this.maxLifeTime = maxLifeTime;
}

public Duration getMaxValidationTime() {
return this.maxValidationTime;
}

public void setMaxValidationTime(Duration maxValidationTime) {
this.maxValidationTime = maxValidationTime;
}

public Duration getMaxAcquireTime() {
return this.maxAcquireTime;
}
Expand Down
Expand Up @@ -77,15 +77,25 @@ void configureWithUrlCreateConnectionPoolByDefault() {
void configureWithUrlAndPoolPropertiesApplyProperties() {
this.contextRunner
.withPropertyValues("spring.r2dbc.url:r2dbc:h2:mem:///" + randomDatabaseName(),
"spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m")
"spring.r2dbc.pool.max-size=15", "spring.r2dbc.pool.max-acquire-time=3m",
"spring.r2dbc.pool.min-idle=1", "spring.r2dbc.pool.max-validation-time=1s",
"spring.r2dbc.pool.initial-size=0")
.run((context) -> {
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(ConnectionPool.class)
.hasSingleBean(R2dbcProperties.class);
ConnectionPool connectionPool = context.getBean(ConnectionPool.class);
PoolMetrics poolMetrics = connectionPool.getMetrics().get();
assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15);
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3));
connectionPool.warmup().block();
try {
PoolMetrics poolMetrics = connectionPool.getMetrics().get();
assertThat(poolMetrics.idleSize()).isEqualTo(1);
assertThat(poolMetrics.getMaxAllocatedSize()).isEqualTo(15);
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxAcquireTime", Duration.ofMinutes(3));
assertThat(connectionPool).hasFieldOrPropertyWithValue("maxValidationTime", Duration.ofSeconds(1));
}
finally {
connectionPool.close().block();
}
});
}

Expand Down

0 comments on commit 947ac8d

Please sign in to comment.