Skip to content

Commit 7c20dd2

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-676 - Pass on configured command timeout to LettuceClusterConnection.
We now not only configure the underlying lettuce connection with the command timeout set via the connection factory, but also make sure to pass the option on to the LettuceClusterConnection. Original pull request: spring-projects#266.
1 parent 65d320b commit 7c20dd2

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceClusterConnection.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.redis.connection.lettuce;
1717

1818
import io.lettuce.core.RedisException;
19+
import io.lettuce.core.RedisURI;
1920
import io.lettuce.core.api.StatefulConnection;
2021
import io.lettuce.core.api.sync.BaseRedisCommands;
2122
import io.lettuce.core.cluster.RedisClusterClient;
@@ -26,6 +27,7 @@
2627
import io.lettuce.core.codec.RedisCodec;
2728
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
2829

30+
import java.time.Duration;
2931
import java.util.ArrayList;
3032
import java.util.Collection;
3133
import java.util.LinkedHashMap;
@@ -65,36 +67,47 @@ public class LettuceClusterConnection extends LettuceConnection implements Defau
6567
private final RedisClusterClient clusterClient;
6668
private ClusterCommandExecutor clusterCommandExecutor;
6769
private ClusterTopologyProvider topologyProvider;
68-
private final boolean disposeClusterCommandExecutorOnClose;
70+
private boolean disposeClusterCommandExecutorOnClose;
6971

7072
/**
71-
* Creates new {@link LettuceClusterConnection} using {@link RedisClusterClient}.
73+
* Creates new {@link LettuceClusterConnection} using {@link RedisClusterClient} with default
74+
* {@link RedisURI#DEFAULT_TIMEOUT_DURATION timeout} and a fresh {@link ClusterCommandExecutor} that gets destroyed on
75+
* close.
7276
*
7377
* @param clusterClient must not be {@literal null}.
7478
*/
7579
public LettuceClusterConnection(RedisClusterClient clusterClient) {
7680

77-
super(null, 100, clusterClient, null, 0);
81+
this(clusterClient, RedisURI.DEFAULT_TIMEOUT_DURATION,
82+
new ClusterCommandExecutor(new LettuceClusterTopologyProvider(clusterClient),
83+
new LettuceClusterNodeResourceProvider(clusterClient), exceptionConverter));
7884

79-
Assert.notNull(clusterClient, "RedisClusterClient must not be null.");
80-
81-
this.clusterClient = clusterClient;
82-
topologyProvider = new LettuceClusterTopologyProvider(clusterClient);
83-
clusterCommandExecutor = new ClusterCommandExecutor(topologyProvider,
84-
new LettuceClusterNodeResourceProvider(clusterClient), exceptionConverter);
85-
disposeClusterCommandExecutorOnClose = true;
85+
this.disposeClusterCommandExecutorOnClose = true;
8686
}
8787

8888
/**
89-
* Creates new {@link LettuceClusterConnection} using {@link RedisClusterClient} running commands across the cluster
90-
* via given {@link ClusterCommandExecutor}.
89+
* Creates new {@link LettuceClusterConnection} with default {@link RedisURI#DEFAULT_TIMEOUT_DURATION timeout} using
90+
* {@link RedisClusterClient} running commands across the cluster via given {@link ClusterCommandExecutor}.
9191
*
9292
* @param clusterClient must not be {@literal null}.
9393
* @param executor must not be {@literal null}.
9494
*/
9595
public LettuceClusterConnection(RedisClusterClient clusterClient, ClusterCommandExecutor executor) {
96+
this(clusterClient, RedisURI.DEFAULT_TIMEOUT_DURATION, executor);
97+
}
98+
99+
/**
100+
* Creates new {@link LettuceClusterConnection} with given command {@code timeout} using {@link RedisClusterClient}
101+
* running commands across the cluster via given {@link ClusterCommandExecutor}.
102+
*
103+
* @param clusterClient must not be {@literal null}.
104+
* @param timeout must not be {@literal null}.
105+
* @param executor must not be {@literal null}.
106+
* @since 2.0
107+
*/
108+
public LettuceClusterConnection(RedisClusterClient clusterClient, Duration timeout, ClusterCommandExecutor executor) {
96109

97-
super(null, 100, clusterClient, null, 0);
110+
super(null, timeout.toMillis(), clusterClient, null, 0);
98111

99112
Assert.notNull(clusterClient, "RedisClusterClient must not be null.");
100113
Assert.notNull(executor, "ClusterCommandExecutor must not be null.");

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ public RedisClusterConnection getClusterConnection() {
272272
throw new InvalidDataAccessApiUsageException("Cluster is not configured!");
273273
}
274274

275-
return new LettuceClusterConnection((RedisClusterClient) client, clusterCommandExecutor);
275+
return new LettuceClusterConnection((RedisClusterClient) client, Duration.ofMillis(getTimeout()),
276+
clusterCommandExecutor);
276277
}
277278

278279
/*

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.data.redis.connection.RedisPassword;
4444
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
4545
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
46+
import org.springframework.test.util.ReflectionTestUtils;
4647

4748
/**
4849
* Unit tests for {@link LettuceConnectionFactory}.
@@ -486,4 +487,23 @@ public void shouldDenyChangesToImmutableClientConfiguration() throws NoSuchAlgor
486487

487488
connectionFactory.setUseSsl(false);
488489
}
490+
491+
@Test // DATAREDIS-676
492+
public void timeoutShouldBePassedOnToClusterConnection() {
493+
494+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig);
495+
connectionFactory.setTimeout(2000);
496+
connectionFactory.afterPropertiesSet();
497+
498+
assertThat(ReflectionTestUtils.getField(connectionFactory.getClusterConnection(), "timeout"), is(equalTo(2000L)));
499+
}
500+
501+
@Test // DATAREDIS-676
502+
public void timeoutSetOnClientConfigShouldBePassedOnToClusterConnection() {
503+
504+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig, LettuceClientConfiguration.builder().commandTimeout(Duration.ofSeconds(2)).build());
505+
connectionFactory.afterPropertiesSet();
506+
507+
assertThat(ReflectionTestUtils.getField(connectionFactory.getClusterConnection(), "timeout"), is(equalTo(2000L)));
508+
}
489509
}

0 commit comments

Comments
 (0)