Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions cmd/pulsar-consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,24 @@ func newConsumer(ctx context.Context, option *option) *consumer {
}

func (c *consumer) readMessage(ctx context.Context) error {
msgChan := c.pulsarConsumer.Chan()
defer func() {
c.pulsarConsumer.Close()
c.client.Close()
}()
for {
select {
case <-ctx.Done():
log.Info("terminating: context cancelled")
return errors.Trace(ctx.Err())
case consumerMsg := <-msgChan:
log.Debug("Received message", zap.Stringer("msgId", consumerMsg.ID()), zap.ByteString("content", consumerMsg.Payload()))
needCommit := c.writer.WriteMessage(ctx, consumerMsg)
if !needCommit {
continue
}
err := c.pulsarConsumer.AckID(consumerMsg.Message.ID())
if err != nil {
log.Panic("Error ack message", zap.Error(err))
}
consumerMsg, err := c.pulsarConsumer.Receive(ctx)
if err != nil {
log.Error("Receive message error", zap.Error(err))
return errors.Trace(err)
}
Comment on lines +108 to +111
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation logs an error when the context is cancelled (e.g., during a graceful shutdown), as pulsarConsumer.Receive(ctx) returns context.Canceled. This is misleading because context cancellation is an expected way to stop the consumer. It's better to check for this specific error and log it at the Info level, consistent with the previous logic.

if err != nil {
			if errors.Cause(err) == context.Canceled {
				log.Info("terminating: context cancelled")
				return errors.Trace(err)
			}
			log.Error("Receive message error", zap.Error(err))
			return errors.Trace(err)
		}

log.Debug("Received message", zap.Stringer("msgId", consumerMsg.ID()), zap.ByteString("content", consumerMsg.Payload()))
needCommit := c.writer.WriteMessage(ctx, consumerMsg)
if !needCommit {
continue
}
err = c.pulsarConsumer.AckID(consumerMsg.ID())
if err != nil {
log.Panic("Error ack message", zap.Error(err))
}
}
}
Expand Down
Loading