/
producer_utils.go
82 lines (75 loc) · 2.78 KB
/
producer_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package kefka
import (
"context"
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
// SyncProduce is a convenient function for producing messages synchronously.
// Technically the Confluent Kafka Producer doesn't support producing events
// synchronously. Instead, this function creates a delivery channel and wait on it
// for the delivery report/acknowledgement.
//
// SyncProduce accepts a context so that this operation can be cancelled or
// timeout. However, it is very important to note this does not cancel producing
// the message. It simply cancels waiting on the delivery report. The message still
// may be delivered.
func SyncProduce(ctx context.Context, p kafkaProducer, topic string, key, value []byte) error {
ch := make(chan kafka.Event, 1)
err := p.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{
Topic: &topic,
Partition: kafka.PartitionAny,
},
Value: value,
Key: key,
}, ch)
if err != nil {
return fmt.Errorf("message could not be enqued: %w", err)
}
select {
case <-ctx.Done():
return fmt.Errorf("aborted waiting for message delivery report: %w", ctx.Err())
case e := <-ch:
m := e.(*kafka.Message)
if m.TopicPartition.Error != nil {
return fmt.Errorf("error publishing/producing message: %w", m.TopicPartition.Error)
}
return nil
}
}
// SyncProduceMessage is a convenient function for producing messages synchronously.
// Technically the Confluent Kafka Producer doesn't support producing events
// synchronously. Instead, this function creates a delivery channel and wait on it
// for the delivery report/acknowledgement.
//
// SyncProduceMessage accepts a context so that this operation can be cancelled or
// timeout. However, it is very important to note this does not cancel producing
// the message. It simply cancels waiting on the delivery report. The message still
// may be delivered.
func SyncProduceMessage(ctx context.Context, p kafkaProducer, msg *kafka.Message) error {
ch := make(chan kafka.Event, 1)
err := p.Produce(msg, ch)
if err != nil {
return fmt.Errorf("message could not be enqued: %w", err)
}
select {
case <-ctx.Done():
return fmt.Errorf("aborted waiting for message delivery report: %w", ctx.Err())
case e := <-ch:
m := e.(*kafka.Message)
if m.TopicPartition.Error != nil {
return fmt.Errorf("error publishing/producing message: %w", m.TopicPartition.Error)
}
return nil
}
}
// FlushAll will continuously call Flush on the Kafka Producer until there are
// zero messages awaiting delivery.
//
// This function is blocking and should really only be called if you need to
// force the internal queue empty. An example might be an application exiting.
func FlushAll(p kafkaProducer) {
for remaining := p.Flush(5000); remaining > 0; {
// keep calling flush until it returns 0 elements remaining
}
}