-
Notifications
You must be signed in to change notification settings - Fork 7
/
producer.go
178 lines (139 loc) · 4.51 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package kafkaavro
import (
"encoding/binary"
"github.com/cenkalti/backoff/v3"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/linkedin/goavro"
"github.com/pkg/errors"
)
type kafkaProducer interface {
Close()
Produce(msg *kafka.Message, deliveryChan chan kafka.Event) error
}
type Producer struct {
producer kafkaProducer
topicPartition kafka.TopicPartition
schemaRegistryClient SchemaRegistryClient
schemaRegistrySubjectKey string
schemaRegistrySubjectValue string
keySchemaID int
valueSchemaID int
avroKeyCodec *goavro.Codec
avroValueCodec *goavro.Codec
backOffConfig backoff.BackOff
}
type ProducerConfig struct {
// Name of the topic where messages will be produced
TopicName string
// Avro schema for message key
KeySchema string
// Avro schema for message value
ValueSchema string
// Low level kafka producer used to produce messages
Producer kafkaProducer
// Schema registry client used for messages validation and schema management
SchemaRegistryClient SchemaRegistryClient
// BackOffConfig is used for setting backoff strategy for retry logic
BackOffConfig backoff.BackOff
}
// NewProducer is a producer that publishes messages to kafka topic using avro serialization format
func NewProducer(cfg ProducerConfig) (*Producer, error) {
if cfg.Producer == nil {
return nil, errors.New("missing producer")
}
if cfg.SchemaRegistryClient == nil {
return nil, errors.New("missing schema registry client")
}
keyCodec, err := goavro.NewCodec(cfg.KeySchema)
if err != nil {
return nil, errors.Wrap(err, "cannot initialize key codec")
}
valueCodec, err := goavro.NewCodec(cfg.ValueSchema)
if err != nil {
return nil, errors.Wrap(err, "cannot initialize value codec")
}
schemaRegistrySubjectKey := cfg.TopicName + "-key"
keySchemaID, err := cfg.SchemaRegistryClient.RegisterNewSchema(schemaRegistrySubjectKey, keyCodec)
if err != nil {
return nil, err
}
schemaRegistrySubjectValue := cfg.TopicName + "-value"
valueSchemaID, err := cfg.SchemaRegistryClient.RegisterNewSchema(schemaRegistrySubjectValue, valueCodec)
if err != nil {
return nil, err
}
return &Producer{
producer: cfg.Producer,
topicPartition: kafka.TopicPartition{
Topic: &cfg.TopicName,
Partition: kafka.PartitionAny,
},
schemaRegistryClient: cfg.SchemaRegistryClient,
schemaRegistrySubjectKey: schemaRegistrySubjectKey,
schemaRegistrySubjectValue: schemaRegistrySubjectValue,
keySchemaID: keySchemaID,
valueSchemaID: valueSchemaID,
avroKeyCodec: keyCodec,
avroValueCodec: valueCodec,
backOffConfig: cfg.BackOffConfig,
}, nil
}
// Produce will try to publish message to a topic. If deliveryChan is provided then function will return immediately,
// otherwise it will wait for delivery
func (ap *Producer) produce(key interface{}, value interface{}, deliveryChan chan kafka.Event) error {
binaryKey, err := getAvroBinary(ap.keySchemaID, ap.avroKeyCodec, key)
if err != nil {
return err
}
binaryValue, err := getAvroBinary(ap.valueSchemaID, ap.avroValueCodec, value)
if err != nil {
return err
}
handleError := false
if deliveryChan == nil {
handleError = true
deliveryChan = make(chan kafka.Event)
}
msg := &kafka.Message{
TopicPartition: ap.topicPartition,
Key: binaryKey,
Value: binaryValue,
}
ap.producer.Produce(msg, deliveryChan)
if handleError {
e := <-deliveryChan
m := e.(*kafka.Message)
if m.TopicPartition.Error != nil {
return m.TopicPartition.Error
}
}
return nil
}
func (ap *Producer) Produce(key interface{}, value interface{}, deliveryChan chan kafka.Event) error {
if ap.backOffConfig != nil {
return backoff.Retry(func() error {
return ap.produce(key, value, deliveryChan)
}, ap.backOffConfig)
}
return ap.produce(key, value, deliveryChan)
}
func getAvroBinary(schemaID int, codec *goavro.Codec, native interface{}) ([]byte, error) {
binarySchemaId := make([]byte, 4)
binary.BigEndian.PutUint32(binarySchemaId, uint32(schemaID))
// Convert native Go form to binary Avro data
binaryValue, err := codec.BinaryFromNative(nil, native)
if err != nil {
return nil, err
}
binaryMsg := make([]byte, 0, len(binaryValue)+5)
// first byte is magic byte, always 0 for now
binaryMsg = append(binaryMsg, byte(0))
// 4-byte schema ID as returned by the Schema Registry
binaryMsg = append(binaryMsg, binarySchemaId...)
// avro serialized data in Avro’s binary encoding
binaryMsg = append(binaryMsg, binaryValue...)
return binaryMsg, nil
}
func (ac *Producer) Close() {
ac.producer.Close()
}