Skip to content

Commit 55ff163

Browse files
weixsunsnicoll
authored andcommitted
Enable Redis connection pool if commons-pool2 is available
See gh-26326
1 parent a72da74 commit 55ff163

File tree

6 files changed

+71
-22
lines changed

6 files changed

+71
-22
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -41,6 +41,7 @@
4141
*
4242
* @author Mark Paluch
4343
* @author Stephane Nicoll
44+
* @author Weix Sun
4445
*/
4546
@Configuration(proxyBeanMethods = false)
4647
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
@@ -76,7 +77,7 @@ private JedisClientConfiguration getJedisClientConfiguration(
7677
ObjectProvider<JedisClientConfigurationBuilderCustomizer> builderCustomizers) {
7778
JedisClientConfigurationBuilder builder = applyProperties(JedisClientConfiguration.builder());
7879
RedisProperties.Pool pool = getProperties().getJedis().getPool();
79-
if (pool != null) {
80+
if (pool.isEnabled()) {
8081
applyPooling(pool, builder);
8182
}
8283
if (StringUtils.hasText(getProperties().getUrl())) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -51,6 +51,7 @@
5151
*
5252
* @author Mark Paluch
5353
* @author Andy Wilkinson
54+
* @author Weix Sun
5455
*/
5556
@Configuration(proxyBeanMethods = false)
5657
@ConditionalOnClass(RedisClient.class)
@@ -104,10 +105,10 @@ private LettuceClientConfiguration getLettuceClientConfiguration(
104105
}
105106

106107
private LettuceClientConfigurationBuilder createBuilder(Pool pool) {
107-
if (pool == null) {
108-
return LettuceClientConfiguration.builder();
108+
if (pool.isEnabled()) {
109+
return new PoolBuilderFactory().createBuilder(pool);
109110
}
110-
return new PoolBuilderFactory().createBuilder(pool);
111+
return LettuceClientConfiguration.builder();
111112
}
112113

113114
private LettuceClientConfigurationBuilder applyProperties(

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -233,6 +233,8 @@ public enum ClientType {
233233
*/
234234
public static class Pool {
235235

236+
private boolean enabled;
237+
236238
/**
237239
* Maximum number of "idle" connections in the pool. Use a negative value to
238240
* indicate an unlimited number of idle connections.
@@ -265,6 +267,14 @@ public static class Pool {
265267
*/
266268
private Duration timeBetweenEvictionRuns;
267269

270+
public boolean isEnabled() {
271+
return this.enabled;
272+
}
273+
274+
public void setEnabled(boolean enabled) {
275+
this.enabled = enabled;
276+
}
277+
268278
public int getMaxIdle() {
269279
return this.maxIdle;
270280
}
@@ -396,14 +406,16 @@ public static class Jedis {
396406
/**
397407
* Jedis pool configuration.
398408
*/
399-
private Pool pool;
409+
private final Pool pool;
400410

401-
public Pool getPool() {
402-
return this.pool;
411+
public Jedis() {
412+
Pool pool = new Pool();
413+
pool.setEnabled(true);
414+
this.pool = pool;
403415
}
404416

405-
public void setPool(Pool pool) {
406-
this.pool = pool;
417+
public Pool getPool() {
418+
return this.pool;
407419
}
408420

409421
}
@@ -421,7 +433,7 @@ public static class Lettuce {
421433
/**
422434
* Lettuce pool configuration.
423435
*/
424-
private Pool pool;
436+
private final Pool pool = new Pool();
425437

426438
private final Cluster cluster = new Cluster();
427439

@@ -437,10 +449,6 @@ public Pool getPool() {
437449
return this.pool;
438450
}
439451

440-
public void setPool(Pool pool) {
441-
this.pool = pool;
442-
}
443-
444452
public Cluster getCluster() {
445453
return this.cluster;
446454
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*
3737
* @author Mark Paluch
3838
* @author Stephane Nicoll
39+
* @author Weix Sun
3940
*/
4041
@ClassPathExclusions("lettuce-core-*.jar")
4142
class RedisAutoConfigurationJedisTests {
@@ -142,6 +143,16 @@ void testRedisConfigurationWithPool() {
142143
});
143144
}
144145

146+
@Test
147+
void testRedisConfigurationDisabledPool() {
148+
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.jedis.pool.enabled:false")
149+
.run((context) -> {
150+
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
151+
assertThat(cf.getHostName()).isEqualTo("foo");
152+
assertThat(cf.getClientConfiguration().isUsePooling()).isEqualTo(false);
153+
});
154+
}
155+
145156
@Test
146157
void testRedisConfigurationWithTimeoutAndConnectTimeout() {
147158
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250",

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -65,6 +65,7 @@
6565
* @author Stephane Nicoll
6666
* @author Alen Turkovic
6767
* @author Scott Frederick
68+
* @author Weix Sun
6869
*/
6970
class RedisAutoConfigurationTests {
7071

@@ -155,10 +156,25 @@ void testPasswordInUrlStartsWithColon() {
155156
}
156157

157158
@Test
158-
void testRedisConfigurationWithPool() {
159-
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.min-idle:1",
160-
"spring.redis.lettuce.pool.max-idle:4", "spring.redis.lettuce.pool.max-active:16",
161-
"spring.redis.lettuce.pool.max-wait:2000", "spring.redis.lettuce.pool.time-between-eviction-runs:30000",
159+
void testRedisConfigurationWithPoolUsingDefaultValue() {
160+
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:true")
161+
.run((context) -> {
162+
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
163+
assertThat(cf.getHostName()).isEqualTo("foo");
164+
GenericObjectPoolConfig<?> poolConfig = getPoolingClientConfiguration(cf).getPoolConfig();
165+
assertThat(poolConfig.getMinIdle()).isEqualTo(0);
166+
assertThat(poolConfig.getMaxIdle()).isEqualTo(8);
167+
assertThat(poolConfig.getMaxTotal()).isEqualTo(8);
168+
assertThat(poolConfig.getMaxWaitMillis()).isEqualTo(-1);
169+
});
170+
}
171+
172+
@Test
173+
void testRedisConfigurationWithPoolUsingCustomValue() {
174+
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:true",
175+
"spring.redis.lettuce.pool.min-idle:1", "spring.redis.lettuce.pool.max-idle:4",
176+
"spring.redis.lettuce.pool.max-active:16", "spring.redis.lettuce.pool.max-wait:2000",
177+
"spring.redis.lettuce.pool.time-between-eviction-runs:30000",
162178
"spring.redis.lettuce.shutdown-timeout:1000").run((context) -> {
163179
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
164180
assertThat(cf.getHostName()).isEqualTo("foo");
@@ -172,6 +188,17 @@ void testRedisConfigurationWithPool() {
172188
});
173189
}
174190

191+
@Test
192+
void testRedisConfigurationDisabledPool() {
193+
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.lettuce.pool.enabled:false")
194+
.run((context) -> {
195+
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
196+
assertThat(cf.getHostName()).isEqualTo("foo");
197+
assertThat(ReflectionTestUtils.getField(cf, "clientConfiguration"))
198+
.isNotInstanceOf(LettucePoolingClientConfiguration.class);
199+
});
200+
}
201+
175202
@Test
176203
void testRedisConfigurationWithTimeoutAndConnectTimeout() {
177204
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.redis.lettuce.pool.enabled=false

0 commit comments

Comments
 (0)