kafka_api/schemata/
init_producer_id_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// Starting in version 1, on quota violation, brokers send out responses before throttling.
21//
22// Version 2 is the first flexible version.
23//
24// Version 3 is the same as version 2.
25//
26// Version 4 adds the support for new error code PRODUCER_FENCED.
27
28#[derive(Debug, Default, Clone)]
29pub struct InitProducerIdResponse {
30    /// The duration in milliseconds for which the request was throttled due to a quota violation,
31    /// or zero if the request did not violate any quota.
32    pub throttle_time_ms: i32,
33    /// The error code, or 0 if there was no error.
34    pub error_code: i16,
35    /// The current producer id.
36    pub producer_id: i64,
37    /// The current epoch associated with the producer id.
38    pub producer_epoch: i16,
39    /// Unknown tagged fields.
40    pub unknown_tagged_fields: Vec<RawTaggedField>,
41}
42
43impl Encodable for InitProducerIdResponse {
44    fn write<B: WriteBytesExt>(&self, buf: &mut B, version: i16) -> IoResult<()> {
45        Int32.encode(buf, self.throttle_time_ms)?;
46        Int16.encode(buf, self.error_code)?;
47        Int64.encode(buf, self.producer_id)?;
48        Int16.encode(buf, self.producer_epoch)?;
49        if version >= 2 {
50            RawTaggedFieldList.encode(buf, &self.unknown_tagged_fields)?;
51        }
52        Ok(())
53    }
54
55    fn calculate_size(&self, version: i16) -> usize {
56        let mut res = 0;
57        res += Int32::SIZE; // self.throttle_time_ms
58        res += Int16::SIZE; // self.error_code
59        res += Int64::SIZE; // self.producer_id
60        res += Int16::SIZE; // self.producer_epoch
61        if version >= 2 {
62            res += RawTaggedFieldList.calculate_size(&self.unknown_tagged_fields);
63        }
64        res
65    }
66}