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

producer: make Flush.Frequency behaviour better #389

Merged
merged 2 commits into from
Mar 24, 2015
Merged
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
25 changes: 11 additions & 14 deletions async_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,13 @@ func (p *asyncProducer) leaderDispatcher(topic string, partition int32, input ch
// groups messages together into appropriately-sized batches for sending to the broker
// based on https://godoc.org/github.com/eapache/channels#BatchingChannel
func (p *asyncProducer) messageAggregator(broker *Broker, input chan *ProducerMessage) {
var ticker *time.Ticker
var timer <-chan time.Time
if p.conf.Producer.Flush.Frequency > 0 {
ticker = time.NewTicker(p.conf.Producer.Flush.Frequency)
timer = ticker.C
}

var buffer []*ProducerMessage
var doFlush chan []*ProducerMessage
var bytesAccumulated int
var defaultFlush bool
var (
timer <-chan time.Time
buffer []*ProducerMessage
doFlush chan []*ProducerMessage
Copy link
Contributor

Choose a reason for hiding this comment

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

flushQueue ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

flushTriggered?

Copy link
Contributor

Choose a reason for hiding this comment

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

triggerFlush! :P

bytesAccumulated int
defaultFlush bool
)

if p.conf.Producer.Flush.Frequency == 0 && p.conf.Producer.Flush.Bytes == 0 && p.conf.Producer.Flush.Messages == 0 {
defaultFlush = true
Expand All @@ -454,6 +450,7 @@ func (p *asyncProducer) messageAggregator(broker *Broker, input chan *ProducerMe
flusher <- buffer
buffer = nil
doFlush = nil
timer = nil
bytesAccumulated = 0
}

Expand All @@ -465,20 +462,20 @@ func (p *asyncProducer) messageAggregator(broker *Broker, input chan *ProducerMe
(p.conf.Producer.Flush.Messages > 0 && len(buffer) >= p.conf.Producer.Flush.Messages) ||
(p.conf.Producer.Flush.Bytes > 0 && bytesAccumulated >= p.conf.Producer.Flush.Bytes) {
doFlush = flusher
} else if p.conf.Producer.Flush.Frequency > 0 && timer == nil {
timer = time.After(p.conf.Producer.Flush.Frequency)
}
case <-timer:
doFlush = flusher
case doFlush <- buffer:
buffer = nil
doFlush = nil
timer = nil
bytesAccumulated = 0
}
}

shutdown:
if ticker != nil {
ticker.Stop()
}
if len(buffer) > 0 {
flusher <- buffer
}
Expand Down