odradek_protocol/lib.rs
1//! Sans-I/O implementation of the Kafka wire protocol.
2//!
3//! Kafka is increasingly a *protocol* with multiple independent server and
4//! client implementations, not just the Apache broker. This crate is the
5//! shared foundation of the odradek constellation: it knows how to encode and
6//! decode the protocol, and nothing about sockets, async runtimes, or broker
7//! semantics. That keeps it reusable from the client, the acceptance suite
8//! (which must impersonate both sides of the wire), and the web proxies.
9//!
10//! Layout:
11//! - [`wire`]: primitive codecs — fixed-width big-endian integers, varints,
12//! zigzag varints, (compact/nullable) strings and bytes, array length
13//! prefixes, and tagged fields (flexible versions / KIP-482).
14//! - [`frame`]: the i32 length-prefixed frame envelope around every
15//! request and response.
16//! - [`api_key`]: the API key registry identifying each request type;
17//! [`message::Message`] is the generic spine over every generated
18//! message, and `messages::supported_versions` maps keys to the
19//! schema snapshot's version ranges.
20//! - [`messages`]: versioned request/response structs generated from the
21//! vendored upstream schemas (`cargo xtask codegen`); unknown tagged
22//! fields round-trip raw.
23//! - [`header`]: request/response header codecs and header-version
24//! selection, including the ApiVersions response-header quirk.
25//! - [`records`]: the record batch (v2) codec with CRC-32C validation;
26//! compressed and unknown-codec payloads stay raw and re-encode
27//! byte-identically (the proxy guarantee).
28//! - [`error_code`]: the open-world Kafka error code registry.
29//! - [`error`]: encode/decode error types.
30//! - [`budget`]: the allocation bound every decoder spends against.
31//!
32//! # Bounded decoding
33//!
34//! Wire counts are attacker-controlled and decoded elements are much
35//! larger than the bytes that ask for them, so every decoder here
36//! allocates against a [`budget::Budget`] sized from its input. With
37//! the default [`budget::Limits`], one decode's peak heap is at most
38//! `clamp(16 * n, 64 KiB, 256 MiB)` for the collections it builds, plus
39//! the *n* bytes of `String` contents it copies 1:1 off the wire. At the
40//! 64 MiB [`frame::DEFAULT_MAX_FRAME`] ceiling the absolute cap binds:
41//! **a hostile frame peaks at ~5x its bytes rather than the 28x
42//! measured before.** Pass another policy through the
43//! `decode_with_limits` entry points; the plain `decode` ones apply the
44//! default, so a caller who never hears about any of this is still
45//! bounded. See [`budget`] for why the factor is 16 and what a server
46//! should tighten.
47//!
48//! # Zero-copy decoding
49//!
50//! Decoding a message costs O(fields) rather than O(payload bytes) —
51//! but only when the source is a [`Bytes`](bytes::Bytes). Payload fields
52//! (a fetch response's `records`, and the keys and values inside them)
53//! are then refcounted slices of the caller's buffer, never copies.
54//! The property comes from `Bytes`'s override of `Buf::copy_to_bytes`;
55//! every other `Buf` implementation — slices, `Cursor`, `Chain` — falls
56//! back to the default, which allocates and copies each payload,
57//! measured ~370x slower for a 1 MiB fetch response. Decode from a
58//! `Bytes` on any path that carries records. `tests/zero_copy.rs` holds
59//! the guarantee to it, down to asserting the decoded payloads alias the
60//! input buffer.
61//!
62//! The same aliasing has a cost worth knowing: a decoded payload keeps
63//! its *whole* source buffer alive, because that is what it points into.
64//! Retain one 10-byte record value out of a 64 MiB fetch response and
65//! all 64 MiB stays resident until that value drops. Slices that outlive
66//! the frame they came from — anything going into a cache, a retry
67//! queue, or a channel to another task — want
68//! `Bytes::copy_from_slice(&value)`, which pays one small copy to
69//! release the frame.
70
71// This crate carries no `unsafe` block and has never needed one:
72// forbid rather than deny, so the decision cannot be reversed by a
73// local `allow` in a module nobody re-reads.
74#![forbid(unsafe_code)]
75
76pub mod api_key;
77pub mod budget;
78pub mod consumer_protocol;
79pub mod error;
80pub mod error_code;
81pub mod frame;
82pub mod header;
83pub mod message;
84pub mod messages;
85pub mod records;
86pub mod wire;
87
88pub use api_key::ApiKey;
89pub use budget::{Budget, Limits};
90pub use error::{DecodeError, EncodeError};
91pub use error_code::ErrorCode;
92pub use message::Message;