consumer.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package sarama
  2. // Consumer processes Kafka messages from a given topic and partition.
  3. // You MUST call Close() on a consumer to avoid leaks, it will not be garbage-collected automatically when
  4. // it passes out of scope (this is in addition to calling Close on the underlying client, which is still necessary).
  5. type Consumer struct {
  6. client *Client
  7. topic string
  8. partition int32
  9. group string
  10. offset int64
  11. broker *Broker
  12. stopper, done chan bool
  13. messages chan *MessageBlock
  14. errors chan error
  15. }
  16. // NewConsumer creates a new consumer attached to the given client. It will read messages from the given topic and partition, as
  17. // part of the named consumer group.
  18. func NewConsumer(client *Client, topic string, partition int32, group string) (*Consumer, error) {
  19. broker, err := client.leader(topic, partition)
  20. if err != nil {
  21. return nil, err
  22. }
  23. c := new(Consumer)
  24. c.client = client
  25. c.topic = topic
  26. c.partition = partition
  27. c.group = group
  28. // We should really be sending an OffsetFetchRequest, but that doesn't seem to
  29. // work in kafka yet. Hopefully will in beta 2...
  30. c.offset = 0
  31. c.broker = broker
  32. c.stopper = make(chan bool)
  33. c.done = make(chan bool)
  34. c.messages = make(chan *MessageBlock)
  35. c.errors = make(chan error)
  36. go c.fetchMessages()
  37. return c, nil
  38. }
  39. // Errors returns the read channel for any errors that might be returned by the broker.
  40. func (c *Consumer) Errors() <-chan error {
  41. return c.errors
  42. }
  43. // Messages returns the read channel for all messages that will be returned by the broker.
  44. func (c *Consumer) Messages() <-chan *MessageBlock {
  45. return c.messages
  46. }
  47. // Close stops the consumer from fetching messages. It is required to call this function before
  48. // a consumer object passes out of scope, as it will otherwise leak memory. You must call this before
  49. // calling Close on the underlying client.
  50. func (c *Consumer) Close() {
  51. close(c.stopper)
  52. <-c.done
  53. }
  54. // helper function for safely sending an error on the errors channel
  55. // if it returns true, the error was sent (or was nil)
  56. // if it returns false, the stopper channel signaled that your goroutine should return!
  57. func (c *Consumer) sendError(err error) bool {
  58. if err == nil {
  59. return true
  60. }
  61. select {
  62. case <-c.stopper:
  63. close(c.messages)
  64. close(c.errors)
  65. close(c.done)
  66. return false
  67. case c.errors <- err:
  68. return true
  69. }
  70. }
  71. func (c *Consumer) fetchMessages() {
  72. var fetchSize int32 = 1024
  73. for {
  74. request := new(FetchRequest)
  75. request.MinBytes = 1
  76. request.MaxWaitTime = 1000
  77. request.AddBlock(c.topic, c.partition, c.offset, fetchSize)
  78. response, err := c.broker.Fetch(c.client.id, request)
  79. switch {
  80. case err == nil:
  81. break
  82. case err == EncodingError:
  83. if c.sendError(err) {
  84. continue
  85. } else {
  86. return
  87. }
  88. default:
  89. c.client.disconnectBroker(c.broker)
  90. for c.broker = nil; err != nil; c.broker, err = c.client.leader(c.topic, c.partition) {
  91. if !c.sendError(err) {
  92. return
  93. }
  94. }
  95. }
  96. block := response.GetBlock(c.topic, c.partition)
  97. if block == nil {
  98. if c.sendError(IncompleteResponse) {
  99. continue
  100. } else {
  101. return
  102. }
  103. }
  104. switch block.Err {
  105. case NO_ERROR:
  106. break
  107. case UNKNOWN_TOPIC_OR_PARTITION, NOT_LEADER_FOR_PARTITION, LEADER_NOT_AVAILABLE:
  108. err = c.client.refreshTopic(c.topic)
  109. if c.sendError(err) {
  110. for c.broker = nil; err != nil; c.broker, err = c.client.leader(c.topic, c.partition) {
  111. if !c.sendError(err) {
  112. return
  113. }
  114. }
  115. continue
  116. } else {
  117. return
  118. }
  119. default:
  120. if c.sendError(block.Err) {
  121. continue
  122. } else {
  123. return
  124. }
  125. }
  126. if len(block.MsgSet.Messages) == 0 {
  127. // We got no messages. If we got a trailing one then we need to ask for more data.
  128. // Otherwise we just poll again and wait for one to be produced...
  129. if block.MsgSet.PartialTrailingMessage {
  130. fetchSize *= 2
  131. }
  132. select {
  133. case <-c.stopper:
  134. close(c.messages)
  135. close(c.errors)
  136. close(c.done)
  137. return
  138. default:
  139. continue
  140. }
  141. }
  142. for _, msgBlock := range block.MsgSet.Messages {
  143. select {
  144. case <-c.stopper:
  145. close(c.messages)
  146. close(c.errors)
  147. close(c.done)
  148. return
  149. case c.messages <- msgBlock:
  150. c.offset++
  151. }
  152. }
  153. }
  154. }