ClusterClientBuilder in redis::cluster - Rust
Description: Used to configure and build a `ClusterClient`.

Skip to main content

## ClusterClientBuilder

# Struct ClusterClientBuilder

Source

    pub struct ClusterClientBuilder { /* private fields */ }

Available on crate feature `cluster` only.
Expand description

Used to configure and build a `ClusterClient`.

## Implementations§

Source§

### impl ClusterClientBuilder

Source

#### pub fn new<T: IntoConnectionInfo>( initial_nodes: impl IntoIterator<Item = T>, ) -> ClusterClientBuilder

Creates a new `ClusterClientBuilder` with the provided initial_nodes.

This is the same as `ClusterClient::builder(initial_nodes)`.

Source

#### pub fn build(self) -> RedisResult<ClusterClient>

Creates a new `ClusterClient` from the parameters.

This does not create connections to the Redis Cluster, but only performs some basic checks on the initial nodes’ URLs and passwords/usernames.

When the `tls-rustls` feature is enabled and TLS credentials are provided, they are set for each cluster connection.

##### §Errors

Upon failure to parse initial nodes or if the initial nodes have different passwords or usernames, an error is returned.

Source

#### pub fn password(self, password: impl AsRef<str>) -> ClusterClientBuilder

Sets password for the new ClusterClient.

Source

#### pub fn username(self, username: impl AsRef<str>) -> ClusterClientBuilder

Sets username for the new ClusterClient.

Source

#### pub fn retries(self, retries: u32) -> ClusterClientBuilder

Sets number of retries for the new ClusterClient.

Source

#### pub fn max_retry_wait(self, max_wait: u64) -> ClusterClientBuilder

Sets maximal wait time in millisceonds between retries for the new ClusterClient.

Source

#### pub fn min_retry_wait(self, min_wait: u64) -> ClusterClientBuilder

Sets minimal wait time in millisceonds between retries for the new ClusterClient.

Source

#### pub fn retry_wait_formula( self, factor: u64, exponent_base: u64, ) -> ClusterClientBuilder

Sets the factor and exponent base for the retry wait time. The formula for the wait is rand(min_wait_retry .. min(max_retry_wait , factor * exponent_base ^ retry))ms.

Source

#### pub fn tls(self, tls: TlsMode) -> ClusterClientBuilder

Available on crate features `tls-native-tls` or `tls-rustls` only.

Sets TLS mode for the new ClusterClient.

It is extracted from the first node of initial_nodes if not set.

Source

#### pub fn danger_accept_invalid_hostnames( self, insecure: bool, ) -> ClusterClientBuilder

Available on crate features `tls-rustls-insecure` or `tls-native-tls` only.

Configure hostname verification when connecting with TLS.

If `insecure` is true, this disables hostname verification, while leaving other aspects of certificate checking enabled. This mode is similar to what `redis-cli` does: TLS connections do check certificates, but hostname errors are ignored.

##### §Warning

You should think very carefully before you use this method. If hostname verification is not used, any valid certificate for any site will be trusted for use from any other. This introduces a significant vulnerability to man-in-the-middle attacks.

Source

#### pub fn certs(self, certificates: TlsCertificates) -> ClusterClientBuilder

Available on crate feature `tls-rustls` only.

Sets raw TLS certificates for the new ClusterClient.

When set, enforces the connection must be TLS secured.

All certificates must be provided as byte streams loaded from PEM files their consistency is checked during `build()` call.

- `certificates` - `TlsCertificates` structure containing:

  - `client_tls` - Optional `ClientTlsConfig` containing byte streams for

    - `client_cert` - client’s byte stream containing client certificate in PEM format
    - `client_key` - client’s byte stream containing private key in PEM format

  - `root_cert` - Optional byte stream yielding PEM formatted file for root certificates.

If `ClientTlsConfig` ( cert+key pair ) is not provided, then client-side authentication is not enabled. If `root_cert` is not provided, then system root certificates are used instead.

Source

#### pub fn read_from_replicas(self) -> ClusterClientBuilder

Enables reading from replicas for all new connections (default is disabled).

If enabled, then read queries will go to the replica nodes & write queries will go to the primary nodes. If there are no replica nodes, then all queries will go to the primary nodes.

