Merge pull request #23137 from anshlykov

* pr/23137:
  Polish "Add separate property for Redis read and connection timeout"
  Add separate property for Redis read and connection timeout

Closes gh-23137
pull/23256/head
Stephane Nicoll 4 years ago
commit 26daf3714e

@ -17,7 +17,6 @@
package org.springframework.boot.autoconfigure.data.redis; package org.springframework.boot.autoconfigure.data.redis;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.time.Duration;
import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPool;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
@ -27,6 +26,7 @@ import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.PropertyMapper;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration; import org.springframework.data.redis.connection.RedisClusterConfiguration;
@ -89,16 +89,11 @@ class JedisConnectionConfiguration extends RedisConnectionConfiguration {
} }
private JedisClientConfigurationBuilder applyProperties(JedisClientConfigurationBuilder builder) { private JedisClientConfigurationBuilder applyProperties(JedisClientConfigurationBuilder builder) {
if (getProperties().isSsl()) { PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
builder.useSsl(); map.from(getProperties().isSsl()).whenTrue().toCall(builder::useSsl);
} map.from(getProperties().getTimeout()).to(builder::readTimeout);
if (getProperties().getTimeout() != null) { map.from(getProperties().getConnectTimeout()).to(builder::connectTimeout);
Duration timeout = getProperties().getTimeout(); map.from(getProperties().getClientName()).whenHasText().to(builder::clientName);
builder.readTimeout(timeout).connectTimeout(timeout);
}
if (StringUtils.hasText(getProperties().getClientName())) {
builder.clientName(getProperties().getClientName());
}
return builder; return builder;
} }

@ -17,9 +17,11 @@
package org.springframework.boot.autoconfigure.data.redis; package org.springframework.boot.autoconfigure.data.redis;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.time.Duration;
import io.lettuce.core.ClientOptions; import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisClient; import io.lettuce.core.RedisClient;
import io.lettuce.core.SocketOptions;
import io.lettuce.core.TimeoutOptions; import io.lettuce.core.TimeoutOptions;
import io.lettuce.core.cluster.ClusterClientOptions; import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions; import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
@ -96,7 +98,7 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
if (StringUtils.hasText(getProperties().getUrl())) { if (StringUtils.hasText(getProperties().getUrl())) {
customizeConfigurationFromUrl(builder); customizeConfigurationFromUrl(builder);
} }
builder.clientOptions(initializeClientOptionsBuilder().timeoutOptions(TimeoutOptions.enabled()).build()); builder.clientOptions(createClientOptions());
builder.clientResources(clientResources); builder.clientResources(clientResources);
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build(); return builder.build();
@ -129,6 +131,15 @@ class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
return builder; return builder;
} }
private ClientOptions createClientOptions() {
ClientOptions.Builder builder = initializeClientOptionsBuilder();
Duration connectTimeout = getProperties().getConnectTimeout();
if (connectTimeout != null) {
builder.socketOptions(SocketOptions.builder().connectTimeout(connectTimeout).build());
}
return builder.timeoutOptions(TimeoutOptions.enabled()).build();
}
private ClientOptions.Builder initializeClientOptionsBuilder() { private ClientOptions.Builder initializeClientOptionsBuilder() {
if (getProperties().getCluster() != null) { if (getProperties().getCluster() != null) {
ClusterClientOptions.Builder builder = ClusterClientOptions.builder(); ClusterClientOptions.Builder builder = ClusterClientOptions.builder();

@ -67,10 +67,15 @@ public class RedisProperties {
private boolean ssl; private boolean ssl;
/** /**
* Connection timeout. * Read timeout.
*/ */
private Duration timeout; private Duration timeout;
/**
* Connection timeout.
*/
private Duration connectTimeout;
/** /**
* Client name to be set on connections with CLIENT SETNAME. * Client name to be set on connections with CLIENT SETNAME.
*/ */
@ -145,6 +150,14 @@ public class RedisProperties {
return this.timeout; return this.timeout;
} }
public Duration getConnectTimeout() {
return this.connectTimeout;
}
public void setConnectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
}
public String getClientName() { public String getClientName() {
return this.clientName; return this.clientName;
} }

@ -136,11 +136,23 @@ class RedisAutoConfigurationJedisTests {
} }
@Test @Test
void testRedisConfigurationWithTimeout() { void testRedisConfigurationWithTimeoutAndConnectTimeout() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:100").run((context) -> { this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250",
"spring.redis.connect-timeout:1000").run((context) -> {
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class); JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo"); assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getTimeout()).isEqualTo(100); assertThat(cf.getTimeout()).isEqualTo(250);
assertThat(cf.getClientConfiguration().getConnectTimeout().toMillis()).isEqualTo(1000);
});
}
@Test
void testRedisConfigurationWithDefaultTimeouts() {
this.contextRunner.withPropertyValues("spring.redis.host:foo").run((context) -> {
JedisConnectionFactory cf = context.getBean(JedisConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getTimeout()).isEqualTo(2000);
assertThat(cf.getClientConfiguration().getConnectTimeout().toMillis()).isEqualTo(2000);
}); });
} }

@ -168,11 +168,25 @@ class RedisAutoConfigurationTests {
} }
@Test @Test
void testRedisConfigurationWithTimeout() { void testRedisConfigurationWithTimeoutAndConnectTimeout() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:100").run((context) -> { this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.timeout:250",
"spring.redis.connect-timeout:1000").run((context) -> {
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class); LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo"); assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getTimeout()).isEqualTo(100); assertThat(cf.getTimeout()).isEqualTo(250);
assertThat(cf.getClientConfiguration().getClientOptions().get().getSocketOptions()
.getConnectTimeout().toMillis()).isEqualTo(1000);
});
}
@Test
void testRedisConfigurationWithDefaultTimeouts() {
this.contextRunner.withPropertyValues("spring.redis.host:foo").run((context) -> {
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
assertThat(cf.getHostName()).isEqualTo("foo");
assertThat(cf.getTimeout()).isEqualTo(60000);
assertThat(cf.getClientConfiguration().getClientOptions().get().getSocketOptions().getConnectTimeout()
.toMillis()).isEqualTo(10000);
}); });
} }

Loading…
Cancel
Save