/
kafka_producer.go
76 lines (70 loc) · 2.15 KB
/
kafka_producer.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
package main
import (
"encoding/xml"
"fmt"
eatonconfig "github.com/ECLabs/Eaton-Feeder/config"
eatonevents "github.com/ECLabs/Eaton-Feeder/events"
"github.com/ECLabs/Eaton-Feeder/ipresolver"
"github.com/ECLabs/Eaton-Feeder/mapping"
"github.com/Shopify/sarama"
)
type IndeedKafkaProducer struct {
producer sarama.AsyncProducer
}
func NewKafkaProducer() (*IndeedKafkaProducer, error) {
config := sarama.NewConfig()
config.ClientID = ipresolver.GetLocalAddr()
config.Producer.RequiredAcks = sarama.WaitForLocal
config.Producer.Compression = sarama.CompressionNone
config.Producer.Return.Successes = true
config.Producer.Return.Errors = true
config.Producer.Partitioner = sarama.NewHashPartitioner
asyncProducer, err := sarama.NewAsyncProducer(eatonconfig.KafkaServers, config)
if err != nil {
return nil, err
}
go func() {
for msg := range asyncProducer.Successes() {
eatonevents.Info(fmt.Sprintf("Successfully sent message to topic %s with key %s", msg.Topic, msg.Key))
}
}()
go func() {
for err := range asyncProducer.Errors() {
eatonevents.Error("Failed to send message due to error: ", err)
}
}()
return &IndeedKafkaProducer{
producer: asyncProducer,
}, nil
}
func (i *IndeedKafkaProducer) Close() error {
return i.producer.Close()
}
func (i *IndeedKafkaProducer) SendMessages(jobResultChannel <-chan mapping.JobResult) (<-chan error, <-chan int) {
errorChannel := make(chan error)
kafkaDoneChannel := make(chan int)
go func() {
defer close(errorChannel)
defer close(kafkaDoneChannel)
defer i.Close()
for jobResult := range jobResultChannel {
if jobResult.IsLast() {
eatonevents.Debug("received last jobResult. returning from function and signaling that the job is complete.")
kafkaDoneChannel <- 0
return
}
bytes, err := xml.Marshal(jobResult)
if err != nil {
errorChannel <- err
continue
}
eatonevents.Debug(fmt.Sprintf("Sending JobResult JobKey: %s", jobResult.JobKey))
i.producer.Input() <- &sarama.ProducerMessage{
Topic: eatonconfig.KafkaTopic,
Value: sarama.ByteEncoder(bytes),
Key: sarama.StringEncoder(jobResult.JobKey),
}
}
}()
return errorChannel, kafkaDoneChannel
}