produce_request.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 of the constants defined here are valid. On broker versions
  4. // prior to 0.8.2.0 any other positive int16 is also valid (the broker will wait for that many
  5. // acknowledgements) but in 0.8.2.0 and later this will raise an exception (it has been replaced
  6. // by setting the `min.isr` value in the brokers configuration).
  7. type RequiredAcks int16
  8. const (
  9. // NoResponse doesn't send any response, the TCP ACK is all you get.
  10. NoResponse RequiredAcks = 0
  11. // WaitForLocal waits for only the local commit to succeed before responding.
  12. WaitForLocal RequiredAcks = 1
  13. // WaitForAll waits for all replicas to commit before responding.
  14. WaitForAll RequiredAcks = -1
  15. )
  16. type ProduceRequest struct {
  17. RequiredAcks RequiredAcks
  18. Timeout int32
  19. msgSets map[string]map[int32]*MessageSet
  20. }
  21. func (p *ProduceRequest) encode(pe packetEncoder) error {
  22. pe.putInt16(int16(p.RequiredAcks))
  23. pe.putInt32(p.Timeout)
  24. err := pe.putArrayLength(len(p.msgSets))
  25. if err != nil {
  26. return err
  27. }
  28. for topic, partitions := range p.msgSets {
  29. err = pe.putString(topic)
  30. if err != nil {
  31. return err
  32. }
  33. err = pe.putArrayLength(len(partitions))
  34. if err != nil {
  35. return err
  36. }
  37. for id, msgSet := range partitions {
  38. pe.putInt32(id)
  39. pe.push(&lengthField{})
  40. err = msgSet.encode(pe)
  41. if err != nil {
  42. return err
  43. }
  44. err = pe.pop()
  45. if err != nil {
  46. return err
  47. }
  48. }
  49. }
  50. return nil
  51. }
  52. func (p *ProduceRequest) key() int16 {
  53. return 0
  54. }
  55. func (p *ProduceRequest) version() int16 {
  56. return 0
  57. }
  58. func (p *ProduceRequest) AddMessage(topic string, partition int32, msg *Message) {
  59. if p.msgSets == nil {
  60. p.msgSets = make(map[string]map[int32]*MessageSet)
  61. }
  62. if p.msgSets[topic] == nil {
  63. p.msgSets[topic] = make(map[int32]*MessageSet)
  64. }
  65. set := p.msgSets[topic][partition]
  66. if set == nil {
  67. set = new(MessageSet)
  68. p.msgSets[topic][partition] = set
  69. }
  70. set.addMessage(msg)
  71. }
  72. func (p *ProduceRequest) AddSet(topic string, partition int32, set *MessageSet) {
  73. if p.msgSets == nil {
  74. p.msgSets = make(map[string]map[int32]*MessageSet)
  75. }
  76. if p.msgSets[topic] == nil {
  77. p.msgSets[topic] = make(map[int32]*MessageSet)
  78. }
  79. p.msgSets[topic][partition] = set
  80. }