Skip to content

Commit 75f4896

Browse files
committed
Merge pull request #16703 from Gemini Kim
* gh-16703: Polish "Enable minIdle by allowing timeBetweenEviction runs to be configured" Enable minIdle by allowing timeBetweenEviction runs to be configured Closes gh-16703
2 parents 509d338 + 96f3a48 commit 75f4896

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -171,7 +171,8 @@ public static class Pool {
171171

172172
/**
173173
* Target for the minimum number of idle connections to maintain in the pool. This
174-
* setting only has an effect if it is positive.
174+
* setting only has an effect if both it and time between eviction runs are
175+
* positive.
175176
*/
176177
private int minIdle = 0;
177178

@@ -188,6 +189,12 @@ public static class Pool {
188189
*/
189190
private Duration maxWait = Duration.ofMillis(-1);
190191

192+
/**
193+
* Time between runs of the idle object evictor thread. When positive, the idle
194+
* object evictor thread starts, otherwise no idle object eviction is performed.
195+
*/
196+
private Duration timeBetweenEvictionRuns;
197+
191198
public int getMaxIdle() {
192199
return this.maxIdle;
193200
}
@@ -220,6 +227,14 @@ public void setMaxWait(Duration maxWait) {
220227
this.maxWait = maxWait;
221228
}
222229

230+
public Duration getTimeBetweenEvictionRuns() {
231+
return this.timeBetweenEvictionRuns;
232+
}
233+
234+
public void setTimeBetweenEvictionRuns(Duration timeBetweenEvictionRuns) {
235+
this.timeBetweenEvictionRuns = timeBetweenEvictionRuns;
236+
}
237+
223238
}
224239

225240
/**

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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)