kesavand | c71914f | 2022-03-25 11:19:03 +0530 | [diff] [blame^] | 1 | package sarama |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // DescribeClientQuotas Response (Version: 0) => throttle_time_ms error_code error_message [entries] |
| 8 | // throttle_time_ms => INT32 |
| 9 | // error_code => INT16 |
| 10 | // error_message => NULLABLE_STRING |
| 11 | // entries => [entity] [values] |
| 12 | // entity => entity_type entity_name |
| 13 | // entity_type => STRING |
| 14 | // entity_name => NULLABLE_STRING |
| 15 | // values => key value |
| 16 | // key => STRING |
| 17 | // value => FLOAT64 |
| 18 | |
| 19 | type DescribeClientQuotasResponse struct { |
| 20 | ThrottleTime time.Duration // 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. |
| 21 | ErrorCode KError // The error code, or `0` if the quota description succeeded. |
| 22 | ErrorMsg *string // The error message, or `null` if the quota description succeeded. |
| 23 | Entries []DescribeClientQuotasEntry // A result entry. |
| 24 | } |
| 25 | |
| 26 | type DescribeClientQuotasEntry struct { |
| 27 | Entity []QuotaEntityComponent // The quota entity description. |
| 28 | Values map[string]float64 // The quota values for the entity. |
| 29 | } |
| 30 | |
| 31 | type QuotaEntityComponent struct { |
| 32 | EntityType QuotaEntityType |
| 33 | MatchType QuotaMatchType |
| 34 | Name string |
| 35 | } |
| 36 | |
| 37 | func (d *DescribeClientQuotasResponse) encode(pe packetEncoder) error { |
| 38 | // ThrottleTime |
| 39 | pe.putInt32(int32(d.ThrottleTime / time.Millisecond)) |
| 40 | |
| 41 | // ErrorCode |
| 42 | pe.putInt16(int16(d.ErrorCode)) |
| 43 | |
| 44 | // ErrorMsg |
| 45 | if err := pe.putNullableString(d.ErrorMsg); err != nil { |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | // Entries |
| 50 | if err := pe.putArrayLength(len(d.Entries)); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | for _, e := range d.Entries { |
| 54 | if err := e.encode(pe); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | func (d *DescribeClientQuotasResponse) decode(pd packetDecoder, version int16) error { |
| 63 | // ThrottleTime |
| 64 | throttleTime, err := pd.getInt32() |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | d.ThrottleTime = time.Duration(throttleTime) * time.Millisecond |
| 69 | |
| 70 | // ErrorCode |
| 71 | errCode, err := pd.getInt16() |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | d.ErrorCode = KError(errCode) |
| 76 | |
| 77 | // ErrorMsg |
| 78 | errMsg, err := pd.getNullableString() |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | d.ErrorMsg = errMsg |
| 83 | |
| 84 | // Entries |
| 85 | entryCount, err := pd.getArrayLength() |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | if entryCount > 0 { |
| 90 | d.Entries = make([]DescribeClientQuotasEntry, entryCount) |
| 91 | for i := range d.Entries { |
| 92 | e := DescribeClientQuotasEntry{} |
| 93 | if err = e.decode(pd, version); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | d.Entries[i] = e |
| 97 | } |
| 98 | } else { |
| 99 | d.Entries = []DescribeClientQuotasEntry{} |
| 100 | } |
| 101 | |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | func (d *DescribeClientQuotasEntry) encode(pe packetEncoder) error { |
| 106 | // Entity |
| 107 | if err := pe.putArrayLength(len(d.Entity)); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | for _, e := range d.Entity { |
| 111 | if err := e.encode(pe); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Values |
| 117 | if err := pe.putArrayLength(len(d.Values)); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | for key, value := range d.Values { |
| 121 | // key |
| 122 | if err := pe.putString(key); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | // value |
| 126 | pe.putFloat64(value) |
| 127 | } |
| 128 | |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | func (d *DescribeClientQuotasEntry) decode(pd packetDecoder, version int16) error { |
| 133 | // Entity |
| 134 | componentCount, err := pd.getArrayLength() |
| 135 | if err != nil { |
| 136 | return err |
| 137 | } |
| 138 | if componentCount > 0 { |
| 139 | d.Entity = make([]QuotaEntityComponent, componentCount) |
| 140 | for i := 0; i < componentCount; i++ { |
| 141 | component := QuotaEntityComponent{} |
| 142 | if err := component.decode(pd, version); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | d.Entity[i] = component |
| 146 | } |
| 147 | } else { |
| 148 | d.Entity = []QuotaEntityComponent{} |
| 149 | } |
| 150 | |
| 151 | // Values |
| 152 | valueCount, err := pd.getArrayLength() |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | if valueCount > 0 { |
| 157 | d.Values = make(map[string]float64, valueCount) |
| 158 | for i := 0; i < valueCount; i++ { |
| 159 | // key |
| 160 | key, err := pd.getString() |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | // value |
| 165 | value, err := pd.getFloat64() |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | d.Values[key] = value |
| 170 | } |
| 171 | } else { |
| 172 | d.Values = map[string]float64{} |
| 173 | } |
| 174 | |
| 175 | return nil |
| 176 | } |
| 177 | |
| 178 | func (c *QuotaEntityComponent) encode(pe packetEncoder) error { |
| 179 | // entity_type |
| 180 | if err := pe.putString(string(c.EntityType)); err != nil { |
| 181 | return err |
| 182 | } |
| 183 | // entity_name |
| 184 | if c.MatchType == QuotaMatchDefault { |
| 185 | if err := pe.putNullableString(nil); err != nil { |
| 186 | return err |
| 187 | } |
| 188 | } else { |
| 189 | if err := pe.putString(c.Name); err != nil { |
| 190 | return err |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return nil |
| 195 | } |
| 196 | |
| 197 | func (c *QuotaEntityComponent) decode(pd packetDecoder, version int16) error { |
| 198 | // entity_type |
| 199 | entityType, err := pd.getString() |
| 200 | if err != nil { |
| 201 | return err |
| 202 | } |
| 203 | c.EntityType = QuotaEntityType(entityType) |
| 204 | |
| 205 | // entity_name |
| 206 | entityName, err := pd.getNullableString() |
| 207 | if err != nil { |
| 208 | return err |
| 209 | } |
| 210 | |
| 211 | if entityName == nil { |
| 212 | c.MatchType = QuotaMatchDefault |
| 213 | } else { |
| 214 | c.MatchType = QuotaMatchExact |
| 215 | c.Name = *entityName |
| 216 | } |
| 217 | |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | func (d *DescribeClientQuotasResponse) key() int16 { |
| 222 | return 48 |
| 223 | } |
| 224 | |
| 225 | func (d *DescribeClientQuotasResponse) version() int16 { |
| 226 | return 0 |
| 227 | } |
| 228 | |
| 229 | func (d *DescribeClientQuotasResponse) headerVersion() int16 { |
| 230 | return 0 |
| 231 | } |
| 232 | |
| 233 | func (d *DescribeClientQuotasResponse) requiredVersion() KafkaVersion { |
| 234 | return V2_6_0_0 |
| 235 | } |