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

[Issue 259][pulsar-client-go] fix fail to add batchbuilder #260

Merged
merged 1 commit into from
May 22, 2020
Merged
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
38 changes: 16 additions & 22 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pulsar

import (
"context"
"errors"
"sync"
"sync/atomic"
"time"
Expand All @@ -39,6 +40,8 @@ const (
producerClosed
)

var errFailAddBatch = errors.New("message send failed")

type partitionProducer struct {
state int32
client *client
Expand Down Expand Up @@ -265,35 +268,26 @@ func (p *partitionProducer) internalSend(request *sendRequest) {
sequenceID = internal.GetAndAdd(p.sequenceIDGenerator, 1)
}

if sendAsBatch {
added := p.batchBuilder.Add(smm, sequenceID, msg.Payload, request,
msg.ReplicationClusters, deliverAt)
if !added {
// The current batch is full.. flush it and retry
p.internalFlushCurrentBatch()
added := p.batchBuilder.Add(smm, sequenceID, msg.Payload, request,
msg.ReplicationClusters, deliverAt)
if !added {
// The current batch is full.. flush it and retry
p.internalFlushCurrentBatch()

// after flushing try again to add the current payload
if ok := p.batchBuilder.Add(smm, sequenceID, msg.Payload, request,
msg.ReplicationClusters, deliverAt); !ok {
p.log.WithField("size", len(msg.Payload)).
WithField("sequenceID", sequenceID).
WithField("properties", msg.Properties).
Error("unable to add message to batch")
}
}
} else {
// Send individually
if added := p.batchBuilder.Add(smm, sequenceID, msg.Payload, request,
msg.ReplicationClusters, deliverAt); !added {
// after flushing try again to add the current payload
if ok := p.batchBuilder.Add(smm, sequenceID, msg.Payload, request,
msg.ReplicationClusters, deliverAt); !ok {
p.publishSemaphore.Release()
request.callback(nil, request.msg, errFailAddBatch)
p.log.WithField("size", len(msg.Payload)).
WithField("sequenceID", sequenceID).
WithField("properties", msg.Properties).
Error("unable to send single message")
Error("unable to add message to batch")
return
}
p.internalFlushCurrentBatch()
}

if request.flushImmediately {
if !sendAsBatch || request.flushImmediately {
p.internalFlushCurrentBatch()
}
}
Expand Down