File tree 1 file changed +20
-0
lines changed
1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -19,10 +19,22 @@ type MessageSender interface {
19
19
SendMessages (msgs []* sarama.ProducerMessage ) error
20
20
}
21
21
22
+ type AsyncMessageSender struct {
23
+ sarama.AsyncProducer
24
+ }
25
+
26
+ func (a * AsyncMessageSender ) SendMessages (msgs []* sarama.ProducerMessage ) error {
27
+ for _ , msg := range msgs {
28
+ a .AsyncProducer .Input () <- msg
29
+ }
30
+ return nil
31
+ }
32
+
22
33
// Settings contains Kafka-specific configurations needed for message creation
23
34
type Settings struct {
24
35
Topic string `json:"topic"`
25
36
Addresses []string `json:"addresses"`
37
+ Async bool `json:"async"`
26
38
* sarama.Config
27
39
}
28
40
@@ -90,6 +102,14 @@ func (e *Exporter) initializeProducer() error {
90
102
if e .dialer == nil {
91
103
e .dialer = func (addrs []string , config * sarama.Config ) (MessageSender , error ) {
92
104
// Adapter for the function to comply with the MessageSender interface return
105
+ if e .Settings .Async {
106
+ asyncProducer , err := sarama .NewAsyncProducer (addrs , config )
107
+ if err != nil {
108
+ return nil , err
109
+ }
110
+ e .Settings .Config .Producer .Return .Errors = false //TODO these should be read
111
+ return & AsyncMessageSender {AsyncProducer : asyncProducer }, nil
112
+ }
93
113
return sarama .NewSyncProducer (addrs , config )
94
114
}
95
115
}
You can’t perform that action at this time.
0 commit comments