consumer.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package sarama
  2. import (
  3. "errors"
  4. "fmt"
  5. "sync"
  6. "sync/atomic"
  7. "time"
  8. )
  9. // ConsumerMessage encapsulates a Kafka message returned by the consumer.
  10. type ConsumerMessage struct {
  11. Key, Value []byte
  12. Topic string
  13. Partition int32
  14. Offset int64
  15. Timestamp time.Time // only set if kafka is version 0.10+
  16. }
  17. // ConsumerError is what is provided to the user when an error occurs.
  18. // It wraps an error and includes the topic and partition.
  19. type ConsumerError struct {
  20. Topic string
  21. Partition int32
  22. Err error
  23. }
  24. func (ce ConsumerError) Error() string {
  25. return fmt.Sprintf("kafka: error while consuming %s/%d: %s", ce.Topic, ce.Partition, ce.Err)
  26. }
  27. // ConsumerErrors is a type that wraps a batch of errors and implements the Error interface.
  28. // It can be returned from the PartitionConsumer's Close methods to avoid the need to manually drain errors
  29. // when stopping.
  30. type ConsumerErrors []*ConsumerError
  31. func (ce ConsumerErrors) Error() string {
  32. return fmt.Sprintf("kafka: %d errors while consuming", len(ce))
  33. }
  34. // Consumer manages PartitionConsumers which process Kafka messages from brokers. You MUST call Close()
  35. // on a consumer to avoid leaks, it will not be garbage-collected automatically when it passes out of
  36. // scope.
  37. //
  38. // Sarama's Consumer type does not currently support automatic consumer-group rebalancing and offset tracking.
  39. // For Zookeeper-based tracking (Kafka 0.8.2 and earlier), the https://github.com/wvanbergen/kafka library
  40. // builds on Sarama to add this support. For Kafka-based tracking (Kafka 0.9 and later), the
  41. // https://github.com/bsm/sarama-cluster library builds on Sarama to add this support.
  42. type Consumer interface {
  43. // Topics returns the set of available topics as retrieved from the cluster
  44. // metadata. This method is the same as Client.Topics(), and is provided for
  45. // convenience.
  46. Topics() ([]string, error)
  47. // Partitions returns the sorted list of all partition IDs for the given topic.
  48. // This method is the same as Client.Partitions(), and is provided for convenience.
  49. Partitions(topic string) ([]int32, error)
  50. // ConsumePartition creates a PartitionConsumer on the given topic/partition with
  51. // the given offset. It will return an error if this Consumer is already consuming
  52. // on the given topic/partition. Offset can be a literal offset, or OffsetNewest
  53. // or OffsetOldest
  54. ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error)
  55. // Close shuts down the consumer. It must be called after all child
  56. // PartitionConsumers have already been closed.
  57. Close() error
  58. }
  59. type consumer struct {
  60. client Client
  61. conf *Config
  62. ownClient bool
  63. lock sync.Mutex
  64. children map[string]map[int32]*partitionConsumer
  65. brokerConsumers map[*Broker]*brokerConsumer
  66. }
  67. // NewConsumer creates a new consumer using the given broker addresses and configuration.
  68. func NewConsumer(addrs []string, config *Config) (Consumer, error) {
  69. client, err := NewClient(addrs, config)
  70. if err != nil {
  71. return nil, err
  72. }
  73. c, err := NewConsumerFromClient(client)
  74. if err != nil {
  75. return nil, err
  76. }
  77. c.(*consumer).ownClient = true
  78. return c, nil
  79. }
  80. // NewConsumerFromClient creates a new consumer using the given client. It is still
  81. // necessary to call Close() on the underlying client when shutting down this consumer.
  82. func NewConsumerFromClient(client Client) (Consumer, error) {
  83. // Check that we are not dealing with a closed Client before processing any other arguments
  84. if client.Closed() {
  85. return nil, ErrClosedClient
  86. }
  87. c := &consumer{
  88. client: client,
  89. conf: client.Config(),
  90. children: make(map[string]map[int32]*partitionConsumer),
  91. brokerConsumers: make(map[*Broker]*brokerConsumer),
  92. }
  93. return c, nil
  94. }
  95. func (c *consumer) Close() error {
  96. if c.ownClient {
  97. return c.client.Close()
  98. }
  99. return nil
  100. }
  101. func (c *consumer) Topics() ([]string, error) {
  102. return c.client.Topics()
  103. }
  104. func (c *consumer) Partitions(topic string) ([]int32, error) {
  105. return c.client.Partitions(topic)
  106. }
  107. func (c *consumer) ConsumePartition(topic string, partition int32, offset int64) (PartitionConsumer, error) {
  108. child := &partitionConsumer{
  109. consumer: c,
  110. conf: c.conf,
  111. topic: topic,
  112. partition: partition,
  113. messages: make(chan *ConsumerMessage, c.conf.ChannelBufferSize),
  114. errors: make(chan *ConsumerError, c.conf.ChannelBufferSize),
  115. feeder: make(chan *FetchResponse, 1),
  116. trigger: make(chan none, 1),
  117. dying: make(chan none),
  118. fetchSize: c.conf.Consumer.Fetch.Default,
  119. }
  120. if err := child.chooseStartingOffset(offset); err != nil {
  121. return nil, err
  122. }
  123. var leader *Broker
  124. var err error
  125. if leader, err = c.client.Leader(child.topic, child.partition); err != nil {
  126. return nil, err
  127. }
  128. if err := c.addChild(child); err != nil {
  129. return nil, err
  130. }
  131. go withRecover(child.dispatcher)
  132. go withRecover(child.responseFeeder)
  133. child.broker = c.refBrokerConsumer(leader)
  134. child.broker.input <- child
  135. return child, nil
  136. }
  137. func (c *consumer) addChild(child *partitionConsumer) error {
  138. c.lock.Lock()
  139. defer c.lock.Unlock()
  140. topicChildren := c.children[child.topic]
  141. if topicChildren == nil {
  142. topicChildren = make(map[int32]*partitionConsumer)
  143. c.children[child.topic] = topicChildren
  144. }
  145. if topicChildren[child.partition] != nil {
  146. return ConfigurationError("That topic/partition is already being consumed")
  147. }
  148. topicChildren[child.partition] = child
  149. return nil
  150. }
  151. func (c *consumer) removeChild(child *partitionConsumer) {
  152. c.lock.Lock()
  153. defer c.lock.Unlock()
  154. delete(c.children[child.topic], child.partition)
  155. }
  156. func (c *consumer) refBrokerConsumer(broker *Broker) *brokerConsumer {
  157. c.lock.Lock()
  158. defer c.lock.Unlock()
  159. bc := c.brokerConsumers[broker]
  160. if bc == nil {
  161. bc = c.newBrokerConsumer(broker)
  162. c.brokerConsumers[broker] = bc
  163. }
  164. bc.refs++
  165. return bc
  166. }
  167. func (c *consumer) unrefBrokerConsumer(brokerWorker *brokerConsumer) {
  168. c.lock.Lock()
  169. defer c.lock.Unlock()
  170. brokerWorker.refs--
  171. if brokerWorker.refs == 0 {
  172. close(brokerWorker.input)
  173. if c.brokerConsumers[brokerWorker.broker] == brokerWorker {
  174. delete(c.brokerConsumers, brokerWorker.broker)
  175. }
  176. }
  177. }
  178. func (c *consumer) abandonBrokerConsumer(brokerWorker *brokerConsumer) {
  179. c.lock.Lock()
  180. defer c.lock.Unlock()
  181. delete(c.brokerConsumers, brokerWorker.broker)
  182. }
  183. // PartitionConsumer
  184. // PartitionConsumer processes Kafka messages from a given topic and partition. You MUST call Close()
  185. // or AsyncClose() on a PartitionConsumer to avoid leaks, it will not be garbage-collected automatically
  186. // when it passes out of scope.
  187. //
  188. // The simplest way of using a PartitionConsumer is to loop over its Messages channel using a for/range
  189. // loop. The PartitionConsumer will only stop itself in one case: when the offset being consumed is reported
  190. // as out of range by the brokers. In this case you should decide what you want to do (try a different offset,
  191. // notify a human, etc) and handle it appropriately. For all other error cases, it will just keep retrying.
  192. // By default, it logs these errors to sarama.Logger; if you want to be notified directly of all errors, set
  193. // your config's Consumer.Return.Errors to true and read from the Errors channel, using a select statement
  194. // or a separate goroutine. Check out the Consumer examples to see implementations of these different approaches.
  195. type PartitionConsumer interface {
  196. // AsyncClose initiates a shutdown of the PartitionConsumer. This method will
  197. // return immediately, after which you should wait until the 'messages' and
  198. // 'errors' channel are drained. It is required to call this function, or
  199. // Close before a consumer object passes out of scope, as it will otherwise
  200. // leak memory. You must call this before calling Close on the underlying client.
  201. AsyncClose()
  202. // Close stops the PartitionConsumer from fetching messages. It is required to
  203. // call this function (or AsyncClose) before a consumer object passes out of
  204. // scope, as it will otherwise leak memory. You must call this before calling
  205. // Close on the underlying client.
  206. Close() error
  207. // Messages returns the read channel for the messages that are returned by
  208. // the broker.
  209. Messages() <-chan *ConsumerMessage
  210. // Errors returns a read channel of errors that occurred during consuming, if
  211. // enabled. By default, errors are logged and not returned over this channel.
  212. // If you want to implement any custom error handling, set your config's
  213. // Consumer.Return.Errors setting to true, and read from this channel.
  214. Errors() <-chan *ConsumerError
  215. // HighWaterMarkOffset returns the high water mark offset of the partition,
  216. // i.e. the offset that will be used for the next message that will be produced.
  217. // You can use this to determine how far behind the processing is.
  218. HighWaterMarkOffset() int64
  219. }
  220. type partitionConsumer struct {
  221. consumer *consumer
  222. conf *Config
  223. topic string
  224. partition int32
  225. broker *brokerConsumer
  226. messages chan *ConsumerMessage
  227. errors chan *ConsumerError
  228. feeder chan *FetchResponse
  229. trigger, dying chan none
  230. responseResult error
  231. fetchSize int32
  232. offset int64
  233. highWaterMarkOffset int64
  234. }
  235. var errTimedOut = errors.New("timed out feeding messages to the user") // not user-facing
  236. func (child *partitionConsumer) sendError(err error) {
  237. cErr := &ConsumerError{
  238. Topic: child.topic,
  239. Partition: child.partition,
  240. Err: err,
  241. }
  242. if child.conf.Consumer.Return.Errors {
  243. child.errors <- cErr
  244. } else {
  245. Logger.Println(cErr)
  246. }
  247. }
  248. func (child *partitionConsumer) dispatcher() {
  249. for _ = range child.trigger {
  250. select {
  251. case <-child.dying:
  252. close(child.trigger)
  253. case <-time.After(child.conf.Consumer.Retry.Backoff):
  254. if child.broker != nil {
  255. child.consumer.unrefBrokerConsumer(child.broker)
  256. child.broker = nil
  257. }
  258. Logger.Printf("consumer/%s/%d finding new broker\n", child.topic, child.partition)
  259. if err := child.dispatch(); err != nil {
  260. child.sendError(err)
  261. child.trigger <- none{}
  262. }
  263. }
  264. }
  265. if child.broker != nil {
  266. child.consumer.unrefBrokerConsumer(child.broker)
  267. }
  268. child.consumer.removeChild(child)
  269. close(child.feeder)
  270. }
  271. func (child *partitionConsumer) dispatch() error {
  272. if err := child.consumer.client.RefreshMetadata(child.topic); err != nil {
  273. return err
  274. }
  275. var leader *Broker
  276. var err error
  277. if leader, err = child.consumer.client.Leader(child.topic, child.partition); err != nil {
  278. return err
  279. }
  280. child.broker = child.consumer.refBrokerConsumer(leader)
  281. child.broker.input <- child
  282. return nil
  283. }
  284. func (child *partitionConsumer) chooseStartingOffset(offset int64) error {
  285. newestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetNewest)
  286. if err != nil {
  287. return err
  288. }
  289. oldestOffset, err := child.consumer.client.GetOffset(child.topic, child.partition, OffsetOldest)
  290. if err != nil {
  291. return err
  292. }
  293. switch {
  294. case offset == OffsetNewest:
  295. child.offset = newestOffset
  296. case offset == OffsetOldest:
  297. child.offset = oldestOffset
  298. case offset >= oldestOffset && offset <= newestOffset:
  299. child.offset = offset
  300. default:
  301. return ErrOffsetOutOfRange
  302. }
  303. return nil
  304. }
  305. func (child *partitionConsumer) Messages() <-chan *ConsumerMessage {
  306. return child.messages
  307. }
  308. func (child *partitionConsumer) Errors() <-chan *ConsumerError {
  309. return child.errors
  310. }
  311. func (child *partitionConsumer) AsyncClose() {
  312. // this triggers whatever broker owns this child to abandon it and close its trigger channel, which causes
  313. // the dispatcher to exit its loop, which removes it from the consumer then closes its 'messages' and
  314. // 'errors' channel (alternatively, if the child is already at the dispatcher for some reason, that will
  315. // also just close itself)
  316. close(child.dying)
  317. }
  318. func (child *partitionConsumer) Close() error {
  319. child.AsyncClose()
  320. go withRecover(func() {
  321. for _ = range child.messages {
  322. // drain
  323. }
  324. })
  325. var errors ConsumerErrors
  326. for err := range child.errors {
  327. errors = append(errors, err)
  328. }
  329. if len(errors) > 0 {
  330. return errors
  331. }
  332. return nil
  333. }
  334. func (child *partitionConsumer) HighWaterMarkOffset() int64 {
  335. return atomic.LoadInt64(&child.highWaterMarkOffset)
  336. }
  337. func (child *partitionConsumer) responseFeeder() {
  338. var msgs []*ConsumerMessage
  339. expiryTimer := time.NewTimer(child.conf.Consumer.MaxProcessingTime)
  340. expireTimedOut := false
  341. feederLoop:
  342. for response := range child.feeder {
  343. msgs, child.responseResult = child.parseResponse(response)
  344. for i, msg := range msgs {
  345. if !expiryTimer.Stop() && !expireTimedOut {
  346. // expiryTimer was expired; clear out the waiting msg
  347. <-expiryTimer.C
  348. }
  349. expiryTimer.Reset(child.conf.Consumer.MaxProcessingTime)
  350. expireTimedOut = false
  351. select {
  352. case child.messages <- msg:
  353. case <-expiryTimer.C:
  354. expireTimedOut = true
  355. child.responseResult = errTimedOut
  356. child.broker.acks.Done()
  357. for _, msg = range msgs[i:] {
  358. child.messages <- msg
  359. }
  360. child.broker.input <- child
  361. continue feederLoop
  362. }
  363. }
  364. child.broker.acks.Done()
  365. }
  366. close(child.messages)
  367. close(child.errors)
  368. }
  369. func (child *partitionConsumer) parseResponse(response *FetchResponse) ([]*ConsumerMessage, error) {
  370. block := response.GetBlock(child.topic, child.partition)
  371. if block == nil {
  372. return nil, ErrIncompleteResponse
  373. }
  374. if block.Err != ErrNoError {
  375. return nil, block.Err
  376. }
  377. if len(block.MsgSet.Messages) == 0 {
  378. // We got no messages. If we got a trailing one then we need to ask for more data.
  379. // Otherwise we just poll again and wait for one to be produced...
  380. if block.MsgSet.PartialTrailingMessage {
  381. if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize == child.conf.Consumer.Fetch.Max {
  382. // we can't ask for more data, we've hit the configured limit
  383. child.sendError(ErrMessageTooLarge)
  384. child.offset++ // skip this one so we can keep processing future messages
  385. } else {
  386. child.fetchSize *= 2
  387. if child.conf.Consumer.Fetch.Max > 0 && child.fetchSize > child.conf.Consumer.Fetch.Max {
  388. child.fetchSize = child.conf.Consumer.Fetch.Max
  389. }
  390. }
  391. }
  392. return nil, nil
  393. }
  394. // we got messages, reset our fetch size in case it was increased for a previous request
  395. child.fetchSize = child.conf.Consumer.Fetch.Default
  396. atomic.StoreInt64(&child.highWaterMarkOffset, block.HighWaterMarkOffset)
  397. incomplete := false
  398. prelude := true
  399. var messages []*ConsumerMessage
  400. for _, msgBlock := range block.MsgSet.Messages {
  401. for _, msg := range msgBlock.Messages() {
  402. offset := msg.Offset
  403. if msg.Msg.Version >= 1 {
  404. baseOffset := msgBlock.Offset - msgBlock.Messages()[len(msgBlock.Messages())-1].Offset
  405. offset += baseOffset
  406. }
  407. if prelude && offset < child.offset {
  408. continue
  409. }
  410. prelude = false
  411. if offset >= child.offset {
  412. messages = append(messages, &ConsumerMessage{
  413. Topic: child.topic,
  414. Partition: child.partition,
  415. Key: msg.Msg.Key,
  416. Value: msg.Msg.Value,
  417. Offset: offset,
  418. Timestamp: msg.Msg.Timestamp,
  419. })
  420. child.offset = offset + 1
  421. } else {
  422. incomplete = true
  423. }
  424. }
  425. }
  426. if incomplete || len(messages) == 0 {
  427. return nil, ErrIncompleteResponse
  428. }
  429. return messages, nil
  430. }
  431. // brokerConsumer
  432. type brokerConsumer struct {
  433. consumer *consumer
  434. broker *Broker
  435. input chan *partitionConsumer
  436. newSubscriptions chan []*partitionConsumer
  437. wait chan none
  438. subscriptions map[*partitionConsumer]none
  439. acks sync.WaitGroup
  440. refs int
  441. }
  442. func (c *consumer) newBrokerConsumer(broker *Broker) *brokerConsumer {
  443. bc := &brokerConsumer{
  444. consumer: c,
  445. broker: broker,
  446. input: make(chan *partitionConsumer),
  447. newSubscriptions: make(chan []*partitionConsumer),
  448. wait: make(chan none),
  449. subscriptions: make(map[*partitionConsumer]none),
  450. refs: 0,
  451. }
  452. go withRecover(bc.subscriptionManager)
  453. go withRecover(bc.subscriptionConsumer)
  454. return bc
  455. }
  456. func (bc *brokerConsumer) subscriptionManager() {
  457. var buffer []*partitionConsumer
  458. // The subscriptionManager constantly accepts new subscriptions on `input` (even when the main subscriptionConsumer
  459. // goroutine is in the middle of a network request) and batches it up. The main worker goroutine picks
  460. // up a batch of new subscriptions between every network request by reading from `newSubscriptions`, so we give
  461. // it nil if no new subscriptions are available. We also write to `wait` only when new subscriptions is available,
  462. // so the main goroutine can block waiting for work if it has none.
  463. for {
  464. if len(buffer) > 0 {
  465. select {
  466. case event, ok := <-bc.input:
  467. if !ok {
  468. goto done
  469. }
  470. buffer = append(buffer, event)
  471. case bc.newSubscriptions <- buffer:
  472. buffer = nil
  473. case bc.wait <- none{}:
  474. }
  475. } else {
  476. select {
  477. case event, ok := <-bc.input:
  478. if !ok {
  479. goto done
  480. }
  481. buffer = append(buffer, event)
  482. case bc.newSubscriptions <- nil:
  483. }
  484. }
  485. }
  486. done:
  487. close(bc.wait)
  488. if len(buffer) > 0 {
  489. bc.newSubscriptions <- buffer
  490. }
  491. close(bc.newSubscriptions)
  492. }
  493. func (bc *brokerConsumer) subscriptionConsumer() {
  494. <-bc.wait // wait for our first piece of work
  495. // the subscriptionConsumer ensures we will get nil right away if no new subscriptions is available
  496. for newSubscriptions := range bc.newSubscriptions {
  497. bc.updateSubscriptions(newSubscriptions)
  498. if len(bc.subscriptions) == 0 {
  499. // We're about to be shut down or we're about to receive more subscriptions.
  500. // Either way, the signal just hasn't propagated to our goroutine yet.
  501. <-bc.wait
  502. continue
  503. }
  504. response, err := bc.fetchNewMessages()
  505. if err != nil {
  506. Logger.Printf("consumer/broker/%d disconnecting due to error processing FetchRequest: %s\n", bc.broker.ID(), err)
  507. bc.abort(err)
  508. return
  509. }
  510. bc.acks.Add(len(bc.subscriptions))
  511. for child := range bc.subscriptions {
  512. child.feeder <- response
  513. }
  514. bc.acks.Wait()
  515. bc.handleResponses()
  516. }
  517. }
  518. func (bc *brokerConsumer) updateSubscriptions(newSubscriptions []*partitionConsumer) {
  519. for _, child := range newSubscriptions {
  520. bc.subscriptions[child] = none{}
  521. Logger.Printf("consumer/broker/%d added subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
  522. }
  523. for child := range bc.subscriptions {
  524. select {
  525. case <-child.dying:
  526. Logger.Printf("consumer/broker/%d closed dead subscription to %s/%d\n", bc.broker.ID(), child.topic, child.partition)
  527. close(child.trigger)
  528. delete(bc.subscriptions, child)
  529. default:
  530. break
  531. }
  532. }
  533. }
  534. func (bc *brokerConsumer) handleResponses() {
  535. // handles the response codes left for us by our subscriptions, and abandons ones that have been closed
  536. for child := range bc.subscriptions {
  537. result := child.responseResult
  538. child.responseResult = nil
  539. switch result {
  540. case nil:
  541. break
  542. case errTimedOut:
  543. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because consuming was taking too long\n",
  544. bc.broker.ID(), child.topic, child.partition)
  545. delete(bc.subscriptions, child)
  546. case ErrOffsetOutOfRange:
  547. // there's no point in retrying this it will just fail the same way again
  548. // shut it down and force the user to choose what to do
  549. child.sendError(result)
  550. Logger.Printf("consumer/%s/%d shutting down because %s\n", child.topic, child.partition, result)
  551. close(child.trigger)
  552. delete(bc.subscriptions, child)
  553. case ErrUnknownTopicOrPartition, ErrNotLeaderForPartition, ErrLeaderNotAvailable, ErrReplicaNotAvailable:
  554. // not an error, but does need redispatching
  555. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
  556. bc.broker.ID(), child.topic, child.partition, result)
  557. child.trigger <- none{}
  558. delete(bc.subscriptions, child)
  559. default:
  560. // dunno, tell the user and try redispatching
  561. child.sendError(result)
  562. Logger.Printf("consumer/broker/%d abandoned subscription to %s/%d because %s\n",
  563. bc.broker.ID(), child.topic, child.partition, result)
  564. child.trigger <- none{}
  565. delete(bc.subscriptions, child)
  566. }
  567. }
  568. }
  569. func (bc *brokerConsumer) abort(err error) {
  570. bc.consumer.abandonBrokerConsumer(bc)
  571. _ = bc.broker.Close() // we don't care about the error this might return, we already have one
  572. for child := range bc.subscriptions {
  573. child.sendError(err)
  574. child.trigger <- none{}
  575. }
  576. for newSubscriptions := range bc.newSubscriptions {
  577. if len(newSubscriptions) == 0 {
  578. <-bc.wait
  579. continue
  580. }
  581. for _, child := range newSubscriptions {
  582. child.sendError(err)
  583. child.trigger <- none{}
  584. }
  585. }
  586. }
  587. func (bc *brokerConsumer) fetchNewMessages() (*FetchResponse, error) {
  588. request := &FetchRequest{
  589. MinBytes: bc.consumer.conf.Consumer.Fetch.Min,
  590. MaxWaitTime: int32(bc.consumer.conf.Consumer.MaxWaitTime / time.Millisecond),
  591. }
  592. if bc.consumer.conf.Version.IsAtLeast(V0_10_0_0) {
  593. request.Version = 2
  594. }
  595. for child := range bc.subscriptions {
  596. request.AddBlock(child.topic, child.partition, child.offset, child.fetchSize)
  597. }
  598. return bc.broker.Fetch(request)
  599. }