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

kafka: apply round robin on bach #809

Merged
merged 1 commit into from
Sep 12, 2024
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
34 changes: 17 additions & 17 deletions workers/kafkaproducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type KafkaProducer struct {
kafkaConnected bool
compressCodec compress.Codec
kafkaConns map[int]*kafka.Conn // Map to store connections by partition
lastPartitionIndex *int
}

func NewKafkaProducer(config *pkgconfig.Config, logger *logger.Logger, name string) *KafkaProducer {
Expand Down Expand Up @@ -218,25 +219,24 @@ func (w *KafkaProducer) FlushBuffer(buf *[]dnsutils.DNSMessage) {
// add support for msg compression and round robin
var err error
if partition == nil {
index := 0
if w.lastPartitionIndex == nil {
w.lastPartitionIndex = new(int) // Initialiser l'index la première fois
}
numPartitions := len(w.kafkaConns)
for _, msg := range msgs {
conn := w.kafkaConns[index]
if w.GetConfig().Loggers.KafkaProducer.Compression == pkgconfig.CompressNone {
_, err = conn.WriteMessages(msg)
} else {
_, err = conn.WriteCompressedMessages(w.compressCodec, msg)
}
if err != nil {
w.LogError("unable to write message", err.Error())
w.kafkaConnected = false
<-w.kafkaReconnect
break
}

// Move to the next partition in round-robin fashion
index = (index + 1) % numPartitions
conn := w.kafkaConns[*w.lastPartitionIndex]
if w.GetConfig().Loggers.KafkaProducer.Compression == pkgconfig.CompressNone {
_, err = conn.WriteMessages(msgs...)
} else {
_, err = conn.WriteCompressedMessages(w.compressCodec, msgs...)
}
if err != nil {
w.LogError("unable to write message", err.Error())
w.kafkaConnected = false
<-w.kafkaReconnect
}

// Move to the next partition in round-robin fashion
*w.lastPartitionIndex = (*w.lastPartitionIndex + 1) % numPartitions
} else {
conn := w.kafkaConns[*partition]
if w.GetConfig().Loggers.KafkaProducer.Compression == pkgconfig.CompressNone {
Expand Down
Loading