offset_fetch_request.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package sarama
  2. type OffsetFetchRequest struct {
  3. ConsumerGroup string
  4. Version int16
  5. partitions map[string][]int32
  6. }
  7. func (r *OffsetFetchRequest) encode(pe packetEncoder) (err error) {
  8. if r.Version < 0 || r.Version > 1 {
  9. return PacketEncodingError{"invalid or unsupported OffsetFetchRequest version field"}
  10. }
  11. if err = pe.putString(r.ConsumerGroup); err != nil {
  12. return err
  13. }
  14. if err = pe.putArrayLength(len(r.partitions)); err != nil {
  15. return err
  16. }
  17. for topic, partitions := range r.partitions {
  18. if err = pe.putString(topic); err != nil {
  19. return err
  20. }
  21. if err = pe.putInt32Array(partitions); err != nil {
  22. return err
  23. }
  24. }
  25. return nil
  26. }
  27. func (r *OffsetFetchRequest) decode(pd packetDecoder, version int16) (err error) {
  28. r.Version = version
  29. if r.ConsumerGroup, err = pd.getString(); err != nil {
  30. return err
  31. }
  32. partitionCount, err := pd.getArrayLength()
  33. if err != nil {
  34. return err
  35. }
  36. if partitionCount == 0 {
  37. return nil
  38. }
  39. r.partitions = make(map[string][]int32)
  40. for i := 0; i < partitionCount; i++ {
  41. topic, err := pd.getString()
  42. if err != nil {
  43. return err
  44. }
  45. partitions, err := pd.getInt32Array()
  46. if err != nil {
  47. return err
  48. }
  49. r.partitions[topic] = partitions
  50. }
  51. return nil
  52. }
  53. func (r *OffsetFetchRequest) key() int16 {
  54. return 9
  55. }
  56. func (r *OffsetFetchRequest) version() int16 {
  57. return r.Version
  58. }
  59. func (r *OffsetFetchRequest) requiredVersion() KafkaVersion {
  60. switch r.Version {
  61. case 1:
  62. return V0_8_2_0
  63. default:
  64. return MinVersion
  65. }
  66. }
  67. func (r *OffsetFetchRequest) AddPartition(topic string, partitionID int32) {
  68. if r.partitions == nil {
  69. r.partitions = make(map[string][]int32)
  70. }
  71. r.partitions[topic] = append(r.partitions[topic], partitionID)
  72. }