
com.aerospike.vector.client.ConnectionConfig Maven / Gradle / Ivy
package com.aerospike.vector.client;
import com.aerospike.vector.client.auth.Credentials;
import java.util.List;
/**
* Represents the configuration for connecting to an AVS (Aerospike Vector Similarity) server. This
* configuration class encompasses details like connection endpoints, TLS configuration, and
* authentication credentials.
*
* A null {@link ClientTlsConfig} signifies that the connection will not use TLS.
*/
public class ConnectionConfig {
private final List seeds;
private final String listenerName;
private final boolean isLoadBalancer;
private final ClientTlsConfig clientTlsConfig;
private final Credentials credentials;
private int connectTimeout = 5_000;
private final int defaultTimeout;
private final boolean failIfNotConnected;
/**
* Constructs a new connection configuration with specified parameters.
*
* @param seeds A list of {@link HostPort} objects specifying the seed nodes for the cluster
* connection
* @param listenerName The name of the listener, if specific configuration is needed
* @param isLoadBalancer Indicates whether load balancing is enabled
* @param clientTlsConfig The TLS configuration for secure connections, null if not using TLS
* @param credentials The credentials used for authentication
* @param connectTimeout The timeout in milliseconds for establishing a connection
* @param defaultTimeout The default timeout in milliseconds for operations
* @param failIfNotConnected Flag indicating whether to fail if no connections could be
* established
*/
private ConnectionConfig(
List seeds,
String listenerName,
boolean isLoadBalancer,
ClientTlsConfig clientTlsConfig,
Credentials credentials,
int connectTimeout,
int defaultTimeout,
boolean failIfNotConnected) {
this.seeds = seeds;
this.listenerName = listenerName;
this.isLoadBalancer = isLoadBalancer;
this.clientTlsConfig = clientTlsConfig;
this.credentials = credentials;
this.connectTimeout = connectTimeout;
this.defaultTimeout = defaultTimeout;
this.failIfNotConnected = failIfNotConnected;
}
/**
* Returns the configured seed nodes.
*
* @return A list of {@link HostPort} objects representing the seed nodes.
*/
public List getSeeds() {
return seeds;
}
/**
* Returns the listener name for the connection.
*
* @return The listener name, or null if not specified.
*/
public String getListenerName() {
return listenerName;
}
/**
* Checks if load balancing is enabled for the connection.
*
* @return true if load balancing is enabled, false otherwise.
*/
public boolean isLoadBalancer() {
return isLoadBalancer;
}
/**
* Returns the TLS configuration for the connection.
*
* @return The {@link ClientTlsConfig} object representing the TLS configuration, or null if TLS
* is not used.
*/
public ClientTlsConfig getClientTlsConfig() {
return clientTlsConfig;
}
/**
* Returns the credentials used for authentication.
*
* @return The {@link Credentials} object.
*/
public Credentials getCredentials() {
return credentials;
}
/**
* Returns the connection timeout.
*
* @return The timeout in milliseconds for establishing connections.
*/
public int getConnectTimeout() {
return connectTimeout;
}
/**
* Returns the default timeout for operations.
*
* @return The default timeout in milliseconds.
*/
public int getDefaultTimeout() {
return defaultTimeout;
}
/**
* Checks if the connection should fail if not connected.
*
* @return true if the system should fail upon not connecting, false otherwise.
*/
public boolean isFailIfNotConnected() {
return failIfNotConnected;
}
/** Builder class for {@link ConnectionConfig} */
public static class Builder {
private List seeds;
private String listenerName;
private boolean isLoadBalancer;
private ClientTlsConfig clientTlsConfig;
private Credentials credentials;
private int connectTimeout = 5_000;
private int defaultTimeout = Integer.MAX_VALUE;
private boolean failIfNotConnected;
/** Empty builder */
public Builder() {}
/**
* Add credential into the builder
*
* @param credentials
* @return Builder with the credential
*/
public Builder withCredentials(Credentials credentials) {
this.credentials = credentials;
return this;
}
/**
* Add the listener into the builder
*
* @param listenerName
* @return Builder with listener name
*/
public Builder withListenerName(String listenerName) {
this.listenerName = listenerName;
return this;
}
/**
* Add the tls config into the builder
*
* @param clientTlsConfig tls related information, if passed null non-tls connection will be
* used
* @return Builder with listener name
*/
public Builder withTls(ClientTlsConfig clientTlsConfig) {
this.clientTlsConfig = clientTlsConfig;
return this;
}
/**
* Indicated if provided seedhosts are load balancer.
*
* @param isLoadbalancer By default, this is disabled
* @return Builder with this flag on or off
*/
public Builder withLoadBalancer(boolean isLoadbalancer) {
this.isLoadBalancer = isLoadbalancer;
return this;
}
/**
* @param connectTimeout connection timeout in milliseconds
* @return Builder with connectTimeout
*/
public Builder withConnectTimeout(int connectTimeout) {
if (connectTimeout < 0) {
throw new IllegalArgumentException("Connect timeout must be greater than 0");
}
this.connectTimeout = connectTimeout;
return this;
}
/**
* The default timeout in milliseconds for operations
*
* @param defaultTimeout default timeout in milliseconds for operations
* @return Builder with defaultTimeout
*/
public Builder withDefaultTimeout(int defaultTimeout) {
if (defaultTimeout < 0) {
throw new IllegalArgumentException("Default timeout must be greater than 0");
}
this.defaultTimeout = defaultTimeout;
return this;
}
/**
* @param failIfNotConnected Throw error if connection was not established
* @return Builder with failIfNotConnected
*/
public Builder withFailIfNotConnected(boolean failIfNotConnected) {
this.failIfNotConnected = failIfNotConnected;
return this;
}
/**
* Hosts to use as seed for node discovery
*
* @param hosts List of HostPorts
* @return Builder with seed Hosts
*/
public Builder withHosts(List hosts) {
this.seeds = hosts;
return this;
}
/**
* Build connection config object with supplied information
*
* @return ConnectionConfig constructed connection config object
*/
public ConnectionConfig build() {
if (seeds == null || seeds.isEmpty()) {
throw new IllegalArgumentException("Seed list cannot be empty");
}
return new ConnectionConfig(
seeds,
listenerName,
isLoadBalancer,
clientTlsConfig,
credentials,
connectTimeout,
defaultTimeout,
failIfNotConnected);
}
}
}