25
25
import java .util .Iterator ;
26
26
import java .util .ServiceConfigurationError ;
27
27
import java .util .ServiceLoader ;
28
+ import java .util .function .Predicate ;
28
29
import java .util .function .Supplier ;
29
30
30
31
import io .lettuce .core .api .StatefulConnection ;
35
36
import io .lettuce .core .protocol .DecodeBufferPolicy ;
36
37
import io .lettuce .core .protocol .ProtocolVersion ;
37
38
import io .lettuce .core .protocol .ReadOnlyCommands ;
39
+ import io .lettuce .core .protocol .RedisCommand ;
38
40
import io .lettuce .core .resource .ClientResources ;
39
41
import reactor .core .publisher .Mono ;
40
42
@@ -50,6 +52,8 @@ public class ClientOptions implements Serializable {
50
52
51
53
public static final boolean DEFAULT_AUTO_RECONNECT = true ;
52
54
55
+ public static final Predicate <RedisCommand <?, ?, ?>> DEFAULT_REPLAY_FILTER = (cmd ) -> false ;
56
+
53
57
public static final int DEFAULT_BUFFER_USAGE_RATIO = 3 ;
54
58
55
59
public static final boolean DEFAULT_CANCEL_CMD_RECONNECT_FAIL = false ;
@@ -92,6 +96,8 @@ public class ClientOptions implements Serializable {
92
96
93
97
private final boolean autoReconnect ;
94
98
99
+ private final Predicate <RedisCommand <?, ?, ?>> replayFilter ;
100
+
95
101
private final boolean cancelCommandsOnReconnectFailure ;
96
102
97
103
private final DecodeBufferPolicy decodeBufferPolicy ;
@@ -126,6 +132,7 @@ public class ClientOptions implements Serializable {
126
132
127
133
protected ClientOptions (Builder builder ) {
128
134
this .autoReconnect = builder .autoReconnect ;
135
+ this .replayFilter = builder .replayFilter ;
129
136
this .cancelCommandsOnReconnectFailure = builder .cancelCommandsOnReconnectFailure ;
130
137
this .decodeBufferPolicy = builder .decodeBufferPolicy ;
131
138
this .disconnectedBehavior = builder .disconnectedBehavior ;
@@ -146,6 +153,7 @@ protected ClientOptions(Builder builder) {
146
153
147
154
protected ClientOptions (ClientOptions original ) {
148
155
this .autoReconnect = original .isAutoReconnect ();
156
+ this .replayFilter = original .getReplayFilter ();
149
157
this .cancelCommandsOnReconnectFailure = original .isCancelCommandsOnReconnectFailure ();
150
158
this .decodeBufferPolicy = original .getDecodeBufferPolicy ();
151
159
this .disconnectedBehavior = original .getDisconnectedBehavior ();
@@ -199,6 +207,8 @@ public static class Builder {
199
207
200
208
private boolean autoReconnect = DEFAULT_AUTO_RECONNECT ;
201
209
210
+ private Predicate <RedisCommand <?, ?, ?>> replayFilter = DEFAULT_REPLAY_FILTER ;
211
+
202
212
private boolean cancelCommandsOnReconnectFailure = DEFAULT_CANCEL_CMD_RECONNECT_FAIL ;
203
213
204
214
private DecodeBufferPolicy decodeBufferPolicy = DecodeBufferPolicies .ratio (DEFAULT_BUFFER_USAGE_RATIO );
@@ -246,6 +256,21 @@ public Builder autoReconnect(boolean autoReconnect) {
246
256
return this ;
247
257
}
248
258
259
+ /**
260
+ * When {@link #autoReconnect(boolean)} is set to true, this {@link Predicate} is used to filter commands to replay when
261
+ * the connection is reestablished after a disconnect. Returning <code>false</code> means the command will not be
262
+ * filtered out and will be replayed. Defaults to replaying all queued commands.
263
+ *
264
+ * @param replayFilter a {@link Predicate} to filter commands to replay. Must not be {@code null}.
265
+ * @see #DEFAULT_REPLAY_FILTER
266
+ * @return {@code this}
267
+ * @since 6.6
268
+ */
269
+ public Builder replayFilter (Predicate <RedisCommand <?, ?, ?>> replayFilter ) {
270
+ this .replayFilter = replayFilter ;
271
+ return this ;
272
+ }
273
+
249
274
/**
250
275
* Allows cancelling queued commands in case a reconnect fails.Defaults to {@code false}. See
251
276
* {@link #DEFAULT_CANCEL_CMD_RECONNECT_FAIL}. <b>This flag is deprecated and should not be used as it can lead to race
@@ -527,13 +552,13 @@ public ClientOptions.Builder mutate() {
527
552
Builder builder = new Builder ();
528
553
529
554
builder .autoReconnect (isAutoReconnect ()).cancelCommandsOnReconnectFailure (isCancelCommandsOnReconnectFailure ())
530
- .decodeBufferPolicy ( getDecodeBufferPolicy ()).disconnectedBehavior ( getDisconnectedBehavior ())
531
- .reauthenticateBehavior ( getReauthenticateBehaviour ()).readOnlyCommands ( getReadOnlyCommands ())
532
- .publishOnScheduler ( isPublishOnScheduler ()).pingBeforeActivateConnection ( isPingBeforeActivateConnection ())
533
- .protocolVersion ( getConfiguredProtocolVersion ()).requestQueueSize ( getRequestQueueSize ())
534
- .scriptCharset ( getScriptCharset ()).jsonParser ( getJsonParser ()).socketOptions ( getSocketOptions ())
535
- .sslOptions ( getSslOptions ()).suspendReconnectOnProtocolFailure ( isSuspendReconnectOnProtocolFailure ())
536
- .timeoutOptions (getTimeoutOptions ());
555
+ .replayFilter ( getReplayFilter ()).decodeBufferPolicy ( getDecodeBufferPolicy ())
556
+ .disconnectedBehavior ( getDisconnectedBehavior ()).reauthenticateBehavior ( getReauthenticateBehaviour ())
557
+ .readOnlyCommands ( getReadOnlyCommands ()).publishOnScheduler ( isPublishOnScheduler ())
558
+ .pingBeforeActivateConnection ( isPingBeforeActivateConnection ()).protocolVersion ( getConfiguredProtocolVersion ())
559
+ .requestQueueSize ( getRequestQueueSize ()).scriptCharset ( getScriptCharset ()).jsonParser ( getJsonParser ())
560
+ .socketOptions ( getSocketOptions ()).sslOptions ( getSslOptions ())
561
+ .suspendReconnectOnProtocolFailure ( isSuspendReconnectOnProtocolFailure ()). timeoutOptions (getTimeoutOptions ());
537
562
538
563
return builder ;
539
564
}
@@ -551,6 +576,16 @@ public boolean isAutoReconnect() {
551
576
return autoReconnect ;
552
577
}
553
578
579
+ /**
580
+ * Controls which {@link RedisCommand} will be replayed after a re-connect. The {@link Predicate} returns <code>true</code>
581
+ * if command should be filtered out and not replayed. Defaults to {@link #DEFAULT_REPLAY_FILTER}.
582
+ *
583
+ * @return the currently set {@link Predicate} used to filter out commands to replay
584
+ */
585
+ public Predicate <RedisCommand <?, ?, ?>> getReplayFilter () {
586
+ return replayFilter ;
587
+ }
588
+
554
589
/**
555
590
* If this flag is {@code true} any queued commands will be canceled when a reconnect fails within the activation sequence.
556
591
* Default is {@code false}.
0 commit comments