package kafka-eio

  1. Overview
  2. Docs
Eio-native Kafka client for OCaml 5, built on librdkafka

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.1.1.tar.gz
md5=88c2b9c6d263a1eb114cc5d1b570288a
sha512=44f5395fcc58b8d4303bc0b36113a17193da3a1925125cb0742fabeb96f32eb6b243c98d2fde283b6c95872ff36ddbf96de6e98b7612799e2aefeb2f85a1db95

doc/kafka-eio.producer/Kafka_producer/index.html

Module Kafka_producerSource

Eio-native Kafka producer built on kafka-eio-core.

Sourcetype delivery_mode =
  1. | At_least_once
  2. | At_most_once
  3. | Exactly_once of {
    1. transaction_id : string;
    }
Sourcetype config = {
  1. brokers : string list;
  2. delivery_mode : delivery_mode;
  3. linger_ms : int option;
    (*

    batch window; None = librdkafka default (5 ms)

    *)
  4. security : Kafka_security.t;
    (*

    transport security; use Kafka_security.default for plaintext dev

    *)
  5. properties : (string * string) list;
    (*

    Raw librdkafka config keys applied after every field above, e.g. ("client.id", "checkout-svc"); ("statistics.interval.ms", "5000"). Use for librdkafka options this module has no typed field for. Overrides any of the typed fields' defaults for the same key.

    *)
}
Sourcetype t

A producer may be shared by any number of fibers in the Eio domain that created it (via create's ~sw). It is not domain-safe: sharing one t across multiple Eio domains is unsupported and unguarded.

Sourceval create : config -> sw:Eio.Switch.t -> (t, Kafka_error.t) result

create cfg ~sw creates a producer and starts delivery and poll fibers in sw. When sw is cancelled the fibers stop and the producer is closed.

Sourceval close : t -> unit

Every operation below returns Error Kafka_error.Destroy (or, for produce_await, a promise already resolved to it) once close has been called, instead of touching the underlying (possibly destroyed) handle. close itself also resolves any produce_await promises still awaiting a delivery receipt to Error Kafka_error.Destroy, so a fiber awaiting one cannot hang forever past shutdown.

Sourceval create_topic : t -> topic_name:string -> partitions:int -> replication_factor:int -> (unit, Kafka_error.t) result

create_topic t ~topic_name ~partitions ~replication_factor creates a topic via librdkafka's admin API, reusing this producer's handle. Treats an already-existing topic as success. Does not leak Kafka_raw.kafka_handle the way an earlier raw_handle accessor did — Kafka_raw is internal to kafka-eio-core and not part of this package's public surface.

Sourceval produce : t -> topic:string -> value:bytes option -> ?key:bytes -> ?headers:(string * string option) list -> unit -> (unit, Kafka_error.t) result

Enqueue a message and return immediately. No delivery confirmation. value = None sends a Kafka tombstone (a NULL payload — the delete marker for a key on a compacted topic), distinct from Some Bytes.empty, a genuine zero-length value. value is required (not optional) so a caller must choose explicitly, rather than a forgotten ~value silently sending a tombstone. ~key, if given, is used exactly as passed — including a genuine zero-length key, which Kafka partitions differently (hashed) from no key at all (round- robin/sticky); omitting ~key entirely means no key. A header's value is string option: None sends a NULL-valued header, distinct from Some "".

Can return Error Kafka_error.Queue_full if librdkafka's local send queue is full — this call does not block or retry for you. Callers producing at a high rate must handle it themselves (drop, retry after a delay, or apply their own backpressure); there is currently no Eio-native blocking/backpressure variant of produce.

The trailing unit is required by OCaml's optional-argument erasure rules.

Sourceval produce_await : t -> topic:string -> value:bytes option -> ?key:bytes -> ?headers:(string * string option) list -> unit -> (unit, Kafka_error.t) result Eio.Promise.t

Enqueue a message and return a promise that resolves when the broker acknowledges delivery (or reports an error). value = None sends a tombstone, and ~key/~headers behave exactly as in produce. Can resolve to Error Kafka_error.Queue_full the same way produce can return it — see produce. The trailing unit is required by OCaml's optional-argument erasure rules.

Sourceval flush : t -> timeout_ms:int -> (unit, Kafka_error.t) result

Block until all enqueued messages have been delivered.

Sourcetype txn_failure = {
  1. error : Kafka_error.t;
  2. is_fatal : bool;
  3. is_retriable : bool;
  4. requires_abort : bool;
}

A transactional-API call (begin/commit/abort/send-offsets) itself failed. is_fatal/is_retriable/requires_abort are librdkafka's per-error-instance flags — not derivable from error alone — telling the caller whether the producer must be retired, whether the same call can be retried, and whether the transaction had to be aborted (already done by with_transaction when this is true).

Sourcetype transaction_error =
  1. | App_error of Kafka_error.t
    (*

    f returned Error _, or raised.

    *)
  2. | Txn_failure of txn_failure
    (*

    a transactional-API call itself failed.

    *)
Sourceval string_of_transaction_error : transaction_error -> string
Sourceval with_transaction : t -> ?consumer_offsets:(Kafka_consumer_handle.t * (string * int32 * int64) list) -> (unit -> (unit, Kafka_error.t) result) -> (unit, transaction_error) result

with_transaction t ?consumer_offsets f runs f inside a Kafka transaction. Commits on Ok, aborts on Error or exception. Requires delivery_mode = Exactly_once.

consumer_offsets, if given, is (consumer_handle, offsets) where offsets are the exact (topic, partition, offset-of-last-processed- message) tuples f processed from that consumer — typically built from the consumed messages' topic/partition/offset fields. Only these offsets are committed as part of the transaction; the consumer's current assignment or position is never read, so a transaction can never advance past a message f did not actually process.