Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix leaked nack tracker goroutine #418

Merged
merged 2 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions pulsar/consumer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func newPartitionConsumer(parent Consumer, client *client, options *partitionCon
err := pc.grabConn()
if err != nil {
pc.log.WithError(err).Error("Failed to create consumer")
pc.nackTracker.Close()
return nil, err
}
pc.log.Info("Created consumer")
Expand All @@ -167,13 +168,15 @@ func newPartitionConsumer(parent Consumer, client *client, options *partitionCon
if pc.options.startMessageIDInclusive && pc.startMessageID == lastestMessageID {
msgID, err := pc.requestGetLastMessageID()
if err != nil {
pc.nackTracker.Close()
return nil, err
}
if msgID.entryID != noMessageEntry {
pc.startMessageID = msgID

err = pc.requestSeek(msgID.messageID)
if err != nil {
pc.nackTracker.Close()
return nil, err
}
}
Expand Down Expand Up @@ -739,6 +742,10 @@ func (pc *partitionConsumer) runEventsLoop() {
func (pc *partitionConsumer) internalClose(req *closeRequest) {
defer close(req.doneCh)
if pc.state != consumerReady {
// this might be redundant but to ensure nack tracker is closed
if pc.nackTracker != nil {
pc.nackTracker.Close()
}
return
}

Expand Down
12 changes: 9 additions & 3 deletions pulsar/negative_acks_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type negativeAcksTracker struct {
sync.Mutex

doneCh chan interface{}
doneOnce sync.Once
negativeAcks map[messageID]time.Time
rc redeliveryConsumer
tick *time.Ticker
Expand Down Expand Up @@ -84,10 +85,11 @@ func (t *negativeAcksTracker) track() {

case <-t.tick.C:
{
t.Lock()

now := time.Now()
msgIds := make([]messageID, 0)

t.Lock()

for msgID, targetTime := range t.negativeAcks {
t.log.Debugf("MsgId: %v -- targetTime: %v -- now: %v", msgID, targetTime, now)
if targetTime.Before(now) {
Expand All @@ -109,5 +111,9 @@ func (t *negativeAcksTracker) track() {
}

func (t *negativeAcksTracker) Close() {
t.doneCh <- nil
// allow Close() to be invoked multiple times by consumer_partition to avoid panic
t.doneOnce.Do(func() {
t.tick.Stop()
t.doneCh <- nil
})
}