Skip to content

Commit 1659d46

Browse files
geminiKimwilkinsona
authored andcommitted
Enable minIdle by allowing timeBetweenEviction runs to be configured
See gh-16703
1 parent 509d338 commit 1659d46

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ private JedisPoolConfig jedisPoolConfig(RedisProperties.Pool pool) {
114114
config.setMaxTotal(pool.getMaxActive());
115115
config.setMaxIdle(pool.getMaxIdle());
116116
config.setMinIdle(pool.getMinIdle());
117+
if (pool.getTimeBetweenEvictionRuns() != null) {
118+
config.setTimeBetweenEvictionRunsMillis(
119+
pool.getTimeBetweenEvictionRuns().toMillis());
120+
}
117121
if (pool.getMaxWait() != null) {
118122
config.setMaxWaitMillis(pool.getMaxWait().toMillis());
119123
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ private GenericObjectPoolConfig<?> getPoolConfig(Pool properties) {
155155
config.setMaxTotal(properties.getMaxActive());
156156
config.setMaxIdle(properties.getMaxIdle());
157157
config.setMinIdle(properties.getMinIdle());
158+
if (properties.getTimeBetweenEvictionRuns() != null) {
159+
config.setTimeBetweenEvictionRunsMillis(
160+
properties.getTimeBetweenEvictionRuns().toMillis());
161+
}
158162
if (properties.getMaxWait() != null) {
159163
config.setMaxWaitMillis(properties.getMaxWait().toMillis());
160164
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public static class Pool {
172172
/**
173173
* Target for the minimum number of idle connections to maintain in the pool. This
174174
* setting only has an effect if it is positive.
175+
*
176+
* This setting only has an effect if it is positive and `timeBetweenEvictionRuns`
177+
* is greater than zero.
175178
*/
176179
private int minIdle = 0;
177180

@@ -188,6 +191,14 @@ public static class Pool {
188191
*/
189192
private Duration maxWait = Duration.ofMillis(-1);
190193

194+
/**
195+
* Time to sleep between runs of the idle object evictor thread.
196+
*
197+
* When positive, the idle object evictor thread starts. When non-positive, no
198+
* idle object evictor thread runs.
199+
*/
200+
private Duration timeBetweenEvictionRuns;
201+
191202
public int getMaxIdle() {
192203
return this.maxIdle;
193204
}
@@ -220,6 +231,14 @@ public void setMaxWait(Duration maxWait) {
220231
this.maxWait = maxWait;
221232
}
222233

234+
public Duration getTimeBetweenEvictionRuns() {
235+
return this.timeBetweenEvictionRuns;
236+
}
237+
238+
public void setTimeBetweenEvictionRuns(Duration timeBetweenEvictionRuns) {
239+
this.timeBetweenEvictionRuns = timeBetweenEvictionRuns;
240+
}
241+
223242
}
224243

225244
/**

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,23 @@ public void testPasswordInUrlStartsWithColon() {
134134

135135
@Test
136136
public void testRedisConfigurationWithPool() {
137-
this.contextRunner.withPropertyValues("spring.redis.host:foo",
138-
"spring.redis.jedis.pool.min-idle:1",
139-
"spring.redis.jedis.pool.max-idle:4",
140-
"spring.redis.jedis.pool.max-active:16",
141-
"spring.redis.jedis.pool.max-wait:2000").run((context) -> {
137+
this.contextRunner
138+
.withPropertyValues("spring.redis.host:foo",
139+
"spring.redis.jedis.pool.min-idle:1",
140+
"spring.redis.jedis.pool.max-idle:4",
141+
"spring.redis.jedis.pool.max-active:16",
142+
"spring.redis.jedis.pool.max-wait:2000",
143+
"spring.redis.jedis.pool.time-between-eviction-runs:30000")
144+
.run((context) -> {
142145
JedisConnectionFactory cf = context
143146
.getBean(JedisConnectionFactory.class);
144147
assertThat(cf.getHostName()).isEqualTo("foo");
145148
assertThat(cf.getPoolConfig().getMinIdle()).isEqualTo(1);
146149
assertThat(cf.getPoolConfig().getMaxIdle()).isEqualTo(4);
147150
assertThat(cf.getPoolConfig().getMaxTotal()).isEqualTo(16);
148151
assertThat(cf.getPoolConfig().getMaxWaitMillis()).isEqualTo(2000);
152+
assertThat(cf.getPoolConfig().getTimeBetweenEvictionRunsMillis())
153+
.isEqualTo(30000);
149154
});
150155
}
151156

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public void testRedisConfigurationWithPool() {
155155
"spring.redis.lettuce.pool.max-idle:4",
156156
"spring.redis.lettuce.pool.max-active:16",
157157
"spring.redis.lettuce.pool.max-wait:2000",
158+
"spring.redis.lettuce.pool.time-between-eviction-runs:30000",
158159
"spring.redis.lettuce.shutdown-timeout:1000").run((context) -> {
159160
LettuceConnectionFactory cf = context
160161
.getBean(LettuceConnectionFactory.class);
@@ -165,6 +166,8 @@ public void testRedisConfigurationWithPool() {
165166
assertThat(poolConfig.getMaxIdle()).isEqualTo(4);
166167
assertThat(poolConfig.getMaxTotal()).isEqualTo(16);
167168
assertThat(poolConfig.getMaxWaitMillis()).isEqualTo(2000);
169+
assertThat(poolConfig.getTimeBetweenEvictionRunsMillis())
170+
.isEqualTo(30000);
168171
assertThat(cf.getShutdownTimeout()).isEqualTo(1000);
169172
});
170173
}

0 commit comments

Comments
 (0)