Skip to content

Commit 409c72a

Browse files
author
albertteoh
committed
Simplify
Signed-off-by: albertteoh <[email protected]>
1 parent 5adfd9d commit 409c72a

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed

Diff for: cmd/ingester/app/consumer/consumer.go

+5-31
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ type Consumer struct {
5151
partitionsHeld int64
5252
partitionsHeldGauge metrics.Gauge
5353

54-
messagesDoneChan chan string
55-
errorsDoneChan chan string
56-
doneWg sync.WaitGroup
54+
doneWg sync.WaitGroup
5755
}
5856

5957
type consumerState struct {
@@ -71,8 +69,6 @@ func New(params Params) (*Consumer, error) {
7169
deadlockDetector: deadlockDetector,
7270
partitionIDToState: make(map[int32]*consumerState),
7371
partitionsHeldGauge: partitionsHeldGauge(params.MetricsFactory),
74-
messagesDoneChan: make(chan string),
75-
errorsDoneChan: make(chan string),
7672
}, nil
7773
}
7874

@@ -92,20 +88,6 @@ func (c *Consumer) Start() {
9288
go c.handleErrors(pc.Partition(), pc.Errors())
9389
}
9490
}()
95-
96-
// Expect to receive message and error handler "done" signals from each partition.
97-
go waitForDoneSignals(c.messagesDoneChan, &c.doneWg, c.logger)
98-
go waitForDoneSignals(c.errorsDoneChan, &c.doneWg, c.logger)
99-
}
100-
101-
// waitForDoneSignals watches the doneChan for incoming "done" messages. If a message is received,
102-
// the doneWg WaitGroup is decremented via a call to Done().
103-
func waitForDoneSignals(doneChan <-chan string, doneWg *sync.WaitGroup, logger *zap.Logger) {
104-
logger.Debug("Waiting for done signals")
105-
for v := range doneChan {
106-
logger.Debug("Received done signal", zap.String("msg", v))
107-
doneWg.Done()
108-
}
10991
}
11092

11193
// Close closes the Consumer and underlying sarama consumer
@@ -120,15 +102,10 @@ func (c *Consumer) Close() error {
120102
c.logger.Debug("Waiting for messages and errors to be handled")
121103
c.doneWg.Wait()
122104

123-
c.logger.Debug("Closing message and error done channels")
124-
close(c.messagesDoneChan)
125-
close(c.errorsDoneChan)
126-
127105
return err
128106
}
129107

130-
// handleMessages handles incoming Kafka messages on a channel. Upon the closure of the message channel,
131-
// handleMessages will signal the messagesDoneChan to indicate the graceful shutdown of message handling is done.
108+
// handleMessages handles incoming Kafka messages on a channel
132109
func (c *Consumer) handleMessages(pc sc.PartitionConsumer) {
133110
c.logger.Info("Starting message handler", zap.Int32("partition", pc.Partition()))
134111
c.partitionMapLock.Lock()
@@ -141,7 +118,7 @@ func (c *Consumer) handleMessages(pc sc.PartitionConsumer) {
141118
c.partitionsHeld--
142119
c.partitionsHeldGauge.Update(c.partitionsHeld)
143120
c.partitionMapLock.Unlock()
144-
c.messagesDoneChan <- "HandleMessages done"
121+
c.doneWg.Done()
145122
}()
146123

147124
msgMetrics := c.newMsgMetrics(pc.Partition())
@@ -185,13 +162,10 @@ func (c *Consumer) closePartition(partitionConsumer sc.PartitionConsumer) {
185162
c.logger.Info("Closed partition consumer", zap.Int32("partition", partitionConsumer.Partition()))
186163
}
187164

188-
// handleErrors handles incoming Kafka consumer errors on a channel. Upon the closure of the error channel,
189-
// handleErrors will signal the errorsDoneChan to indicate the graceful shutdown of error handling is done.
165+
// handleErrors handles incoming Kafka consumer errors on a channel
190166
func (c *Consumer) handleErrors(partition int32, errChan <-chan *sarama.ConsumerError) {
191167
c.logger.Info("Starting error handler", zap.Int32("partition", partition))
192-
defer func() {
193-
c.errorsDoneChan <- "HandleErrors done"
194-
}()
168+
defer c.doneWg.Done()
195169

196170
errMetrics := c.newErrMetrics(partition)
197171
for err := range errChan {

0 commit comments

Comments
 (0)