Expand description
Sans-I/O implementation of the Kafka wire protocol.
Kafka is increasingly a protocol with multiple independent server and client implementations, not just the Apache broker. This crate is the shared foundation of the odradek constellation: it knows how to encode and decode the protocol, and nothing about sockets, async runtimes, or broker semantics. That keeps it reusable from the client, the acceptance suite (which must impersonate both sides of the wire), and the web proxies.
Layout:
wire: primitive codecs — fixed-width big-endian integers, varints, zigzag varints, (compact/nullable) strings and bytes, array length prefixes, and tagged fields (flexible versions / KIP-482).frame: the i32 length-prefixed frame envelope around every request and response.api_key: the API key registry identifying each request type;message::Messageis the generic spine over every generated message, andmessages::supported_versionsmaps keys to the schema snapshot’s version ranges.messages: versioned request/response structs generated from the vendored upstream schemas (cargo xtask codegen); unknown tagged fields round-trip raw.header: request/response header codecs and header-version selection, including the ApiVersions response-header quirk.records: the record batch (v2) codec with CRC-32C validation; compressed and unknown-codec payloads stay raw and re-encode byte-identically (the proxy guarantee).error_code: the open-world Kafka error code registry.error: encode/decode error types.budget: the allocation bound every decoder spends against.
§Bounded decoding
Wire counts are attacker-controlled and decoded elements are much
larger than the bytes that ask for them, so every decoder here
allocates against a budget::Budget sized from its input. With
the default budget::Limits, one decode’s peak heap is at most
clamp(16 * n, 64 KiB, 256 MiB) for the collections it builds, plus
the n bytes of String contents it copies 1:1 off the wire. At the
64 MiB frame::DEFAULT_MAX_FRAME ceiling the absolute cap binds:
a hostile frame peaks at ~5x its bytes rather than the 28x
measured before. Pass another policy through the
decode_with_limits entry points; the plain decode ones apply the
default, so a caller who never hears about any of this is still
bounded. See budget for why the factor is 16 and what a server
should tighten.
§Zero-copy decoding
Decoding a message costs O(fields) rather than O(payload bytes) —
but only when the source is a Bytes. Payload fields
(a fetch response’s records, and the keys and values inside them)
are then refcounted slices of the caller’s buffer, never copies.
The property comes from Bytes’s override of Buf::copy_to_bytes;
every other Buf implementation — slices, Cursor, Chain — falls
back to the default, which allocates and copies each payload,
measured ~370x slower for a 1 MiB fetch response. Decode from a
Bytes on any path that carries records. tests/zero_copy.rs holds
the guarantee to it, down to asserting the decoded payloads alias the
input buffer.
The same aliasing has a cost worth knowing: a decoded payload keeps
its whole source buffer alive, because that is what it points into.
Retain one 10-byte record value out of a 64 MiB fetch response and
all 64 MiB stays resident until that value drops. Slices that outlive
the frame they came from — anything going into a cache, a retry
queue, or a channel to another task — want
Bytes::copy_from_slice(&value), which pays one small copy to
release the frame.
Re-exports§
pub use api_key::ApiKey;pub use budget::Budget;pub use budget::Limits;pub use error::DecodeError;pub use error::EncodeError;pub use error_code::ErrorCode;pub use message::Message;
Modules§
- api_key
- The Kafka API key registry.
- budget
- The decode allocation budget: what stops a small hostile frame from becoming a large hostile allocation.
- consumer_
protocol - The classic consumer protocol’s embedded payloads.
- error
- Error types shared by the wire codecs.
- error_
code - Kafka protocol error codes.
- frame
- The Kafka frame envelope: an
i32length prefix around every request and response. - header
- Request/response header version selection.
- message
- The trait every generated request/response message implements.
- messages
- Kafka message types generated from the vendored schemas.
- records
- Record batch (v2) encoding — the record set carried in Produce and
Fetch
recordsfields. - wire
- Primitive codecs for the Kafka wire format.