kafka_api/schemata/heartbeat_request.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::ReadBytesExt;
16
17use crate::codec::err_decode_message_null;
18use crate::codec::Decodable;
19use crate::codec::Decoder;
20use crate::codec::Int32;
21use crate::codec::NullableString;
22use crate::codec::RawTaggedField;
23use crate::codec::RawTaggedFieldList;
24use crate::IoResult;
25
26// Version 1 and version 2 are the same as version 0.
27//
28// Starting from version 3, we add a new field called groupInstanceId to indicate member identity
29// across restarts.
30//
31// Version 4 is the first flexible version.
32#[derive(Debug, Default, Clone)]
33pub struct HeartbeatRequest {
34 /// The group id.
35 pub group_id: String,
36 /// The generation of the group.
37 pub generation_id: i32,
38 /// The member ID.
39 pub member_id: String,
40 /// The unique identifier of the consumer instance provided by end user.
41 pub group_instance_id: Option<String>,
42 /// Unknown tagged fields.
43 pub unknown_tagged_fields: Vec<RawTaggedField>,
44}
45
46impl Decodable for HeartbeatRequest {
47 fn read<B: ReadBytesExt>(buf: &mut B, version: i16) -> IoResult<Self> {
48 let mut this = HeartbeatRequest {
49 group_id: NullableString(version >= 4)
50 .decode(buf)?
51 .ok_or_else(|| err_decode_message_null("topics"))?,
52 generation_id: Int32.decode(buf)?,
53 member_id: NullableString(version >= 4)
54 .decode(buf)?
55 .ok_or_else(|| err_decode_message_null("member_id"))?,
56 ..Default::default()
57 };
58 if version >= 3 {
59 this.group_instance_id = NullableString(version >= 4).decode(buf)?;
60 }
61 if version >= 4 {
62 this.unknown_tagged_fields = RawTaggedFieldList.decode(buf)?;
63 }
64 Ok(this)
65 }
66}