sync_producer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package sarama
  2. import "sync"
  3. // SyncProducer publishes Kafka messages, blocking until they have been acknowledged. It routes messages to the correct
  4. // broker, refreshing metadata as appropriate, and parses responses for errors. You must call Close() on a producer
  5. // to avoid leaks, it may not be garbage-collected automatically when it passes out of scope.
  6. //
  7. // The SyncProducer comes with two caveats: it will generally be less efficient than the AsyncProducer, and the actual
  8. // durability guarantee provided when a message is acknowledged depend on the configured value of `Producer.RequiredAcks`.
  9. // There are configurations where a message acknowledged by the SyncProducer can still sometimes be lost.
  10. //
  11. // For implementation reasons, the SyncProducer requires `Producer.Return.Errors` and `Producer.Return.Successes` to
  12. // be set to true in its configuration.
  13. type SyncProducer interface {
  14. // SendMessage produces a given message, and returns only when it either has
  15. // succeeded or failed to produce. It will return the partition and the offset
  16. // of the produced message, or an error if the message failed to produce.
  17. SendMessage(msg *ProducerMessage) (partition int32, offset int64, err error)
  18. // SendMessages produces a given set of messages, and returns only when all
  19. // messages in the set have either succeeded or failed. Note that messages
  20. // can succeed and fail individually; if some succeed and some fail,
  21. // SendMessages will return an error.
  22. SendMessages(msgs []*ProducerMessage) error
  23. // Close shuts down the producer; you must call this function before a producer
  24. // object passes out of scope, as it may otherwise leak memory.
  25. // You must call this before calling Close on the underlying client.
  26. Close() error
  27. }
  28. type syncProducer struct {
  29. producer *asyncProducer
  30. wg sync.WaitGroup
  31. }
  32. // NewSyncProducer creates a new SyncProducer using the given broker addresses and configuration.
  33. func NewSyncProducer(addrs []string, config *Config) (SyncProducer, error) {
  34. if config == nil {
  35. config = NewConfig()
  36. config.Producer.Return.Successes = true
  37. }
  38. if err := verifyProducerConfig(config); err != nil {
  39. return nil, err
  40. }
  41. p, err := NewAsyncProducer(addrs, config)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return newSyncProducerFromAsyncProducer(p.(*asyncProducer)), nil
  46. }
  47. // NewSyncProducerFromClient creates a new SyncProducer using the given client. It is still
  48. // necessary to call Close() on the underlying client when shutting down this producer.
  49. func NewSyncProducerFromClient(client Client) (SyncProducer, error) {
  50. if err := verifyProducerConfig(client.Config()); err != nil {
  51. return nil, err
  52. }
  53. p, err := NewAsyncProducerFromClient(client)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return newSyncProducerFromAsyncProducer(p.(*asyncProducer)), nil
  58. }
  59. func newSyncProducerFromAsyncProducer(p *asyncProducer) *syncProducer {
  60. sp := &syncProducer{producer: p}
  61. sp.wg.Add(2)
  62. go withRecover(sp.handleSuccesses)
  63. go withRecover(sp.handleErrors)
  64. return sp
  65. }
  66. func verifyProducerConfig(config *Config) error {
  67. if !config.Producer.Return.Errors {
  68. return ConfigurationError("Producer.Return.Errors must be true to be used in a SyncProducer")
  69. }
  70. if !config.Producer.Return.Successes {
  71. return ConfigurationError("Producer.Return.Successes must be true to be used in a SyncProducer")
  72. }
  73. return nil
  74. }
  75. func (sp *syncProducer) SendMessage(msg *ProducerMessage) (partition int32, offset int64, err error) {
  76. expectation := make(chan *ProducerError, 1)
  77. msg.expectation = expectation
  78. sp.producer.Input() <- msg
  79. if pErr := <-expectation; pErr != nil {
  80. return -1, -1, pErr.Err
  81. }
  82. return msg.Partition, msg.Offset, nil
  83. }
  84. func (sp *syncProducer) SendMessages(msgs []*ProducerMessage) error {
  85. expectations := make(chan chan *ProducerError, len(msgs))
  86. go func() {
  87. for _, msg := range msgs {
  88. expectation := make(chan *ProducerError, 1)
  89. msg.expectation = expectation
  90. sp.producer.Input() <- msg
  91. expectations <- expectation
  92. }
  93. close(expectations)
  94. }()
  95. var errors ProducerErrors
  96. for expectation := range expectations {
  97. if pErr := <-expectation; pErr != nil {
  98. errors = append(errors, pErr)
  99. }
  100. }
  101. if len(errors) > 0 {
  102. return errors
  103. }
  104. return nil
  105. }
  106. func (sp *syncProducer) handleSuccesses() {
  107. defer sp.wg.Done()
  108. for msg := range sp.producer.Successes() {
  109. expectation := msg.expectation
  110. expectation <- nil
  111. }
  112. }
  113. func (sp *syncProducer) handleErrors() {
  114. defer sp.wg.Done()
  115. for err := range sp.producer.Errors() {
  116. expectation := err.Msg.expectation
  117. expectation <- err
  118. }
  119. }
  120. func (sp *syncProducer) Close() error {
  121. sp.producer.AsyncClose()
  122. sp.wg.Wait()
  123. return nil
  124. }