Skip to main content

odradek_protocol/
message.rs

1//! The trait every generated request/response message implements.
2//!
3//! Generated inherent methods remain the primary API; this trait is the
4//! generic spine over them, so dispatch loops (a proxy's forwarding
5//! core, the acceptance suite's exchange helpers) can be written once
6//! instead of per message type. Implementations are emitted by
7//! `cargo xtask codegen` for every top-level message.
8
9use bytes::{Buf, BufMut};
10
11use crate::budget::Limits;
12use crate::error::{DecodeError, EncodeError};
13
14/// A versioned Kafka message: encode/decode plus its identity in the
15/// api-key registry.
16///
17/// Generated code implements this for every top-level message, but the
18/// trait is public because hand-written implementations are a reason it
19/// exists — a proxy with its own representation of a message can join
20/// the same dispatch loops. Anything added here later will come with a
21/// default body, so such an implementation does not break;
22/// [`Message::decode_with_limits`] was added exactly that way and its
23/// documentation records why.
24pub trait Message: Sized {
25    /// The api key identifying this message's request type.
26    const API_KEY: i16;
27    /// The lowest schema version this snapshot can speak.
28    const MIN_VERSION: i16;
29    /// The highest schema version this snapshot can speak.
30    const MAX_VERSION: i16;
31
32    fn encode(&self, buf: &mut impl BufMut, version: i16) -> Result<(), EncodeError>;
33
34    /// Decode one message of `version`.
35    ///
36    /// Decoding costs O(fields), not O(payload bytes), but only when
37    /// `buf` is a [`Bytes`](bytes::Bytes): payload fields (a fetch
38    /// response's `records`, and the keys and values inside them) are
39    /// then refcounted slices of the caller's buffer rather than copies.
40    /// That comes from `Bytes`'s override of `Buf::copy_to_bytes`; other
41    /// `Buf` implementations — slices, `Cursor`, `Chain` — get the
42    /// default, which allocates and copies every payload, measured ~370x
43    /// slower for a 1 MiB fetch response. Decode from a `Bytes` on any
44    /// path that carries records. `tests/zero_copy.rs` holds the
45    /// property to it.
46    ///
47    /// Allocation is bounded by the default
48    /// [`Limits`], derived from `buf`'s remaining length; see
49    /// [`Message::decode_with_limits`].
50    fn decode(buf: &mut impl Buf, version: i16) -> Result<Self, DecodeError>;
51
52    /// Decode one message of `version` under `limits`.
53    ///
54    /// Every generated message overrides this. The default is here so
55    /// that adding it broke no hand-written implementation: it ignores
56    /// `limits` and decodes under that type's own default bound, which
57    /// is never *less* safe than [`Message::decode`].
58    /// How long the broker wants this client to wait before sending
59    /// more, or `None` for a message that has no such field.
60    ///
61    /// Kafka's quota mechanism is advisory in the only way that
62    /// matters: the broker answers, sets this, and then stops reading
63    /// from the connection for that long. A client that ignores it does
64    /// not get faster — its next request simply sits unanswered until
65    /// the mute expires, which is indistinguishable from a hung broker
66    /// and will trip a request timeout instead of a backoff.
67    ///
68    /// Generated for every message whose schema carries the field;
69    /// defaulted here so that adding it broke no hand-written
70    /// implementation, the way [`Message::decode_with_limits`] was.
71    fn throttle_time_ms(&self) -> Option<i32> {
72        None
73    }
74
75    fn decode_with_limits(
76        buf: &mut impl Buf,
77        version: i16,
78        limits: Limits,
79    ) -> Result<Self, DecodeError> {
80        let _ = limits;
81        Self::decode(buf, version)
82    }
83}