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
impl Producer
Sourcepub fn new(topic: Arc<Topic>) -> Self
pub fn new(topic: Arc<Topic>) -> Self
Wraps a Topic in a Producer. Prefer Kafko::producer_for which
looks the topic up by name.
Sourcepub fn partition_count(&self) -> u32
pub fn partition_count(&self) -> u32
Number of partitions on the topic this producer writes to.
Sourcepub async fn send(
&self,
key: Option<Bytes>,
value: Bytes,
) -> Result<RecordPosition>
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());Sourcepub async fn send_to(
&self,
partition: u32,
key: Option<Bytes>,
value: Bytes,
) -> Result<RecordPosition>
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.
Sourcepub async fn send_record(&self, record: Record) -> Result<RecordPosition>
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.
Sourcepub async fn send_batch(
&self,
items: Vec<(Option<Bytes>, Bytes)>,
) -> Result<Vec<RecordPosition>>
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()).
Sourcepub async fn send_batch_records(
&self,
records: Vec<Record>,
) -> Result<Vec<RecordPosition>>
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.