fetch_request.go 1.4 KB

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