Skip to content

Commit 4e90311

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: #266.
1 parent 17fc8fa commit 4e90311

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 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.
@@ -61,6 +61,7 @@
6161

6262
import com.lambdaworks.redis.KeyValue;
6363
import com.lambdaworks.redis.RedisException;
64+
import com.lambdaworks.redis.RedisURI;
6465
import com.lambdaworks.redis.api.StatefulConnection;
6566
import com.lambdaworks.redis.cluster.RedisClusterClient;
6667
import com.lambdaworks.redis.cluster.SlotHash;
@@ -87,36 +88,46 @@ public class LettuceClusterConnection extends LettuceConnection
8788
private final RedisClusterClient clusterClient;
8889
private ClusterCommandExecutor clusterCommandExecutor;
8990
private ClusterTopologyProvider topologyProvider;
90-
private final boolean disposeClusterCommandExecutorOnClose;
91+
private boolean disposeClusterCommandExecutorOnClose;
9192

9293
/**
93-
* Creates new {@link LettuceClusterConnection} using {@link RedisClusterClient}.
94+
* Creates new {@link LettuceClusterConnection} using {@link RedisClusterClient} with default
95+
* {@link RedisURI#DEFAULT_TIMEOUT timeout} and a fresh {@link ClusterCommandExecutor} that gets destroyed on close.
9496
*
9597
* @param clusterClient must not be {@literal null}.
9698
*/
9799
public LettuceClusterConnection(RedisClusterClient clusterClient) {
98100

99-
super(null, 100, clusterClient, null, 0);
101+
this(clusterClient, RedisURI.DEFAULT_TIMEOUT,
102+
new ClusterCommandExecutor(new LettuceClusterTopologyProvider(clusterClient),
103+
new LettuceClusterNodeResourceProvider(clusterClient), exceptionConverter));
100104

101-
Assert.notNull(clusterClient, "RedisClusterClient must not be null.");
102-
103-
this.clusterClient = clusterClient;
104-
topologyProvider = new LettuceClusterTopologyProvider(clusterClient);
105-
clusterCommandExecutor = new ClusterCommandExecutor(topologyProvider,
106-
new LettuceClusterNodeResourceProvider(clusterClient), exceptionConverter);
107-
disposeClusterCommandExecutorOnClose = true;
105+
this.disposeClusterCommandExecutorOnClose = true;
108106
}
109107

110108
/**
111-
* Creates new {@link LettuceClusterConnection} using {@link RedisClusterClient} running commands across the cluster
112-
* via given {@link ClusterCommandExecutor}.
109+
* Creates new {@link LettuceClusterConnection} with default {@link RedisURI#DEFAULT_TIMEOUT timeout} using
110+
* {@link RedisClusterClient} running commands across the cluster via given {@link ClusterCommandExecutor}.
113111
*
114112
* @param clusterClient must not be {@literal null}.
115113
* @param executor must not be {@literal null}.
116114
*/
117115
public LettuceClusterConnection(RedisClusterClient clusterClient, ClusterCommandExecutor executor) {
116+
this(clusterClient, RedisURI.DEFAULT_TIMEOUT, executor);
117+
}
118+
119+
/**
120+
* Creates new {@link LettuceClusterConnection} with given command {@code timeout} using {@link RedisClusterClient}
121+
* running commands across the cluster via given {@link ClusterCommandExecutor}.
122+
*
123+
* @param clusterClient must not be {@literal null}.
124+
* @param timeout command timeout (in milliseconds)
125+
* @param executor must not be {@literal null}.
126+
* @since 1.8.7
127+
*/
128+
public LettuceClusterConnection(RedisClusterClient clusterClient, long timeout, ClusterCommandExecutor executor) {
118129

119-
super(null, 100, clusterClient, null, 0);
130+
super(null, timeout, clusterClient, null, 0);
120131

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2016 the original author or authors.
2+
* Copyright 2011-2017 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.
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.redis.connection.lettuce;
1817

1918
import java.util.ArrayList;
@@ -201,7 +200,7 @@ public RedisClusterConnection getClusterConnection() {
201200
throw new InvalidDataAccessApiUsageException("Cluster is not configured!");
202201
}
203202

204-
return new LettuceClusterConnection((RedisClusterClient) client, clusterCommandExecutor);
203+
return new LettuceClusterConnection((RedisClusterClient) client, getTimeout(), clusterCommandExecutor);
205204
}
206205

207206
public void initConnection() {
@@ -577,7 +576,8 @@ private AbstractRedisClient createRedisClient() {
577576
}
578577

579578
RedisClusterClient clusterClient = clientResources != null
580-
? RedisClusterClient.create(clientResources, initialUris) : RedisClusterClient.create(initialUris);
579+
? RedisClusterClient.create(clientResources, initialUris)
580+
: RedisClusterClient.create(initialUris);
581581

582582
this.clusterCommandExecutor = new ClusterCommandExecutor(
583583
new LettuceClusterConnection.LettuceClusterTopologyProvider(clusterClient),

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232
import org.springframework.data.redis.ConnectionFactoryTracker;
3333
import org.springframework.data.redis.connection.RedisClusterConfiguration;
3434
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
35+
import org.springframework.test.util.ReflectionTestUtils;
3536

3637
import com.lambdaworks.redis.AbstractRedisClient;
3738
import com.lambdaworks.redis.RedisClient;
3839
import com.lambdaworks.redis.RedisURI;
3940
import com.lambdaworks.redis.cluster.RedisClusterClient;
41+
import org.springframework.test.util.ReflectionTestUtils;
42+
4043

4144
/**
4245
* @author Christoph Strobl
@@ -275,4 +278,14 @@ public void verifyPeerTLSOptionShouldBeSetCorrectlyOnClusterClient() {
275278
assertThat(uri.isVerifyPeer(), is(true));
276279
}
277280
}
281+
282+
@Test // DATAREDIS-676
283+
public void timeoutShouldBePassedOnToClusterConnection() {
284+
285+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig);
286+
connectionFactory.setTimeout(2000);
287+
connectionFactory.afterPropertiesSet();
288+
289+
assertThat((Long) ReflectionTestUtils.getField(connectionFactory.getClusterConnection(), "timeout"), is(equalTo(2000L)));
290+
}
278291
}

0 commit comments

Comments
 (0)