Skip to main content

KafkaPublisher

Struct KafkaPublisher 

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

A producer handle sharing the broker’s connection.

OutgoingMessage::name is the destination topic. A PARTITION_KEY_HEADER header becomes the record’s native key, so Kafka routes messages that share a key to the same partition; without it the configured partitioner picks one.

Each publish awaits the broker’s delivery report, so an Ok means the cluster accepted the record (durability then depends on the producer’s acks setting, configurable through KafkaBroker::producer_config).

transactional_id upgrades the handle to a transactional one implementing TransactionalPublisher: publishes between begin_transaction and commit become visible atomically (readers on Kafka’s default read_committed isolation see all of them or none), and abort discards them broker-side.

Obtained from KafkaBroker::publisher; usable before Broker::connect resolves the connection (publishing earlier returns KafkaError::NotConnected).

Implementations§

Source§

impl KafkaPublisher

Source

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

How long a publish may wait for space when librdkafka’s local queue is full, before failing with a queue-full error. Without it a publish waits for space indefinitely, which is the natural back-pressure behavior.

Source

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

Upgrades to a transactional publisher fenced by id (Kafka’s transactional.id).

The id must be stable and unique per concurrent producer: Kafka uses it to fence zombies, so two live producers sharing an id abort each other. Create several publishers with distinct ids for concurrent transactional flows. The transactional producer itself is created (and its transactions initialized) on first use, from the broker’s resolved producer configuration.

§Examples
use ruststream_rdkafka::KafkaBroker;

let broker = KafkaBroker::new(["localhost:9092"]);
let replies = broker.publisher().transactional_id("orders-svc-1");
Source

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

How long transaction control calls (init, commit, abort) may block before reporting failure. Defaults to 30 seconds; this is the call deadline handed to librdkafka, not its transaction.timeout.ms (reachable through KafkaBroker::producer_config).

Only meaningful after transactional_id.

Trait Implementations§

Source§

impl Clone for KafkaPublisher

Source§

fn clone(&self) -> KafkaPublisher

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 Debug for KafkaPublisher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Publisher for KafkaPublisher

Source§

async fn publish(&self, msg: OutgoingMessage<'_>) -> Result<(), Self::Error>

Publishes msg to the topic named by OutgoingMessage::name and awaits the delivery report. Inside an open transaction the record joins it; otherwise it goes out through the broker’s shared plain producer, transactional id or not.

§Errors

Returns KafkaError::NotConnected before Broker::connect resolves the connection and KafkaError::Publish when the cluster rejects the record or the delivery times out (librdkafka’s message.timeout.ms).

§Cancel safety

Not cancel safe: dropping the future may leave the record in flight, delivered or not.

Source§

type Error = KafkaError

The error type returned by publish.
Source§

impl TransactionalPublisher for KafkaPublisher

Source§

async fn begin_transaction(&self) -> Result<(), Self::Error>

Begins a Kafka transaction (creating and initializing the transactional producer on first use).

One producer runs one transaction at a time, so beginning while one is open is an error, not a queue: a second begin means two flows share one publisher, and silently merging their messages into one transaction would commit one flow’s records with the other’s. Concurrent transactional flows use distinct publishers (see TransactionalPartitions).

§Errors

Returns KafkaError::InvalidOptions without a transactional_id, KafkaError::TransactionBusy when a transaction is already open on this publisher (or a clone sharing its id), KafkaError::NotConnected before Broker::connect, and KafkaError::Publish when initialization or the begin call fails.

Source§

async fn commit(&self) -> Result<(), Self::Error>

Commits the open transaction, making its records visible atomically; a no-op when none is open.

§Errors

Returns KafkaError::Publish when the commit fails. librdkafka distinguishes retriable failures from ones requiring an abort; after an error the transaction’s state is unresolved, so treat the publisher as needing an abort or replacement.

Source§

async fn abort(&self) -> Result<(), Self::Error>

Aborts the open transaction, discarding its records broker-side; a no-op when none is open.

§Errors

Returns KafkaError::Publish when the abort fails.

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> 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<P> ErasedPublisher for P
where P: Publisher,

Source§

fn publish_bytes<'a>( &'a self, name: &'a str, payload: &'a [u8], ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Sync + Send>>> + Send + 'a>>

Publishes payload to name, with no headers. Read more
Source§

fn publish_message<'a>( &'a self, name: &'a str, payload: &'a [u8], headers: &'a Headers, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Sync + Send>>> + Send + 'a>>

Publishes payload to name with headers. 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> 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.
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