1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! 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::Message`] is the generic spine over every generated
//! message, and `messages::supported_versions` maps 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`](bytes::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.
// This crate carries no `unsafe` block and has never needed one:
// forbid rather than deny, so the decision cannot be reversed by a
// local `allow` in a module nobody re-reads.
pub use ApiKey;
pub use ;
pub use ;
pub use ErrorCode;
pub use Message;