fetch_request.go 2.5 KB

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