4444 * .addEndpoint(new ClientRoutesEndpoint(
4545 * UUID.fromString("12345678-1234-1234-1234-123456789012"),
4646 * "my-privatelink.us-east-1.aws.scylladb.com:9042"))
47+ * .withDnsCacheDuration(1000L) // Cache DNS for 1 second (default: 500ms)
4748 * .build();
4849 *
4950 * CqlSession session = CqlSession.builder()
5758@ Immutable
5859public class ClientRoutesConfig {
5960
61+ private static final long DEFAULT_DNS_CACHE_DURATION_MILLIS = 500L ;
62+
6063 private final List <ClientRoutesEndpoint > endpoints ;
6164 private final String tableName ;
65+ private final long dnsCacheDurationMillis ;
6266
63- private ClientRoutesConfig (List <ClientRoutesEndpoint > endpoints , String tableName ) {
67+ private ClientRoutesConfig (
68+ List <ClientRoutesEndpoint > endpoints , String tableName , long dnsCacheDurationMillis ) {
6469 if (endpoints == null || endpoints .isEmpty ()) {
6570 throw new IllegalArgumentException ("At least one endpoint must be specified" );
6671 }
72+ if (dnsCacheDurationMillis < 0 ) {
73+ throw new IllegalArgumentException ("DNS cache duration must be non-negative" );
74+ }
6775 this .endpoints = Collections .unmodifiableList (new ArrayList <>(endpoints ));
6876 this .tableName = tableName ;
77+ this .dnsCacheDurationMillis = dnsCacheDurationMillis ;
6978 }
7079
7180 /**
@@ -88,6 +97,19 @@ public String getTableName() {
8897 return tableName ;
8998 }
9099
100+ /**
101+ * Returns the DNS cache duration in milliseconds.
102+ *
103+ * <p>This controls how long resolved DNS entries are cached before being re-resolved. A shorter
104+ * duration is appropriate for dynamic environments where DNS mappings change frequently, while a
105+ * longer duration can reduce DNS lookup overhead in stable environments.
106+ *
107+ * @return the DNS cache duration in milliseconds (default: 500ms).
108+ */
109+ public long getDnsCacheDurationMillis () {
110+ return dnsCacheDurationMillis ;
111+ }
112+
91113 /**
92114 * Creates a new builder for constructing a {@link ClientRoutesConfig}.
93115 *
@@ -107,12 +129,14 @@ public boolean equals(Object o) {
107129 return false ;
108130 }
109131 ClientRoutesConfig that = (ClientRoutesConfig ) o ;
110- return endpoints .equals (that .endpoints ) && Objects .equals (tableName , that .tableName );
132+ return dnsCacheDurationMillis == that .dnsCacheDurationMillis
133+ && endpoints .equals (that .endpoints )
134+ && Objects .equals (tableName , that .tableName );
111135 }
112136
113137 @ Override
114138 public int hashCode () {
115- return Objects .hash (endpoints , tableName );
139+ return Objects .hash (endpoints , tableName , dnsCacheDurationMillis );
116140 }
117141
118142 @ Override
@@ -123,13 +147,16 @@ public String toString() {
123147 + ", tableName='"
124148 + tableName
125149 + '\''
150+ + ", dnsCacheDurationMillis="
151+ + dnsCacheDurationMillis
126152 + '}' ;
127153 }
128154
129155 /** Builder for {@link ClientRoutesConfig}. */
130156 public static class Builder {
131157 private final List <ClientRoutesEndpoint > endpoints = new ArrayList <>();
132158 private String tableName ;
159+ private long dnsCacheDurationMillis = DEFAULT_DNS_CACHE_DURATION_MILLIS ;
133160
134161 /**
135162 * Adds an endpoint to the configuration.
@@ -177,6 +204,29 @@ public Builder withTableName(@Nullable String tableName) {
177204 return this ;
178205 }
179206
207+ /**
208+ * Sets the DNS cache duration in milliseconds.
209+ *
210+ * <p>This controls how long resolved DNS entries are cached before being re-resolved. A shorter
211+ * duration is appropriate for dynamic environments where DNS mappings change frequently (e.g.,
212+ * during rolling updates), while a longer duration can reduce DNS lookup overhead in stable
213+ * environments.
214+ *
215+ * <p>Default: 500ms
216+ *
217+ * @param durationMillis the cache duration in milliseconds (must be non-negative).
218+ * @return this builder.
219+ * @throws IllegalArgumentException if the duration is negative.
220+ */
221+ @ NonNull
222+ public Builder withDnsCacheDuration (long durationMillis ) {
223+ if (durationMillis < 0 ) {
224+ throw new IllegalArgumentException ("DNS cache duration must be non-negative" );
225+ }
226+ this .dnsCacheDurationMillis = durationMillis ;
227+ return this ;
228+ }
229+
180230 /**
181231 * Builds the {@link ClientRoutesConfig} with the configured endpoints and table name.
182232 *
@@ -185,7 +235,7 @@ public Builder withTableName(@Nullable String tableName) {
185235 */
186236 @ NonNull
187237 public ClientRoutesConfig build () {
188- return new ClientRoutesConfig (endpoints , tableName );
238+ return new ClientRoutesConfig (endpoints , tableName , dnsCacheDurationMillis );
189239 }
190240 }
191241}
0 commit comments