-
Notifications
You must be signed in to change notification settings - Fork 2
/
producer.go
109 lines (87 loc) · 2.38 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package producer
import (
"context"
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/Shopify/sarama"
)
// Producer.
type Producer struct {
producer sarama.SyncProducer
client sarama.Client
}
var errCreateProducer = fmt.Errorf("error during producer creation")
// New.
func New(kafkaBrokers []string) (*Producer, error) {
kafkaCfg := sarama.NewConfig()
kafkaCfg.Consumer.Return.Errors = true
kafkaCfg.Version = sarama.V1_0_0_0
kafkaCfg.Consumer.Offsets.Initial = sarama.OffsetOldest
kafkaCfg.Producer.Return.Successes = true
kafkaClient, err := sarama.NewClient(kafkaBrokers, kafkaCfg)
if err != nil {
return nil, fmt.Errorf("%w: %v", errCreateProducer, err)
}
producer, err := sarama.NewSyncProducerFromClient(kafkaClient)
if err != nil {
return nil, fmt.Errorf("%w: %v", errCreateProducer, err)
}
return &Producer{producer: producer, client: kafkaClient}, nil
}
var errSendProducer = fmt.Errorf("error sending message")
// Send a message to a topic to be scattered using the key.
func (p *Producer) Send(topic, key string, message interface{}) error {
bytes, err := p.encode(message)
if err != nil {
return fmt.Errorf("%w:%v", errSendProducer, err)
}
producerMessage := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(bytes),
Key: sarama.ByteEncoder(key),
Headers: []sarama.RecordHeader{messageTypeHeader(message)},
Timestamp: time.Now(),
}
_, _, err = p.producer.SendMessage(producerMessage)
if err != nil {
return fmt.Errorf("%w:%v", errSendProducer, err)
}
return nil
}
func (p Producer) HealthCheck(_ context.Context) bool {
controller, err := p.client.Controller()
if err != nil {
return false
}
connected, err := controller.Connected()
if err != nil {
return false
}
return connected
}
// Marshaller is an interface to serialize messages to kfkTopics.
type Marshaller interface {
MarshalKFK() ([]byte, error)
}
func (p *Producer) encode(message interface{}) ([]byte, error) {
marshall, ok := message.(Marshaller)
if ok {
return marshall.MarshalKFK()
}
return json.Marshal(message)
}
func messageTypeHeader(message interface{}) sarama.RecordHeader {
return sarama.RecordHeader{
Key: []byte("@type"),
Value: []byte(messageType(message)),
}
}
func messageType(m interface{}) string {
rType := reflect.TypeOf(m)
if rType.Kind() == reflect.Ptr {
return rType.Elem().Name()
}
return rType.Name()
}