-
Notifications
You must be signed in to change notification settings - Fork 2
/
kafka_producer.go
254 lines (227 loc) · 7.66 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package kafka_go
import (
"fmt"
"github.com/Shopify/sarama"
"os"
"os/signal"
"syscall"
"time"
)
/*
NewKafkaProducer creates a kafkaProducer instance used for producing messages to kafka cluster.
Input kafkaProducerParam should be created using DefaultKafkaProducerParam and then the required
params should changed from there. Its not allowed to construct kafkaProducerParam directly. This
is enforced to prevent user from setting unexpected default values.
If the user is only interested with providing broker address and do not willing to changes any default
param values, NewKafkaProducerQuick can be used instead in such cases. which only takes the broker
as input.
example:
params := DefaultKafkaProducerParam([]string{"localhost:9091", "localhost:9092", "localhost:9093"})
params.Retry = 5
params.Acknowledge = AtAll
producer, err := NewKafkaProducer(params)
*/
func NewKafkaProducer(params *kafkaProducerParam) (*kafkaProducer, error) {
if params.Brokers == nil || len(params.Brokers) == 0 {
Logger.Println("No broker provided")
return nil, fmt.Errorf("at least one broker is mandatory")
}
if params.Compression > 4 || params.Compression < 0 {
Logger.Println("Invalid compression type")
return nil, fmt.Errorf("compression type is not valid")
}
if params.Acknowledge > 1 || params.Acknowledge < -1 {
Logger.Println("Invalid acknowledgement type")
return nil, fmt.Errorf("acknowledgement type is not valid")
}
config := getConfig(params)
producer := &kafkaProducer{
ap: asyncProducer(params.Brokers, config),
sp: syncProducer(params.Brokers, config),
}
watchCloseSignal(producer)
return producer, nil
}
/*
NewKafkaProducerQuick is quicker way to instantiate a producer instance by just providing the
brokers address all together. It will take case of other params and set them to there best default
values. In case user want to have better configured producer as per their use case, they should
rather use NewKafkaProducer with custom kafkaProducerParam key values. DefaultKafkaProducerParam
is the only way to create kafkaProducerParam because changing any other params associated.
*/
func NewKafkaProducerQuick(brokers []string) (*kafkaProducer, error) {
params := DefaultKafkaProducerParam(brokers)
return NewKafkaProducer(params)
}
/*
DefaultKafkaProducerParam is only way to create kafkaProducerParam instance to be used for producer
instance creation. This hsa been restricted to avoid user from setting bad default values while
configuring a producer. User interested in changing the params should first create param instance
using this method and then change the specific param key if needed.
example:
params := DefaultKafkaProducerParam(brokers)
params.LogError = false
*/
func DefaultKafkaProducerParam(brokers []string) *kafkaProducerParam {
return &kafkaProducerParam{
Brokers: brokers,
FlushFrequency: 500 * time.Millisecond,
FlushMessages: 5,
LogError: true,
Retry: 3,
Compression: CtSnappy,
Acknowledge: AtLocal,
}
}
/*
kafkaProducerParam can't be created by end user to protect default values.
its make it easy to start from default values by using DefaultKafkaProducerParam.
All you need is just the brokers to start using the producer.
*/
type kafkaProducerParam struct {
// Brokers in kafka clusters
Brokers []string
// [Optional]
// Client identity for logging purpose
ClientID string
// [Optional]
// kafka cluster version. eg - "2.2.1" default - "2.3.0"
// supports versions from "0.8.x" to "2.3.x"
Version string
// [Optional]
// How frequently should the message be flushed to the cluster. default - 500ms
FlushFrequency time.Duration
// [Optional]
// Number of message to trigger a flush. default - 5
FlushMessages int
// [Optional]
// Log error on failure while publishing messaged. default - true
// applicable only for async producer
LogError bool
// [Optional]
// Total retries to publish a message. default - 3
Retry int
// [Optional]
// Compression while publishing the message. default - snappy
Compression CompressionType
// [Optional]
// Acknowledgement for the published message. default - local
Acknowledge AcknowledgmentType
}
type kafkaProducer struct {
ap sarama.AsyncProducer
sp sarama.SyncProducer
}
func (kp *kafkaProducer) PublishSync(message *PublisherMessage) (meta map[string]interface{}, err error) {
partition, offset, err := kp.sp.SendMessage(&sarama.ProducerMessage{
Topic: message.Topic,
Key: sarama.StringEncoder(message.Key),
Value: sarama.ByteEncoder(message.Data),
})
meta = make(map[string]interface{})
meta["partition"] = partition
meta["offset"] = offset
return
}
func (kp *kafkaProducer) PublishSyncBulk(messages []*PublisherMessage) error {
producerMessages := make([]*sarama.ProducerMessage, 0, len(messages))
for _, message := range messages {
producerMessages = append(producerMessages, &sarama.ProducerMessage{
Topic: message.Topic,
Key: sarama.StringEncoder(message.Key),
Value: sarama.ByteEncoder(message.Data),
})
}
return kp.sp.SendMessages(producerMessages)
}
func (kp *kafkaProducer) PublishAsync(message *PublisherMessage) {
kp.ap.Input() <- &sarama.ProducerMessage{
Topic: message.Topic,
Key: sarama.StringEncoder(message.Key),
Value: sarama.ByteEncoder(message.Data),
}
}
func (kp *kafkaProducer) PublishAsyncBulk(messages []*PublisherMessage) {
for _, message := range messages {
go func(msg *PublisherMessage) {
kp.ap.Input() <- &sarama.ProducerMessage{
Topic: msg.Topic,
Key: sarama.StringEncoder(msg.Key),
Value: sarama.ByteEncoder(msg.Data),
}
}(message)
}
}
func (kp *kafkaProducer) Close() {
err := kp.sp.Close()
if err != nil {
Logger.Printf("Error closing sync producer, %v", err)
}
err = kp.ap.Close()
if err != nil {
Logger.Printf("Error closing async producer, %v", err)
}
Logger.Printf("Producer closed")
}
func watchCloseSignal(producer *kafkaProducer) {
stopSignal := make(chan os.Signal)
signal.Notify(stopSignal, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig := <-stopSignal
Logger.Printf("Close signal received %v", sig)
producer.Close()
}()
}
func getConfig(params *kafkaProducerParam) *sarama.Config {
config := sarama.NewConfig()
if params.ClientID != "" {
config.ClientID = params.ClientID
}
if params.Version != "" {
version, err := sarama.ParseKafkaVersion(params.Version)
if err != nil {
Logger.Fatalf("Error parsing kafka version, %v", err)
}
config.Version = version
} else {
config.Version = sarama.MaxVersion
}
config.Producer.RequiredAcks = sarama.RequiredAcks(params.Acknowledge)
config.Producer.Compression = sarama.CompressionCodec(params.Compression)
config.Producer.Flush.Frequency = params.FlushFrequency
config.Producer.Flush.Messages = params.FlushMessages
config.Producer.Return.Successes = true
config.Producer.Return.Errors = params.LogError
config.Producer.Retry.Max = params.Retry
return config
}
func syncProducer(brokers []string, config *sarama.Config) sarama.SyncProducer {
// overriding Return keys for sync producer
config.Producer.Return.Errors = true
syncProducer, err := sarama.NewSyncProducer(brokers, config)
if err != nil {
Logger.Fatalf("Unable to create sync producer: %v", err)
}
return syncProducer
}
func asyncProducer(brokers []string, config *sarama.Config) sarama.AsyncProducer {
asyncProducer, err := sarama.NewAsyncProducer(brokers, config)
if err != nil {
Logger.Fatalf("Unable to create async producer: %v", err)
}
if config.Producer.Return.Errors {
go func() {
for err := range asyncProducer.Errors() {
Logger.Printf("Error from async producer: %v", err)
}
}()
}
if config.Producer.Return.Successes {
go func() {
for range asyncProducer.Successes() {
continue
}
}()
}
return asyncProducer
}