produce_request.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package protocol
  2. // Special values accepted by Kafka for the ResponseCondition member of produce requests.
  3. const (
  4. NO_RESPONSE int16 = 0 // Don't send any response, the TCP ACK is all you get.
  5. WAIT_FOR_LOCAL int16 = 1 // Wait for only the local commit to succeed before responding.
  6. WAIT_FOR_ALL int16 = -1 // Wait for all replicas to commit before responding.
  7. )
  8. type ProduceRequest struct {
  9. ResponseCondition int16
  10. Timeout int32
  11. msgSets map[string]map[int32]*MessageSet
  12. }
  13. func (p *ProduceRequest) encode(pe packetEncoder) {
  14. pe.putInt16(p.ResponseCondition)
  15. pe.putInt32(p.Timeout)
  16. pe.putArrayCount(len(p.msgSets))
  17. for topic, partitions := range p.msgSets {
  18. pe.putString(topic)
  19. pe.putArrayCount(len(partitions))
  20. for id, msgSet := range partitions {
  21. pe.putInt32(id)
  22. msgSet.encode(pe)
  23. }
  24. }
  25. }
  26. func (p *ProduceRequest) key() int16 {
  27. return 0
  28. }
  29. func (p *ProduceRequest) version() int16 {
  30. return 0
  31. }
  32. func (p *ProduceRequest) AddMessage(topic string, partition int32, msg *Message) {
  33. if p.msgSets == nil {
  34. p.msgSets = make(map[string]map[int32]*MessageSet)
  35. }
  36. if p.msgSets[topic] == nil {
  37. p.msgSets[topic] = make(map[int32]*MessageSet)
  38. }
  39. set := p.msgSets[topic][partition]
  40. if set == nil {
  41. set = newMessageSet()
  42. p.msgSets[topic][partition] = set
  43. }
  44. set.addMessage(msg)
  45. }