Skip to main content

AsyncKafkaClient

Struct AsyncKafkaClient 

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

An async Kafka client for bootstrap and connection management.

This lightweight client manages a pool of [AsyncConnection]s and is intended to be used by other async wrappers (producer/consumer) to obtain connections to brokers without imposing Sync/'static constraints on the higher-level code. It will attempt to connect to the provided bootstrap hosts on creation (unless the host list is empty), but will not continuously maintain metadata — callers can use [ensure_connected] to trigger a reconnection to bootstrap hosts when necessary.

Implementations§

Source§

impl AsyncKafkaClient

Source

pub async fn new(hosts: Vec<String>) -> Result<Self>

Creates a new async client and connects to the bootstrap brokers.

Source

pub async fn with_client_id( hosts: Vec<String>, client_id: String, ) -> Result<Self>

Creates a new async client with a specific client ID.

Attempts to connect to the provided hosts in order until a connection succeeds. If no hosts are reachable and the hosts list is non-empty, an error Error::Connection(ConnectionError::NoHostReachable) is returned.

Source

pub async fn with_client_id_and_security( hosts: Vec<String>, client_id: String, security: Option<SecurityConfig>, ) -> Result<Self>

Creates a new async client with optional TLS security.

Source

pub fn client_id(&self) -> &str

Returns the client ID.

Source

pub fn bootstrap_hosts(&self) -> &[String]

Returns the bootstrap hosts.

Source

pub fn security(&self) -> Option<&SecurityConfig>

Returns the configured optional security settings.

Source

pub async fn get_connection( &mut self, host: &str, ) -> Result<&mut AsyncConnection>

Gets (or creates) a mutable reference to a connection for host.

If a connection for host does not yet exist, the underlying [AsyncConnection::connect] is attempted and the connection is stored in the internal pool. The returned reference is tied to the mutable borrow of self and therefore short-lived.

Source

pub fn connected_hosts(&self) -> Vec<&str>

Gets the list of currently connected hosts.

This returns the host addresses for which there is an established connection in the internal pool. The returned Vec<&str> is a snapshot of the current keys and does not hold any borrow on self afterwards.

Source

pub async fn ensure_connected(&mut self) -> Result<()>

Ensures the client has at least one active connection.

If the client was created with bootstrap hosts and the internal pool is currently empty, this will attempt to connect to the bootstrap hosts in order until one succeeds. It is a no-op when bootstrap_hosts is empty or when the pool already contains connections.

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> Same for T

Source§

type Output = T

Should always be Self
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<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