cubefs
156 строк · 3.3 Кб
1package sarama
2
3// ApiVersionsResponseKey contains the APIs supported by the broker.
4type ApiVersionsResponseKey struct {
5// Version defines the protocol version to use for encode and decode
6Version int16
7// ApiKey contains the API index.
8ApiKey int16
9// MinVersion contains the minimum supported version, inclusive.
10MinVersion int16
11// MaxVersion contains the maximum supported version, inclusive.
12MaxVersion int16
13}
14
15func (a *ApiVersionsResponseKey) encode(pe packetEncoder, version int16) (err error) {
16a.Version = version
17pe.putInt16(a.ApiKey)
18
19pe.putInt16(a.MinVersion)
20
21pe.putInt16(a.MaxVersion)
22
23if version >= 3 {
24pe.putEmptyTaggedFieldArray()
25}
26
27return nil
28}
29
30func (a *ApiVersionsResponseKey) decode(pd packetDecoder, version int16) (err error) {
31a.Version = version
32if a.ApiKey, err = pd.getInt16(); err != nil {
33return err
34}
35
36if a.MinVersion, err = pd.getInt16(); err != nil {
37return err
38}
39
40if a.MaxVersion, err = pd.getInt16(); err != nil {
41return err
42}
43
44if version >= 3 {
45if _, err := pd.getEmptyTaggedFieldArray(); err != nil {
46return err
47}
48}
49
50return nil
51}
52
53type ApiVersionsResponse struct {
54// Version defines the protocol version to use for encode and decode
55Version int16
56// ErrorCode contains the top-level error code.
57ErrorCode int16
58// ApiKeys contains the APIs supported by the broker.
59ApiKeys []ApiVersionsResponseKey
60// ThrottleTimeMs contains the duration in milliseconds for which the request was throttled due to a quota violation, or zero if the request did not violate any quota.
61ThrottleTimeMs int32
62}
63
64func (r *ApiVersionsResponse) encode(pe packetEncoder) (err error) {
65pe.putInt16(r.ErrorCode)
66
67if r.Version >= 3 {
68pe.putCompactArrayLength(len(r.ApiKeys))
69} else {
70if err := pe.putArrayLength(len(r.ApiKeys)); err != nil {
71return err
72}
73}
74for _, block := range r.ApiKeys {
75if err := block.encode(pe, r.Version); err != nil {
76return err
77}
78}
79
80if r.Version >= 1 {
81pe.putInt32(r.ThrottleTimeMs)
82}
83
84if r.Version >= 3 {
85pe.putEmptyTaggedFieldArray()
86}
87
88return nil
89}
90
91func (r *ApiVersionsResponse) decode(pd packetDecoder, version int16) (err error) {
92r.Version = version
93if r.ErrorCode, err = pd.getInt16(); err != nil {
94return err
95}
96
97var numApiKeys int
98if r.Version >= 3 {
99numApiKeys, err = pd.getCompactArrayLength()
100if err != nil {
101return err
102}
103} else {
104numApiKeys, err = pd.getArrayLength()
105if err != nil {
106return err
107}
108}
109r.ApiKeys = make([]ApiVersionsResponseKey, numApiKeys)
110for i := 0; i < numApiKeys; i++ {
111var block ApiVersionsResponseKey
112if err = block.decode(pd, r.Version); err != nil {
113return err
114}
115r.ApiKeys[i] = block
116}
117
118if r.Version >= 1 {
119if r.ThrottleTimeMs, err = pd.getInt32(); err != nil {
120return err
121}
122}
123
124if r.Version >= 3 {
125if _, err = pd.getEmptyTaggedFieldArray(); err != nil {
126return err
127}
128}
129
130return nil
131}
132
133func (r *ApiVersionsResponse) key() int16 {
134return 18
135}
136
137func (r *ApiVersionsResponse) version() int16 {
138return r.Version
139}
140
141func (r *ApiVersionsResponse) headerVersion() int16 {
142// ApiVersionsResponse always includes a v0 header.
143// See KIP-511 for details
144return 0
145}
146
147func (r *ApiVersionsResponse) requiredVersion() KafkaVersion {
148switch r.Version {
149case 0:
150return V0_10_0_0
151case 3:
152return V2_4_0_0
153default:
154return V0_10_0_0
155}
156}
157