api_versions_response.go 3.5 KB

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