kafka_api/schemata/heartbeat_response.rs
1// Copyright 2024 tison <wander4096@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use byteorder::WriteBytesExt;
16
17use crate::codec::Encodable;
18use crate::codec::Encoder;
19use crate::codec::FixedSizeEncoder;
20use crate::codec::Int16;
21use crate::codec::Int32;
22use crate::codec::RawTaggedField;
23use crate::codec::RawTaggedFieldList;
24use crate::IoResult;
25
26// Version 1 adds throttle time.
27//
28// Starting in version 2, on quota violation, brokers send out responses before throttling.
29//
30// Starting from version 3, heartbeatRequest supports a new field called groupInstanceId to indicate
31// member identity across restarts.
32//
33// Version 4 is the first flexible version.
34#[derive(Debug, Default, Clone)]
35pub struct HeartbeatResponse {
36 /// The duration in milliseconds for which the request was throttled due to a quota violation,
37 /// or zero if the request did not violate any quota.
38 pub throttle_time_ms: i32,
39 /// The error code, or 0 if there was no error.
40 pub error_code: i16,
41 /// Unknown tagged fields.
42 pub unknown_tagged_fields: Vec<RawTaggedField>,
43}
44
45impl Encodable for HeartbeatResponse {
46 fn write<B: WriteBytesExt>(&self, buf: &mut B, version: i16) -> IoResult<()> {
47 if version >= 1 {
48 Int32.encode(buf, self.throttle_time_ms)?;
49 }
50 Int16.encode(buf, self.error_code)?;
51 if version >= 4 {
52 RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
53 }
54 Ok(())
55 }
56
57 fn calculate_size(&self, version: i16) -> usize {
58 let mut res = 0;
59 if version >= 1 {
60 res += Int32::SIZE; // self.throttle_time_ms
61 }
62 res += Int16::SIZE; // self.error_code
63 if version >= 4 {
64 res += RawTaggedFieldList.calculate_size(&self.unknown_tagged_fields);
65 }
66 res
67 }
68}