Skip to main content

Producer

Struct Producer 

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

Cheap handle that appends records to a topic, routing each to a partition.

Producer is Clone (wraps Arc<Topic>); cloning is free and every clone writes to the same topic. Records are timestamped at send-time. Routing: a keyed record goes to hash(key) % partition_count (so same-key records keep their order); a keyless record is spread round-robin across partitions.

Implementations§

Source§

impl Producer

Source

pub fn new(topic: Arc<Topic>) -> Self

Wraps a Topic in a Producer. Prefer Kafko::producer_for which looks the topic up by name.

Source

pub fn partition_count(&self) -> u32

Number of partitions on the topic this producer writes to.

Source

pub async fn send( &self, key: Option<Bytes>, value: Bytes, ) -> Result<RecordPosition>

Appends a record, routing by key, and returns its RecordPosition (the partition it landed on plus its offset within that partition).

Resolves once the bytes are in the OS file (page cache) — the same durability contract as Kafka acks=1.

§Example
use bytes::Bytes;
use kafko::Kafko;

let broker = Kafko::open("./data").await?;
broker.create_topic("orders").await?;
let producer = broker.producer_for("orders").await?;

let pos = producer.send(Some(Bytes::from("cust-1")), Bytes::from("order-1")).await?;
println!("partition {} offset {}", pos.partition(), pos.offset());
Source

pub async fn send_to( &self, partition: u32, key: Option<Bytes>, value: Bytes, ) -> Result<RecordPosition>

Appends a record to an explicit partition, ignoring key-based routing. Errors with KafkoError::InvalidPartitionCount if partition is out of range for the topic.

Source

pub async fn send_record(&self, record: Record) -> Result<RecordPosition>

Appends an already-constructed Record (preserving its timestamp), routing by the record’s own key, and returns its RecordPosition.

Source

pub async fn send_batch( &self, items: Vec<(Option<Bytes>, Bytes)>, ) -> Result<Vec<RecordPosition>>

Appends a batch of records, timestamping each at the moment of the call, and returns their RecordPositions in input order.

Records are grouped by their routed partition and each group is written as one atomic append. Atomicity is per partition: a batch whose records span partitions is atomic within each partition, not across them. For a single-partition topic this is one fully-atomic append, identical to a single Log::append_batch.

An empty input is a no-op returning Ok(Vec::new()).

Source

pub async fn send_batch_records( &self, records: Vec<Record>, ) -> Result<Vec<RecordPosition>>

Like send_batch, but takes already-constructed records so callers can preserve per-record timestamps. Each record routes by its own key; the per-partition atomicity contract is identical.

Trait Implementations§

Source§

impl Clone for Producer

Source§

fn clone(&self) -> Producer

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

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.