Source

#### pub fn connection_timeout( self, connection_timeout: Duration, ) -> ClusterClientBuilder

Enables timing out on slow connection time.

If enabled, the cluster will only wait the given time on each connection attempt to each node.

Source

#### pub fn response_timeout( self, response_timeout: Duration, ) -> ClusterClientBuilder

Enables timing out on slow responses.

If enabled, the cluster will only wait the given time to each response from each node.

Source

#### pub fn use_protocol(self, protocol: ProtocolVersion) -> ClusterClientBuilder

Sets the protocol with which the client should communicate with the server.

Source

#### pub fn push_sender( self, push_sender: impl AsyncPushSender, ) -> ClusterClientBuilder

Available on crate feature `cluster-async` only.

Sets sender sender for push values.

The sender can be a channel, or an arbitrary function that handles crate::PushInfo values. This will fail client creation if the connection isn’t configured for RESP3 communications via the crate::RedisConnectionInfo::set_protocol function.

##### §Examples

    let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
    let config = ClusterClientBuilder::new(vec!["redis://127.0.0.1:6379/"])
        .use_protocol(redis::ProtocolVersion::RESP3)
        .push_sender(tx);

    let messages = Arc::new(Mutex::new(Vec::new()));
    let config = ClusterClientBuilder::new(vec!["redis://127.0.0.1:6379/"])
        .use_protocol(redis::ProtocolVersion::RESP3)
        .push_sender(move |msg|{
            let Ok(mut messages) = messages.lock() else {
                return Err(redis::aio::SendError);
            };
            messages.push(msg);
            Ok(())
        });

Source

#### pub fn tcp_settings(self, tcp_settings: TcpSettings) -> ClusterClientBuilder

Set the behavior of the underlying TCP connections.

Source

#### pub fn async_dns_resolver( self, resolver: impl AsyncDNSResolver, ) -> ClusterClientBuilder

Available on crate feature `cluster-async` only.

Set asynchronous DNS resolver for the underlying TCP connections.

The parameter resolver must implement the `crate::io::AsyncDNSResolver` trait.

Source

#### pub fn cache_config(self, cache_config: CacheConfig) -> Self

Available on crate features `cache-aio` and `cluster-async` only.

Sets cache config for `crate::cluster_async::ClusterConnection`, check CacheConfig for more details.

## Auto Trait Implementations§

§

### impl Freeze for ClusterClientBuilder

§

### impl !RefUnwindSafe for ClusterClientBuilder

§

### impl Send for ClusterClientBuilder

§

### impl Sync for ClusterClientBuilder

§

### impl Unpin for ClusterClientBuilder

§

### impl !UnwindSafe for ClusterClientBuilder

## Blanket Implementations§

Source§

### impl<T> Any for T
where T: 'static + ?Sized,

Source§

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more
Source§

### impl<T> Borrow<T> for T
where T: ?Sized,

Source§

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

### impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

### impl<T> From<T> for T

Source§

#### fn from(t: T) -> T

Returns the argument unchanged.

§

### impl<T> Instrument for T

§

#### fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [`Span`], returning an `Instrumented` wrapper. Read more
§

#### fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current `Span`, returning an `Instrumented` wrapper. Read more
Source§

### impl<T, U> Into<U> for T
where U: From<T>,

Source§

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `From<T> for U` chooses to do.

§

### impl<T> PolicyExt for T
where T: ?Sized,

§

#### fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new `Policy` that returns [`Action::Follow`] only if `self` and `other` return `Action::Follow`. Read more
§

#### fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new `Policy` that returns [`Action::Follow`] if either `self` or `other` returns `Action::Follow`. Read more
Source§

### impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

#### type Error = Infallible

The type returned in the event of a conversion error.
Source§

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

### impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

### impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

#### fn vzip(self) -> V

§

### impl<T> WithSubscriber for T

§

#### fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided `Subscriber` to this type, returning a [`WithDispatch`] wrapper. Read more
§

#### fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default `Subscriber` to this type, returning a [`WithDispatch`] wrapper. Read more
§

### impl<T> ErasedDestructor for T
where T: 'static,

§

### impl<T> MaybeSendSync for T