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
impl AsyncKafkaClient
Sourcepub async fn new(hosts: Vec<String>) -> Result<Self>
pub async fn new(hosts: Vec<String>) -> Result<Self>
Creates a new async client and connects to the bootstrap brokers.
Sourcepub async fn with_client_id(
hosts: Vec<String>,
client_id: String,
) -> Result<Self>
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.
Sourcepub async fn with_client_id_and_security(
hosts: Vec<String>,
client_id: String,
security: Option<SecurityConfig>,
) -> Result<Self>
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.
Sourcepub fn bootstrap_hosts(&self) -> &[String]
pub fn bootstrap_hosts(&self) -> &[String]
Returns the bootstrap hosts.
Sourcepub fn security(&self) -> Option<&SecurityConfig>
pub fn security(&self) -> Option<&SecurityConfig>
Returns the configured optional security settings.
Sourcepub async fn get_connection(
&mut self,
host: &str,
) -> Result<&mut AsyncConnection>
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.
Sourcepub fn connected_hosts(&self) -> Vec<&str>
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.
Sourcepub async fn ensure_connected(&mut self) -> Result<()>
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.