Skip to main content

Client

Struct Client 

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

A Kafka client backed by a BrokerPool.

Construct a Client with Client::builder.

A clone of a Client is cheap. The clone shares the underlying BrokerPool through an Arc and copies the connection options by value.

Implementations§

Source§

impl Client

Source

pub async fn start( bootstrap: String, client_id: String, dns_timeout: Time, socket_connection_setup_timeout: Time, socket_connection_setup_timeout_max: Time, reconnect_backoff: Time, reconnect_backoff_max: Time, connections_max_idle: Time, client_dns_lookup: ClientDnsLookup, send_buffer: Option<ByteSize>, receive_buffer: Option<ByteSize>, request_timeout: Time, dispatch_queue_capacity: usize, frame_max: ByteSize, metadata_recovery_strategy: MetadataRecoveryStrategy, metadata_recovery_rebootstrap_trigger: Time, security: Option<ClientSecurity>, metadata_scope: MetadataScope, telemetry: Option<ClientTelemetryConfig>, ) -> Result<Self, ClientError>

Build a Client pointed at the given bootstrap address.

§Errors

Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.

Source

pub fn builder() -> ClientStartBuilder

Build a Client pointed at the given bootstrap address.

§Errors

Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.

Source§

impl Client

Source

pub fn connection_options(&self) -> &ConnectionOptions

The options of every connection that this client opens, such as the socket buffers (send.buffer.bytes, receive.buffer.bytes) that each broker socket gets before it connects.

Source

pub fn metrics(&self) -> Option<&ClientMetrics>

The metric registry of a client that pushes its metrics (KIP-714). A client crate registers its own metrics in it.

Source

pub async fn close_telemetry(&self)

Send the terminating telemetry push (KIP-714), if the client pushes metrics and has a subscription, and wait for it. Kafka’s clients send it from close. Without this call, the push goes out in the background once the last clone of the client drops.

Source

pub async fn client_instance_id( &self, timeout: Time, ) -> Result<Option<Uuid>, ClientError>

The client instance id that the broker assigned for metrics push (KIP-714), waiting up to timeout for the first subscription, as Kafka’s clientInstanceId does. Ok(None) when no subscription has loaded by then, as Kafka returns null.

§Errors

Returns ClientError::InvalidArgument for a negative timeout, and ClientError::TelemetryDisabled for a client that does not push metrics.

Source

pub async fn send<R: ProtocolRequest>( &self, req: R, ) -> Result<R::Response, ClientError>

Send a request that has no fixed target broker.

Before the first metadata response, the request goes to a bootstrap address. After it, the request goes to the least loaded known broker, as Kafka’s NetworkClient.leastLoadedNode picks the node.

§Errors

Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.

Source

pub async fn send_at_least<R: ProtocolRequest>( &self, req: R, min_version: i16, ) -> Result<R::Response, ClientError>

Send a request, as send routes it, only when the broker and client share a version at or above min_version.

§Errors

Returns ClientError::IncompatibleVersion before dispatch when the shared range is below min_version. Other errors match send.

Source

pub async fn send_to_connection<R: ProtocolRequest>( &self, broker_id: Option<i32>, req: R, ) -> Result<R::Response, ClientError>

Send on one fixed pool connection, without selecting another broker.

Some(id) selects that broker’s connection. None selects the distinct bootstrap connection even after cluster metadata has been learned. Callers that account for per-connection request limits can keep this selection fixed until the request completes, then refresh and retry on failure themselves.

§Errors

Returns a routing, connection setup, encoding or response error.

Source

pub async fn send_to_connection_no_response<R: ProtocolRequest>( &self, broker_id: Option<i32>, req: R, ) -> Result<(), ClientError>

Send without a response on the fixed connection selected by send_to_connection.

§Errors

Returns a routing, connection setup, encoding or writer enqueue error.

Source

pub async fn send_no_response<R: ProtocolRequest>( &self, req: R, ) -> Result<(), ClientError>

Send a request, as send routes it, without registering a pending response. Intended for protocol operations such as Produce acks=0.

§Errors

Returns an error if the connection cannot be opened or cannot accept the encoded request.

Source

pub async fn reconnect_bootstrap(&self)

Drop the cached bootstrap connection and refresh the bootstrap address list from the original bootstrap string.

Callers that know their bootstrap request is safe to retry can call this after a transport error and before they send again.

Source

pub fn finalized_features(&self) -> FinalizedFeatures

The finalized feature levels (KIP-584) with the highest epoch that a connection of this client received in ApiVersions. Kafka’s producer reads transaction.version from them. The epoch is UNKNOWN_FINALIZED_FEATURES_EPOCH before a connection to a broker that sends features.

Source

pub fn knows_broker(&self, broker_id: i32) -> bool

