Skip to content

Commit 8c5c0fe

Browse files
Jens Green OlanderJohan Walles
authored andcommitted
Replace ConnectionGetters with lambdas
1 parent cba6832 commit 8c5c0fe

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

src/main/java/redis/clients/jedis/JedisClusterCommand.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package redis.clients.jedis;
22

3+
import java.util.function.Supplier;
34
import redis.clients.jedis.exceptions.JedisAskDataException;
45
import redis.clients.jedis.exceptions.JedisClusterMaxAttemptsException;
56
import redis.clients.jedis.exceptions.JedisClusterOperationException;
@@ -79,36 +80,27 @@ public T runWithAnyNode() {
7980
}
8081
}
8182

82-
private interface ConnectionGetter {
83-
Jedis getConnection();
84-
}
85-
8683
private T runWithRetries(final int slot) {
87-
ConnectionGetter connectionGetter = new ConnectionGetter() {
88-
@Override
89-
public Jedis getConnection() {
90-
return connectionHandler.getConnectionFromSlot(slot);
91-
}
92-
};
84+
Supplier<Jedis> connectionSupplier = () -> connectionHandler.getConnectionFromSlot(slot);
9385

9486
// If we got one redirection, stick with that and don't try anything else
95-
ConnectionGetter redirected = null;
87+
Supplier<Jedis> redirectionSupplier = null;
9688

9789
for (int currentAttempt = 0; currentAttempt < this.maxAttempts; currentAttempt++) {
9890
Jedis connection = null;
9991
try {
100-
if (redirected != null) {
101-
connection = redirected.getConnection();
92+
if (redirectionSupplier != null) {
93+
connection = redirectionSupplier.get();
10294
} else {
103-
connection = connectionGetter.getConnection();
95+
connection = connectionSupplier.get();
10496
}
10597
return execute(connection);
10698
} catch (JedisNoReachableClusterNodeException e) {
10799
throw e;
108100
} catch (JedisConnectionException e) {
109-
connectionGetter = handleConnectionProblem(slot, currentAttempt);
101+
connectionSupplier = handleConnectionProblem(slot, currentAttempt);
110102
} catch (JedisRedirectionException e) {
111-
redirected = handleRedirection(connection, e);
103+
redirectionSupplier = handleRedirection(connection, e);
112104
} finally {
113105
releaseConnection(connection);
114106
}
@@ -117,7 +109,7 @@ public Jedis getConnection() {
117109
throw new JedisClusterMaxAttemptsException("No more cluster attempts left.");
118110
}
119111

120-
private ConnectionGetter handleConnectionProblem(final int slot, int currentAttempt) {
112+
private Supplier<Jedis> handleConnectionProblem(final int slot, int currentAttempt) {
121113
int attemptsLeft = (maxAttempts - currentAttempt) - 1;
122114
if (attemptsLeft <= 1) {
123115
//We need this because if node is not reachable anymore - we need to finally initiate slots
@@ -128,15 +120,10 @@ private ConnectionGetter handleConnectionProblem(final int slot, int currentAtte
128120
this.connectionHandler.renewSlotCache();
129121
}
130122

131-
return new ConnectionGetter() {
132-
@Override
133-
public Jedis getConnection() {
134-
return connectionHandler.getConnectionFromSlot(slot);
135-
}
136-
};
123+
return () -> connectionHandler.getConnectionFromSlot(slot);
137124
}
138125

139-
private ConnectionGetter handleRedirection(Jedis connection, final JedisRedirectionException jre) {
126+
private Supplier<Jedis> handleRedirection(Jedis connection, final JedisRedirectionException jre) {
140127
// if MOVED redirection occurred,
141128
if (jre instanceof JedisMovedDataException) {
142129
// it rebuilds cluster's slot cache recommended by Redis cluster specification
@@ -146,17 +133,14 @@ private ConnectionGetter handleRedirection(Jedis connection, final JedisRedirect
146133
// release current connection before iteration
147134
releaseConnection(connection);
148135

149-
return new ConnectionGetter() {
150-
@Override
151-
public Jedis getConnection() {
152-
Jedis connection = connectionHandler.getConnectionFromNode(jre.getTargetNode());
153-
if (jre instanceof JedisAskDataException) {
154-
// TODO: Pipeline asking with the original command to make it faster....
155-
connection.asking();
156-
}
157-
158-
return connection;
136+
return () -> {
137+
Jedis redirectedConnection = connectionHandler.getConnectionFromNode(jre.getTargetNode());
138+
if (jre instanceof JedisAskDataException) {
139+
// TODO: Pipeline asking with the original command to make it faster....
140+
redirectedConnection.asking();
159141
}
142+
143+
return redirectedConnection;
160144
};
161145
}
162146

0 commit comments

Comments
 (0)