offset_request.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package sarama
  2. type OffsetRequestBlock struct {
  3. Time int64
  4. MaxOffsets int32
  5. }
  6. func (r *OffsetRequestBlock) encode(pe packetEncoder) error {
  7. pe.putInt64(int64(r.Time))
  8. pe.putInt32(r.MaxOffsets)
  9. return nil
  10. }
  11. func (r *OffsetRequestBlock) decode(pd packetDecoder) (err error) {
  12. if r.Time, err = pd.getInt64(); err != nil {
  13. return err
  14. }
  15. if r.MaxOffsets, err = pd.getInt32(); err != nil {
  16. return err
  17. }
  18. return nil
  19. }
  20. type OffsetRequest struct {
  21. Blocks map[string]map[int32]*OffsetRequestBlock
  22. }
  23. func (r *OffsetRequest) encode(pe packetEncoder) error {
  24. pe.putInt32(-1) // replica ID is always -1 for clients
  25. err := pe.putArrayLength(len(r.Blocks))
  26. if err != nil {
  27. return err
  28. }
  29. for topic, partitions := range r.Blocks {
  30. err = pe.putString(topic)
  31. if err != nil {
  32. return err
  33. }
  34. err = pe.putArrayLength(len(partitions))
  35. if err != nil {
  36. return err
  37. }
  38. for partition, block := range partitions {
  39. pe.putInt32(partition)
  40. if err = block.encode(pe); err != nil {
  41. return err
  42. }
  43. }
  44. }
  45. return nil
  46. }
  47. func (r *OffsetRequest) decode(pd packetDecoder) error {
  48. // Ignore replica ID
  49. if _, err := pd.getInt32(); err != nil {
  50. return err
  51. }
  52. blockCount, err := pd.getArrayLength()
  53. if err != nil {
  54. return err
  55. }
  56. if blockCount == 0 {
  57. return nil
  58. }
  59. r.Blocks = make(map[string]map[int32]*OffsetRequestBlock)
  60. for i := 0; i < blockCount; i++ {
  61. topic, err := pd.getString()
  62. if err != nil {
  63. return err
  64. }
  65. partitionCount, err := pd.getArrayLength()
  66. if err != nil {
  67. return err
  68. }
  69. r.Blocks[topic] = make(map[int32]*OffsetRequestBlock)
  70. for j := 0; j < partitionCount; j++ {
  71. partition, err := pd.getInt32()
  72. if err != nil {
  73. return err
  74. }
  75. block := &OffsetRequestBlock{}
  76. if err := block.decode(pd); err != nil {
  77. return err
  78. }
  79. r.Blocks[topic][partition] = block
  80. }
  81. }
  82. return nil
  83. }
  84. func (r *OffsetRequest) key() int16 {
  85. return 2
  86. }
  87. func (r *OffsetRequest) version() int16 {
  88. return 0
  89. }
  90. func (r *OffsetRequest) AddBlock(topic string, partitionID int32, time int64, maxOffsets int32) {
  91. if r.Blocks == nil {
  92. r.Blocks = make(map[string]map[int32]*OffsetRequestBlock)
  93. }
  94. if r.Blocks[topic] == nil {
  95. r.Blocks[topic] = make(map[int32]*OffsetRequestBlock)
  96. }
  97. tmp := new(OffsetRequestBlock)
  98. tmp.Time = time
  99. tmp.MaxOffsets = maxOffsets
  100. r.Blocks[topic][partitionID] = tmp
  101. }