describe_client_quotas_response.go 5.2 KB

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