2
2
3
3
import io .github .resilience4j .circuitbreaker .CallNotPermittedException ;
4
4
import java .time .Duration ;
5
+ import java .util .ArrayList ;
5
6
import java .util .Arrays ;
6
7
import java .util .List ;
7
8
@@ -450,8 +451,10 @@ public static interface StrategySupplier {
450
451
* @see Builder#Builder(ClusterConfig[])
451
452
*/
452
453
public MultiClusterClientConfig (ClusterConfig [] clusterConfigs ) {
454
+
453
455
if (clusterConfigs == null || clusterConfigs .length < 1 ) throw new JedisValidationException (
454
456
"ClusterClientConfigs are required for MultiClusterPooledConnectionProvider" );
457
+
455
458
for (ClusterConfig clusterConfig : clusterConfigs ) {
456
459
if (clusterConfig == null )
457
460
throw new IllegalArgumentException ("ClusterClientConfigs must not contain null elements" );
@@ -634,6 +637,20 @@ public boolean isFastFailover() {
634
637
return fastFailover ;
635
638
}
636
639
640
+ /**
641
+ * Creates a new Builder instance for configuring MultiClusterClientConfig.
642
+ * <p>
643
+ * At least one cluster configuration must be added to the builder before calling build(). Use the
644
+ * endpoint() methods to add cluster configurations.
645
+ * </p>
646
+ * @return new Builder instance
647
+ * @throws JedisValidationException if clusterConfigs is null or empty
648
+ * @see Builder#Builder(ClusterConfig[])
649
+ */
650
+ public static Builder builder () {
651
+ return new Builder ();
652
+ }
653
+
637
654
/**
638
655
* Creates a new Builder instance for configuring MultiClusterClientConfig.
639
656
* @param clusterConfigs array of cluster configurations defining available Redis endpoints
@@ -751,6 +768,7 @@ public HostAndPort getHostAndPort() {
751
768
* @return new Builder instance
752
769
* @throws IllegalArgumentException if hostAndPort or clientConfig is null
753
770
*/
771
+ // TODO : Replace HostAndPort with Endpoint
754
772
public static Builder builder (HostAndPort hostAndPort , JedisClientConfig clientConfig ) {
755
773
return new Builder (hostAndPort , clientConfig );
756
774
}
@@ -974,7 +992,7 @@ public ClusterConfig build() {
974
992
public static class Builder {
975
993
976
994
/** Array of cluster configurations defining available Redis endpoints. */
977
- private ClusterConfig [] clusterConfigs ;
995
+ private final List < ClusterConfig > clusterConfigs = new ArrayList <>() ;
978
996
979
997
// ============ Retry Configuration Fields ============
980
998
/** Maximum number of retry attempts including the initial call. */
@@ -1033,17 +1051,20 @@ public static class Builder {
1033
1051
/** Delay in milliseconds between failover attempts. */
1034
1052
private int delayInBetweenFailoverAttempts = DELAY_IN_BETWEEN_FAILOVER_ATTEMPTS_DEFAULT ;
1035
1053
1054
+ /**
1055
+ * Constructs a new Builder with the specified cluster configurations.
1056
+ */
1057
+ public Builder () {
1058
+ }
1059
+
1036
1060
/**
1037
1061
* Constructs a new Builder with the specified cluster configurations.
1038
1062
* @param clusterConfigs array of cluster configurations defining available Redis endpoints
1039
1063
* @throws JedisValidationException if clusterConfigs is null or empty
1040
1064
*/
1041
1065
public Builder (ClusterConfig [] clusterConfigs ) {
1042
1066
1043
- if (clusterConfigs == null || clusterConfigs .length < 1 ) throw new JedisValidationException (
1044
- "ClusterClientConfigs are required for MultiClusterPooledConnectionProvider" );
1045
-
1046
- this .clusterConfigs = clusterConfigs ;
1067
+ this (Arrays .asList (clusterConfigs ));
1047
1068
}
1048
1069
1049
1070
/**
@@ -1052,7 +1073,47 @@ public Builder(ClusterConfig[] clusterConfigs) {
1052
1073
* @throws JedisValidationException if clusterConfigs is null or empty
1053
1074
*/
1054
1075
public Builder (List <ClusterConfig > clusterConfigs ) {
1055
- this (clusterConfigs .toArray (new ClusterConfig [0 ]));
1076
+ this .clusterConfigs .addAll (clusterConfigs );
1077
+ }
1078
+
1079
+ /**
1080
+ * Adds a pre-configured endpoint configuration.
1081
+ * <p>
1082
+ * This method allows adding a fully configured ClusterConfig instance, providing maximum
1083
+ * flexibility for advanced configurations including custom health check strategies, connection
1084
+ * pool settings, etc.
1085
+ * </p>
1086
+ * @param clusterConfig the pre-configured cluster configuration
1087
+ * @return this builder
1088
+ */
1089
+ public Builder endpoint (ClusterConfig clusterConfig ) {
1090
+ this .clusterConfigs .add (clusterConfig );
1091
+ return this ;
1092
+ }
1093
+
1094
+ /**
1095
+ * Adds a Redis endpoint with custom client configuration.
1096
+ * <p>
1097
+ * This method allows specifying endpoint-specific configuration such as authentication, SSL
1098
+ * settings, timeouts, etc. This configuration will override the default client configuration
1099
+ * for this specific endpoint.
1100
+ * </p>
1101
+ * @param endpoint the Redis server endpoint
1102
+ * @param weight the weight for this endpoint (higher values = higher priority)
1103
+ * @param clientConfig the client configuration for this endpoint
1104
+ * @return this builder
1105
+ */
1106
+ public Builder endpoint (Endpoint endpoint , float weight , JedisClientConfig clientConfig ) {
1107
+ // Convert Endpoint to HostAndPort for ClusterConfig
1108
+ // TODO : Refactor ClusterConfig to accept Endpoint directly
1109
+ HostAndPort hostAndPort = (endpoint instanceof HostAndPort ) ? (HostAndPort ) endpoint
1110
+ : new HostAndPort (endpoint .getHost (), endpoint .getPort ());
1111
+
1112
+ ClusterConfig clusterConfig = ClusterConfig .builder (hostAndPort , clientConfig ).weight (weight )
1113
+ .build ();
1114
+
1115
+ this .clusterConfigs .add (clusterConfig );
1116
+ return this ;
1056
1117
}
1057
1118
1058
1119
// ============ Retry Configuration Methods ============
@@ -1453,7 +1514,9 @@ public Builder delayInBetweenFailoverAttempts(int delayInBetweenFailoverAttempts
1453
1514
* @return a new MultiClusterClientConfig instance with the configured settings
1454
1515
*/
1455
1516
public MultiClusterClientConfig build () {
1456
- MultiClusterClientConfig config = new MultiClusterClientConfig (this .clusterConfigs );
1517
+
1518
+ MultiClusterClientConfig config = new MultiClusterClientConfig (
1519
+ this .clusterConfigs .toArray (new ClusterConfig [0 ]));
1457
1520
1458
1521
// Copy retry configuration
1459
1522
config .retryMaxAttempts = this .retryMaxAttempts ;
0 commit comments