Whether the pool knows a dialable address for broker_id.

The pool knows one when refresh_metadata learned it and the port was not 0. A caller can then choose between broker routing and the bootstrap send without a speculative connect.

Source

pub fn broker(&self, broker_id: i32) -> BrokerHandle<'_>

Return a BrokerHandle that routes requests to a specific broker by id.

refresh_metadata must have registered the broker first.

Source

pub fn evict_broker(&self, broker_id: i32)

Drop the pooled connection to broker_id so the next request to it reconnects to its current advertised address.

Call this after a send fails, so a bounced or failed-over broker is not retried over a dead socket.

Source

pub fn metadata_topics(&self) -> &MetadataTopics

The topics that refresh_metadata names. The clones of this client share them.

Source

pub async fn refresh_metadata(&self) -> Result<MetadataResponse, ClientError>

Send the MetadataRequest of metadata_topics, parse the broker list from the response, refresh the pool’s address registry, and return the typed response.

With MetadataScope::AllTopics the request asks for all topics. With MetadataScope::Topics it names the current topics, as Kafka’s producer and consumer do. Use refresh_metadata_with for another request.

§Errors

Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.

Source

pub async fn refresh_metadata_with( &self, request: MetadataRequest, ) -> Result<MetadataResponse, ClientError>

Send request, parse the broker list from the response, refresh the pool’s address registry, and return the typed response.

Kafka’s producer names its topics in the request (ProducerMetadata.newMetadataRequestBuilder). A request that names a topic lets the broker create the topic when allow_auto_topic_creation is set, and returns a per-topic error such as TOPIC_AUTHORIZATION_FAILED for it. Every retry and rebootstrap in this call sends the same request.

§Errors

Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.

Source

pub async fn offset_for_leader_epoch( &self, topic: &str, partition: i32, current_leader_epoch: i32, leader_epoch: i32, ) -> Result<EpochEndOffset, ClientError>

Send a single-partition OffsetForLeaderEpoch (api_key=23) over the bootstrap connection.

This is a thin wrapper over the free offset_for_leader_epoch helper that the consumer’s KIP-320 position-validation pass uses. Client does not otherwise expose its connection, so this method borrows the same bootstrap connection that send uses.

§Errors

Returns an error on transport or version-negotiation failure, or when the response does not contain the partition.

Source

pub async fn offset_for_leader_epoch_on( &self, broker_id: i32, topic: &str, partition: i32, current_leader_epoch: i32, leader_epoch: i32, ) -> Result<EpochEndOffset, ClientError>

Send a single-partition OffsetForLeaderEpoch (api_key=23) to a specific broker by id, through BrokerPool::get.

This method mirrors offset_for_leader_epoch but targets the partition leader instead of the bootstrap connection. KIP-320 requires the validation RPC to reach the partition leader, which is the only replica with the authoritative epoch→end-offset history.

The broker must already be in the pool’s registry, which refresh_metadata populates.

§Errors

Returns Disconnected if broker_id is not in the registry. Returns an error on transport or version-negotiation failure. Returns an error when the response does not contain the partition.

Source

pub async fn fetch_partition_with_isolation_on( &self, broker_id: i32, fetch: IsolatedFetch<'_>, ) -> Result<Vec<FetchedRecord>, ClientError>

Fetch one partition from a specific broker with the requested isolation level. The broker address must have been learned through refresh_metadata.

§Errors

Returns a connection, protocol, or broker error. A failed connection to a known broker is evicted, metadata is refreshed, and the request is retried once against the broker’s current advertised address.

Source

pub fn close(self)

Close the client and all pooled connections.

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl TelemetryConnections for Client

Source§

fn open_connection( &self, preferred: Option<i32>, ) -> Option<(i32, Arc<Connection>)>

An open connection with its broker id, preferring the broker of preferred while it stays open. Per KIP-714 a client keeps pushing to the same broker. None when no connection is open.

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Paint for T
where T: ?Sized,

§

fn fg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the foreground set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like red() and green(), which have the same functionality but are pithier.

§Example

Set foreground color to white using fg():

use yansi::{Paint, Color};

painted.fg(Color::White);

Set foreground color to white using white().

use yansi::Paint;

painted.white();
§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Primary].

§Example
println!("{}", value.primary());
§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Fixed].

§Example
println!("{}", value.fixed(color));
§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Rgb].

§Example
println!("{}", value.rgb(r, g, b));
§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Black].

§Example
println!("{}", value.black());
§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Red].

§Example
println!("{}", value.red());
§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Green].

§Example
println!("{}", value.green());
§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Yellow].

§Example
println!("{}", value.yellow());
§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Blue].

§Example
println!("{}", value.blue());
§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Magenta].

