offset_request.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. type OffsetRequest struct {
  12. blocks map[string]map[int32]*offsetRequestBlock
  13. }
  14. func (r *OffsetRequest) encode(pe packetEncoder) error {
  15. pe.putInt32(-1) // replica ID is always -1 for clients
  16. err := pe.putArrayLength(len(r.blocks))
  17. if err != nil {
  18. return err
  19. }
  20. for topic, partitions := range r.blocks {
  21. err = pe.putString(topic)
  22. if err != nil {
  23. return err
  24. }
  25. err = pe.putArrayLength(len(partitions))
  26. if err != nil {
  27. return err
  28. }
  29. for partition, block := range partitions {
  30. pe.putInt32(partition)
  31. err = block.encode(pe)
  32. if err != nil {
  33. return err
  34. }
  35. }
  36. }
  37. return nil
  38. }
  39. func (r *OffsetRequest) key() int16 {
  40. return 2
  41. }
  42. func (r *OffsetRequest) version() int16 {
  43. return 0
  44. }
  45. func (r *OffsetRequest) AddBlock(topic string, partitionID int32, time int64, maxOffsets int32) {
  46. if r.blocks == nil {
  47. r.blocks = make(map[string]map[int32]*offsetRequestBlock)
  48. }
  49. if r.blocks[topic] == nil {
  50. r.blocks[topic] = make(map[int32]*offsetRequestBlock)
  51. }
  52. tmp := new(offsetRequestBlock)
  53. tmp.time = time
  54. tmp.maxOffsets = maxOffsets
  55. r.blocks[topic][partitionID] = tmp
  56. }