kafka_api/schemata/sync_group_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::*;
18use crate::IoResult;
19
20// Version 1 adds throttle time.
21//
22// Starting in version 2, on quota violation, brokers send out responses before throttling.
23//
24// Starting from version 3, syncGroupRequest supports a new field called groupInstanceId to indicate
25// member identity across restarts.
26//
27// Version 4 is the first flexible version.
28//
29// Starting from version 5, the broker sends back the Protocol Type and the Protocol Name
30// to the client (KIP-559).
31
32#[derive(Debug, Default, Clone)]
33pub struct SyncGroupResponse {
34 /// The duration in milliseconds for which the request was throttled due to a quota violation,
35 /// or zero if the request did not violate any quota.
36 pub throttle_time_ms: i32,
37 /// The error code, or 0 if there was no error.
38 pub error_code: i16,
39 /// The group protocol type.
40 pub protocol_type: Option<String>,
41 /// The group protocol name
42 pub protocol_name: Option<String>,
43 /// The member assignment.
44 pub assignment: Vec<u8>,
45 /// Unknown tagged fields.
46 pub unknown_tagged_fields: Vec<RawTaggedField>,
47}
48
49impl Encodable for SyncGroupResponse {
50 fn write<B: WriteBytesExt>(&self, buf: &mut B, version: i16) -> IoResult<()> {
51 if version >= 1 {
52 Int32.encode(buf, self.throttle_time_ms)?;
53 }
54 Int16.encode(buf, self.error_code)?;
55 if version >= 5 {
56 NullableString(true).encode(buf, self.protocol_type.as_deref())?;
57 NullableString(true).encode(buf, self.protocol_name.as_deref())?;
58 }
59 NullableBytes(version >= 4).encode(buf, &self.assignment)?;
60 if version >= 4 {
61 RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
62 }
63 Ok(())
64 }
65
66 fn calculate_size(&self, version: i16) -> usize {
67 let mut res = 0;
68 if version >= 1 {
69 res += Int32::SIZE; // self.throttle_time_ms
70 }
71 res += Int16::SIZE; // self.error_code
72 if version >= 5 {
73 res += NullableString(true).calculate_size(self.protocol_type.as_deref());
74 res += NullableString(true).calculate_size(self.protocol_name.as_deref());
75 }
76 res += NullableBytes(version >= 4).calculate_size(&self.assignment);
77 if version >= 4 {
78 res += RawTaggedFieldList.calculate_size(&self.unknown_tagged_fields);
79 }
80 res
81 }
82}