produce_request.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package sarama
  2. // RequiredAcks is used in Produce Requests to tell the broker how many replica acknowledgements
  3. // it must see before responding. Any positive int16 value is valid, or the constants defined here.
  4. type RequiredAcks int16
  5. const (
  6. NO_RESPONSE RequiredAcks = 0 // Don't send any response, the TCP ACK is all you get.
  7. WAIT_FOR_LOCAL RequiredAcks = 1 // Wait for only the local commit to succeed before responding.
  8. WAIT_FOR_ALL RequiredAcks = -1 // Wait for all replicas to commit before responding.
  9. )
  10. type ProduceRequest struct {
  11. RequiredAcks RequiredAcks
  12. Timeout int32
  13. msgSets map[string]map[int32]*MessageSet
  14. }
  15. func (p *ProduceRequest) encode(pe packetEncoder) error {
  16. pe.putInt16(int16(p.RequiredAcks))
  17. pe.putInt32(p.Timeout)
  18. err := pe.putArrayLength(len(p.msgSets))
  19. if err != nil {
  20. return err
  21. }
  22. for topic, partitions := range p.msgSets {
  23. err = pe.putString(topic)
  24. if err != nil {
  25. return err
  26. }
  27. err = pe.putArrayLength(len(partitions))
  28. if err != nil {
  29. return err
  30. }
  31. for id, msgSet := range partitions {
  32. pe.putInt32(id)
  33. pe.push(&lengthField{})
  34. err = msgSet.encode(pe)
  35. if err != nil {
  36. return err
  37. }
  38. err = pe.pop()
  39. if err != nil {
  40. return err
  41. }
  42. }
  43. }
  44. return nil
  45. }
  46. func (p *ProduceRequest) key() int16 {
  47. return 0
  48. }
  49. func (p *ProduceRequest) version() int16 {
  50. return 0
  51. }
  52. func (p *ProduceRequest) AddMessage(topic string, partition int32, msg *Message) {
  53. if p.msgSets == nil {
  54. p.msgSets = make(map[string]map[int32]*MessageSet)
  55. }
  56. if p.msgSets[topic] == nil {
  57. p.msgSets[topic] = make(map[int32]*MessageSet)
  58. }
  59. set := p.msgSets[topic][partition]
  60. if set == nil {
  61. set = new(MessageSet)
  62. p.msgSets[topic][partition] = set
  63. }
  64. set.addMessage(msg)
  65. }