offset_commit_response.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package sarama
  2. import "time"
  3. type OffsetCommitResponse struct {
  4. Version int16
  5. ThrottleTimeMs int32
  6. Errors map[string]map[int32]KError
  7. }
  8. func (r *OffsetCommitResponse) AddError(topic string, partition int32, kerror KError) {
  9. if r.Errors == nil {
  10. r.Errors = make(map[string]map[int32]KError)
  11. }
  12. partitions := r.Errors[topic]
  13. if partitions == nil {
  14. partitions = make(map[int32]KError)
  15. r.Errors[topic] = partitions
  16. }
  17. partitions[partition] = kerror
  18. }
  19. func (r *OffsetCommitResponse) encode(pe packetEncoder) error {
  20. if r.Version >= 3 {
  21. pe.putInt32(r.ThrottleTimeMs)
  22. }
  23. if err := pe.putArrayLength(len(r.Errors)); err != nil {
  24. return err
  25. }
  26. for topic, partitions := range r.Errors {
  27. if err := pe.putString(topic); err != nil {
  28. return err
  29. }
  30. if err := pe.putArrayLength(len(partitions)); err != nil {
  31. return err
  32. }
  33. for partition, kerror := range partitions {
  34. pe.putInt32(partition)
  35. pe.putInt16(int16(kerror))
  36. }
  37. }
  38. return nil
  39. }
  40. func (r *OffsetCommitResponse) decode(pd packetDecoder, version int16) (err error) {
  41. r.Version = version
  42. if version >= 3 {
  43. r.ThrottleTimeMs, err = pd.getInt32()
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. numTopics, err := pd.getArrayLength()
  49. if err != nil || numTopics == 0 {
  50. return err
  51. }
  52. r.Errors = make(map[string]map[int32]KError, numTopics)
  53. for i := 0; i < numTopics; i++ {
  54. name, err := pd.getString()
  55. if err != nil {
  56. return err
  57. }
  58. numErrors, err := pd.getArrayLength()
  59. if err != nil {
  60. return err
  61. }
  62. r.Errors[name] = make(map[int32]KError, numErrors)
  63. for j := 0; j < numErrors; j++ {
  64. id, err := pd.getInt32()
  65. if err != nil {
  66. return err
  67. }
  68. tmp, err := pd.getInt16()
  69. if err != nil {
  70. return err
  71. }
  72. r.Errors[name][id] = KError(tmp)
  73. }
  74. }
  75. return nil
  76. }
  77. func (r *OffsetCommitResponse) key() int16 {
  78. return 8
  79. }
  80. func (r *OffsetCommitResponse) version() int16 {
  81. return r.Version
  82. }
  83. func (r *OffsetCommitResponse) headerVersion() int16 {
  84. return 0
  85. }
  86. func (r *OffsetCommitResponse) isValidVersion() bool {
  87. return r.Version >= 0 && r.Version <= 7
  88. }
  89. func (r *OffsetCommitResponse) requiredVersion() KafkaVersion {
  90. switch r.Version {
  91. case 7:
  92. return V2_3_0_0
  93. case 5, 6:
  94. return V2_1_0_0
  95. case 4:
  96. return V2_0_0_0
  97. case 3:
  98. return V0_11_0_0
  99. case 2:
  100. return V0_9_0_0
  101. case 0, 1:
  102. return V0_8_2_0
  103. default:
  104. return V2_4_0_0
  105. }
  106. }
  107. func (r *OffsetCommitResponse) throttleTime() time.Duration {
  108. return time.Duration(r.ThrottleTimeMs) * time.Millisecond
  109. }