Skip to main content

ClientBuilder

Struct ClientBuilder 

Source
pub struct ClientBuilder { /* private fields */ }
Expand description

Builder for constructing a Client with custom connection options.

Implementations§

Source§

impl ClientBuilder

Source

pub fn new() -> Self

Create a new builder with default options.

Source

pub fn host(self, host: &str, port: u16) -> Self

Add a server address (host and port).

Source

pub fn url(self, url: &str) -> Result<Self>

Add a server by URL, extracting host, port, TLS, and auth settings.

Source

pub fn cluster(self) -> Self

Enable cluster mode.

Source

pub fn tls(self) -> Self

Enable TLS with certificate verification.

Source

pub fn tls_insecure(self) -> Self

Enable TLS without certificate verification.

Source

pub fn password(self, pw: impl Into<String>) -> Self

Set the authentication password.

Source

pub fn username(self, username: impl Into<String>) -> Self

Set the authentication username (ACL).

Source

pub fn database(self, db: i64) -> Self

Select the database number.

Source

pub fn read_from(self, strategy: ReadFrom) -> Self

Set the read-from strategy (primary, replica, etc.).

Source

pub fn connect_timeout(self, timeout: Duration) -> Self

Set the connection timeout.

Source

pub fn request_timeout(self, timeout: Duration) -> Self

Set the per-request timeout.

Source

pub fn blocking_cmd_timeout_extension(self, extension: Duration) -> Self

Extra time added to a blocking command’s server-side timeout when computing the client-side request deadline.

Blocking commands (BLPOP, BLMPOP, XREAD BLOCK, etc.) pass a server-side timeout as part of the command payload. To prevent the client from failing a request that the server is legitimately about to answer, the client waits server_timeout + extension before treating the command as timed out. The extension is a safety margin over slow links or loaded servers.

Defaults to 500ms. Under concurrent blocking pressure (many clients waking at the same server timeout), 500ms can be insufficient — consider 1s or 2s if you observe late responders losing their replies.

Source

pub fn max_inflight(self, max_inflight: u32) -> Self

Set the maximum number of in-flight requests.

Source

pub fn retry_strategy(self, strategy: ConnectionRetryStrategy) -> Self

Set the connection retry strategy.

Source

pub fn client_name(self, name: impl Into<String>) -> Self

Set the client name sent to the server.

Source

pub fn protocol(self, proto: ProtocolVersion) -> Self

Set the RESP protocol version.

Source

pub fn lazy_connect(self) -> Self

👎Deprecated since 0.1.1:

Use ClientBuilder::build_lazy() -> LazyClient instead. lazy_connect() sets a flag that Client::new now rejects; the connect-on-first-use path lives on LazyClient.

Enable lazy connection (connect on first command, not on build).

Deprecated. The lazy_connect flag on ConnectionRequest no longer alters Client::new / build() behaviour — those entry points always return a connected Client or an error. The connect-on-first-use path moved to a dedicated type: construct a LazyClient via ClientBuilder::build_lazy or LazyClient::from_config.

Callers still chaining .lazy_connect().build() will see build() return InvalidClientConfig at runtime — the deprecation warning here surfaces the migration at compile time.

Source

pub fn tcp_nodelay(self) -> Self

Enable TCP_NODELAY.

Source

pub fn pubsub_subscriptions(self, subs: PubSubSubscriptionInfo) -> Self

Set PubSub subscriptions for the connection.

Source

pub fn push_sender(self, tx: UnboundedSender<PushInfo>) -> Self

Receive RESP3 push frames (pub/sub messages, invalidation notices, keyspace notifications) on a caller-provided channel.

The sender half is wired into every connection this client owns; push frames that the connection receives are forwarded to it verbatim as crate::PushInfo values.

Push delivery requires RESP3 — build the client with ClientBuilder::protocol set to crate::value::ProtocolVersion::RESP3. Subscribing to channels is performed separately via:

  • build-time: ClientBuilder::pubsub_subscriptions (replayed on reconnect);
  • runtime: client.cmd("SUBSCRIBE").arg("chan").execute::<()>().await? (intercepted by the pubsub synchronizer and reconciled in the background; the call returns as soon as the desired state is recorded).
§Example
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let client = ClientBuilder::new()
    .host("127.0.0.1", 6379)
    .protocol(ProtocolVersion::RESP3)
    .push_sender(tx)
    .build()
    .await?;

// Request a subscription. The reconciler issues SUBSCRIBE in the
// background; push frames arrive on `rx` once the server confirms.
let _: () = client.cmd("SUBSCRIBE").arg("events").execute().await?;
while let Some(push) = rx.recv().await {
    // handle push frame
    let _ = push;
}
Source

pub async fn build(self) -> Result<Client>

Build and connect the client.

Source

pub fn build_lazy(self) -> Result<LazyClient>

Build a client that defers TCP connection until the first command is executed.

Unlike build, this does NOT attempt to reach the server — it validates the address list and returns a LazyClient synchronously. The underlying connection is established on the first LazyClient::connect call (or implicitly on the first command issued through the returned &Client). Subsequent calls reuse the already- established connection; a OnceCell guarantees connect runs exactly once.

Use this when you want a client handle you can construct synchronously and thread through app startup, but where the Valkey endpoint may not be reachable yet (e.g. the server is part of the same deployment and comes up later).

Trait Implementations§

Source§

impl Default for ClientBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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