§Example
println!("{}", value.magenta());
§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Cyan].

§Example
println!("{}", value.cyan());
§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: White].

§Example
println!("{}", value.white());
§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlack].

§Example
println!("{}", value.bright_black());
§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightRed].

§Example
println!("{}", value.bright_red());
§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightGreen].

§Example
println!("{}", value.bright_green());
§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightYellow].

§Example
println!("{}", value.bright_yellow());
§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlue].

§Example
println!("{}", value.bright_blue());
§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.bright_magenta());
§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightCyan].

§Example
println!("{}", value.bright_cyan());
§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightWhite].

§Example
println!("{}", value.bright_white());
§

fn bg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the background set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like on_red() and on_green(), which have the same functionality but are pithier.

§Example

Set background color to red using fg():

use yansi::{Paint, Color};

painted.bg(Color::Red);

Set background color to red using on_red().

use yansi::Paint;

painted.on_red();
§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Primary].

§Example
println!("{}", value.on_primary());
§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Fixed].

§Example
println!("{}", value.on_fixed(color));
§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Rgb].

§Example
println!("{}", value.on_rgb(r, g, b));
§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Black].

§Example
println!("{}", value.on_black());
§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Red].

§Example
println!("{}", value.on_red());
§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Green].

§Example
println!("{}", value.on_green());
§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Yellow].

§Example
println!("{}", value.on_yellow());
§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Blue].

§Example
println!("{}", value.on_blue());
§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Magenta].

§Example
println!("{}", value.on_magenta());
§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Cyan].

§Example
println!("{}", value.on_cyan());
§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: White].

§Example
println!("{}", value.on_white());
§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlack].

§Example
println!("{}", value.on_bright_black());
§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightRed].

§Example
println!("{}", value.on_bright_red());
§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightGreen].

§Example
println!("{}", value.on_bright_green());
§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightYellow].

§Example
println!("{}", value.on_bright_yellow());
§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlue].

§Example
println!("{}", value.on_bright_blue());
§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.on_bright_magenta());
§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightCyan].

§Example
println!("{}", value.on_bright_cyan());
§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightWhite].

§Example
println!("{}", value.on_bright_white());
§

fn attr(&self, value: Attribute) -> Painted<&T>

Enables the styling [Attribute] value.

This method should be used rarely. Instead, prefer to use attribute-specific builder methods like bold() and underline(), which have the same functionality but are pithier.

§Example

Make text bold using attr():

use yansi::{Paint, Attribute};

painted.attr(Attribute::Bold);

Make text bold using using bold().

use yansi::Paint;

painted.bold();
§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Bold].

§Example
println!("{}", value.bold());
§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Dim].

§Example
println!("{}", value.dim());
§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Italic].

§Example
println!("{}", value.italic());
§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Underline].

§Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute :: Blink].

§Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute :: RapidBlink].

§Example
println!("{}", value.rapid_blink());
§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Invert].

§Example
println!("{}", value.invert());
§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Conceal].

§Example
println!("{}", value.conceal());
§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Strike].

§Example
println!("{}", value.strike());
§

fn quirk(&self, value: Quirk) -> Painted<&T>

Enables the yansi [Quirk] value.

This method should be used rarely. Instead, prefer to use quirk-specific builder methods like mask() and wrap(), which have the same functionality but are pithier.

§Example

Enable wrapping using .quirk():

use yansi::{Paint, Quirk};

painted.quirk(Quirk::Wrap);

Enable wrapping using wrap().

use yansi::Paint;

painted.wrap();
§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Mask].

§Example
println!("{}", value.mask());
§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Wrap].

§Example
println!("{}", value.wrap());
§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Linger].

§Example
println!("{}", value.linger());
§

fn clear(&self) -> Painted<&T>

👎Deprecated since 1.0.1:

renamed to resetting() due to conflicts with Vec::clear(). The clear() method will be removed in a future release.

Returns self with the quirk() set to [Quirk :: Clear].

§Example
println!("{}", value.clear());
§

fn resetting(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Resetting].

§Example
println!("{}", value.resetting());
§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Bright].

§Example
println!("{}", value.bright());
§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: OnBright].

§Example
println!("{}", value.on_bright());
§

fn whenever(&self, value: Condition) -> Painted<&T>

Conditionally enable styling based on whether the [Condition] value applies. Replaces any previous condition.

See the crate level docs for more details.

§Example

Enable styling painted only when both stdout and stderr are TTYs:

use yansi::{Paint, Condition};

painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
§

fn new(self) -> Painted<Self>
where Self: Sized,

Create a new [Painted] with a default [Style]. Read more
§

fn paint<S>(&self, style: S) -> Painted<&Self>
where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<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