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] retry producer creation upon error after succssful topic lookup #1139

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
44 changes: 43 additions & 1 deletion pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,49 @@ func newPartitionProducer(client *client, topic string, options *ProducerOptions
} else {
p.userProvidedProducerName = false
}
err := p.grabCnx()
// retry to create producer when failed with maxRetry
var maxRetry int
if p.options.MaxReconnectToBroker == nil {
maxRetry = -1
} else {
maxRetry = int(*p.options.MaxReconnectToBroker)
}

var delayReconnectTime time.Duration
defaultBackoff := internal.DefaultBackoff{}

var err error
for maxRetry != 0 {
if p.options.BackoffPolicy == nil {
delayReconnectTime = defaultBackoff.Next()
} else {
delayReconnectTime = p.options.BackoffPolicy.Next()
}

atomic.AddUint64(&p.epoch, 1)
err = p.grabCnx()
if err == nil {
break
}
p.log.WithError(err).Error("Failed to create producer at newPartitionProducer")
if errors.Is(err, ErrTopicNotfound) {
// when topic is not found, do not attempt to reconnect
p.log.Warn("Failed to create producer due to Topic Not Found")
break
}

if errors.Is(err, ErrTopicTerminated) {
p.log.Info("Topic was terminated, failing pending messages, will not create producer")
break
}

if maxRetry > 0 {
maxRetry--
}
logger.WithError(err).
Error("Failed to create producer at newPartitionProducer retry to create producer")
time.Sleep(delayReconnectTime)
}
if err != nil {
p.batchFlushTicker.Stop()
logger.WithError(err).Error("Failed to create producer at newPartitionProducer")
Expand Down
Loading