forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_versions_response.go
172 lines (143 loc) · 3.56 KB
/
api_versions_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package sarama
import (
"time"
)
// ApiVersionsResponseKey contains the APIs supported by the broker.
type ApiVersionsResponseKey struct {
// Version defines the protocol version to use for encode and decode
Version int16
// ApiKey contains the API index.
ApiKey int16
// MinVersion contains the minimum supported version, inclusive.
MinVersion int16
// MaxVersion contains the maximum supported version, inclusive.
MaxVersion int16
}
func (a *ApiVersionsResponseKey) encode(pe packetEncoder, version int16) (err error) {
a.Version = version
pe.putInt16(a.ApiKey)
pe.putInt16(a.MinVersion)
pe.putInt16(a.MaxVersion)
if version >= 3 {
pe.putEmptyTaggedFieldArray()
}
return nil
}
func (a *ApiVersionsResponseKey) Decode(pd packetDecoder, version int16) (err error) {
a.Version = version
if a.ApiKey, err = pd.getInt16(); err != nil {
return err
}
if a.MinVersion, err = pd.getInt16(); err != nil {
return err
}
if a.MaxVersion, err = pd.getInt16(); err != nil {
return err
}
if version >= 3 {
if _, err := pd.getEmptyTaggedFieldArray(); err != nil {
return err
}
}
return nil
}
type ApiVersionsResponse struct {
// Version defines the protocol version to use for encode and decode
Version int16
// ErrorCode contains the top-level error code.
ErrorCode int16
// ApiKeys contains the APIs supported by the broker.
ApiKeys []ApiVersionsResponseKey
// 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.
ThrottleTimeMs int32
}
func (r *ApiVersionsResponse) Encode(pe packetEncoder) (err error) {
pe.putInt16(r.ErrorCode)
if r.Version >= 3 {
pe.putCompactArrayLength(len(r.ApiKeys))
} else {
if err := pe.putArrayLength(len(r.ApiKeys)); err != nil {
return err
}
}
for _, block := range r.ApiKeys {
if err := block.encode(pe, r.Version); err != nil {
return err
}
}
if r.Version >= 1 {
pe.putInt32(r.ThrottleTimeMs)
}
if r.Version >= 3 {
pe.putEmptyTaggedFieldArray()
}
return nil
}
func (r *ApiVersionsResponse) Decode(pd packetDecoder, version int16) (err error) {
r.Version = version
if r.ErrorCode, err = pd.getInt16(); err != nil {
return err
}
var numApiKeys int
if r.Version >= 3 {
numApiKeys, err = pd.getCompactArrayLength()
if err != nil {
return err
}
} else {
numApiKeys, err = pd.getArrayLength()
if err != nil {
return err
}
}
r.ApiKeys = make([]ApiVersionsResponseKey, numApiKeys)
for i := 0; i < numApiKeys; i++ {
var block ApiVersionsResponseKey
if err = block.Decode(pd, r.Version); err != nil {
return err
}
r.ApiKeys[i] = block
}
if r.Version >= 1 {
if r.ThrottleTimeMs, err = pd.getInt32(); err != nil {
return err
}
}
if r.Version >= 3 {
if _, err = pd.getEmptyTaggedFieldArray(); err != nil {
return err
}
}
return nil
}
func (r *ApiVersionsResponse) APIKey() int16 {
return 18
}
func (r *ApiVersionsResponse) APIVersion() int16 {
return r.Version
}
func (r *ApiVersionsResponse) HeaderVersion() int16 {
// ApiVersionsResponse always includes a v0 header.
// See KIP-511 for details
return 0
}
func (r *ApiVersionsResponse) IsValidVersion() bool {
return r.Version >= 0 && r.Version <= 3
}
func (r *ApiVersionsResponse) RequiredVersion() KafkaVersion {
switch r.Version {
case 3:
return V2_4_0_0
case 2:
return V2_0_0_0
case 1:
return V0_11_0_0
case 0:
return V0_10_0_0
default:
return V2_4_0_0
}
}
func (r *ApiVersionsResponse) throttleTime() time.Duration {
return time.Duration(r.ThrottleTimeMs) * time.Millisecond
}