Merge pull request #9526 from mp911de:set-lettuce-shutdown-timeout-properly

* pr/9526:
  Polish "Set Lettuce shutdown timeout properly"
  Reduce Lettuce shutdown timeout to 100ms
  Set Lettuce shutdown timeout properly
This commit is contained in:
Stephane Nicoll 2017-06-15 14:00:10 +02:00
commit 865971a540
4 changed files with 19 additions and 7 deletions

View File

@ -85,8 +85,16 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
private LettuceConnectionFactory createLettuceConnectionFactory( private LettuceConnectionFactory createLettuceConnectionFactory(
ClientResources clientResources) { ClientResources clientResources) {
return new LettuceConnectionFactory(applyProperties(createLettucePool(), return applyProperties(
this.properties.getLettuce().getPool(), clientResources)); new LettuceConnectionFactory(applyProperties(createLettucePool(),
this.properties.getLettuce().getPool(), clientResources)));
}
private LettuceConnectionFactory applyProperties(
LettuceConnectionFactory connectionFactory) {
connectionFactory
.setShutdownTimeout(this.properties.getLettuce().getShutdownTimeout());
return connectionFactory;
} }
private DefaultLettucePool createLettucePool() { private DefaultLettucePool createLettucePool() {
@ -173,7 +181,7 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
RedisProperties.Lettuce lettuce = this.properties.getLettuce(); RedisProperties.Lettuce lettuce = this.properties.getLettuce();
if (lettuce.getShutdownTimeout() >= 0) { if (lettuce.getShutdownTimeout() >= 0) {
builder.shutdownTimeout(Duration builder.shutdownTimeout(Duration
.ofSeconds(this.properties.getLettuce().getShutdownTimeout())); .ofMillis(this.properties.getLettuce().getShutdownTimeout()));
} }
} }
return builder; return builder;

View File

@ -316,7 +316,7 @@ public class RedisProperties {
/** /**
* Shutdown timeout in milliseconds. * Shutdown timeout in milliseconds.
*/ */
private int shutdownTimeout = 2000; private int shutdownTimeout = 100;
/** /**
* Lettuce pool configuration. * Lettuce pool configuration.

View File

@ -72,13 +72,15 @@ public class RedisAutoConfigurationTests {
@Test @Test
public void testOverrideRedisConfiguration() { public void testOverrideRedisConfiguration() {
load("spring.redis.host:foo", "spring.redis.database:1"); load("spring.redis.host:foo", "spring.redis.database:1",
"spring.redis.lettuce.shutdown-timeout:500");
LettuceConnectionFactory cf = this.context LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class); .getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo"); assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getDatabase()).isEqualTo(1); assertThat(cf.getDatabase()).isEqualTo(1);
assertThat(cf.getPassword()).isNull(); assertThat(cf.getPassword()).isNull();
assertThat(cf.isUseSsl()).isFalse(); assertThat(cf.isUseSsl()).isFalse();
assertThat(cf.getShutdownTimeout()).isEqualTo(500);
} }
@Test @Test
@ -118,7 +120,8 @@ public class RedisAutoConfigurationTests {
load("spring.redis.host:foo", "spring.redis.lettuce.pool.min-idle:1", load("spring.redis.host:foo", "spring.redis.lettuce.pool.min-idle:1",
"spring.redis.lettuce.pool.max-idle:4", "spring.redis.lettuce.pool.max-idle:4",
"spring.redis.lettuce.pool.max-active:16", "spring.redis.lettuce.pool.max-active:16",
"spring.redis.lettuce.pool.max-wait:2000"); "spring.redis.lettuce.pool.max-wait:2000",
"spring.redis.lettuce.shutdown-timeout:1000");
LettuceConnectionFactory cf = this.context LettuceConnectionFactory cf = this.context
.getBean(LettuceConnectionFactory.class); .getBean(LettuceConnectionFactory.class);
assertThat(getDefaultLettucePool(cf).getHostName()).isEqualTo("foo"); assertThat(getDefaultLettucePool(cf).getHostName()).isEqualTo("foo");
@ -127,6 +130,7 @@ public class RedisAutoConfigurationTests {
assertThat(getDefaultLettucePool(cf).getPoolConfig().getMaxTotal()).isEqualTo(16); assertThat(getDefaultLettucePool(cf).getPoolConfig().getMaxTotal()).isEqualTo(16);
assertThat(getDefaultLettucePool(cf).getPoolConfig().getMaxWaitMillis()) assertThat(getDefaultLettucePool(cf).getPoolConfig().getMaxWaitMillis())
.isEqualTo(2000); .isEqualTo(2000);
assertThat(cf.getShutdownTimeout()).isEqualTo(1000);
} }
@Test @Test

View File

@ -886,7 +886,7 @@ content into your application; rather pick only the properties that you need.
spring.redis.lettuce.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections. spring.redis.lettuce.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.lettuce.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely. spring.redis.lettuce.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.lettuce.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive. spring.redis.lettuce.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.lettuce.shutdown-timeout=2000 # Shutdown timeout in milliseconds. spring.redis.lettuce.shutdown-timeout=100 # Shutdown timeout in milliseconds.
spring.redis.password= # Login password of the redis server. spring.redis.password= # Login password of the redis server.
spring.redis.port=6379 # Redis server port. spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of Redis server. spring.redis.sentinel.master= # Name of Redis server.