-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic_writer.go
69 lines (65 loc) · 1.84 KB
/
topic_writer.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
package kafka
import (
"context"
"crypto/tls"
"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl/scram"
"time"
)
type TopicWriter struct {
Writer *kafka.Writer
Generate func() string
}
func NewTopicWriter(writer *kafka.Writer, options ...func() string) (*TopicWriter, error) {
var generate func() string
if len(options) > 0 {
generate = options[0]
}
return &TopicWriter{Writer: writer, Generate: generate}, nil
}
func NewTopicWriterByConfig(c WriterConfig, options ...func() string) (*TopicWriter, error) {
if c.Client.Timeout <= 0 {
c.Client.Timeout = 30
}
dialer := GetDialer(c.Client.Username, c.Client.Password, scram.SHA512, &kafka.Dialer{
Timeout: time.Duration(c.Client.Timeout) * time.Second,
DualStack: true,
TLS: &tls.Config{},
})
writer := NewKafkaWriter(c.Topic, c.Brokers, dialer)
return NewTopicWriter(writer, options...)
}
func (p *TopicWriter) Write(ctx context.Context, topic string, data []byte, attributes map[string]string) error {
msg := kafka.Message{Value: data}
if attributes != nil {
msg.Headers = MapToHeader(attributes)
}
if p.Generate != nil {
id := p.Generate()
msg.Key = []byte(id)
p.Writer.Topic = topic
err := p.Writer.WriteMessages(ctx, msg)
return err
} else {
p.Writer.Topic = topic
err := p.Writer.WriteMessages(ctx, msg)
return err
}
}
func (p *TopicWriter) WriteValue(ctx context.Context, topic string, data []byte) error {
return p.Write(ctx, topic, data, nil)
}
func (p *TopicWriter) WriteWithKey(ctx context.Context, topic string, data []byte, key []byte, attributes map[string]string) (string, error) {
var binary = data
var err error
msg := kafka.Message{Value: binary}
if attributes != nil {
msg.Headers = MapToHeader(attributes)
}
if key != nil {
msg.Key = key
}
p.Writer.Topic = topic
err = p.Writer.WriteMessages(ctx, msg)
return "", err
}