-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
206 lines (190 loc) · 5.2 KB
/
kafka.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
package windy
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"github.com/Visforest/goset/v2"
"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/visforest/windy/core"
"os"
"time"
)
type kClient struct {
ctx context.Context
conn *kafka.Conn
writer *kafka.Writer
reader *kafka.Reader
}
func (c *kClient) Push(m *core.Msg) error {
val, err := json.Marshal(m)
if err != nil {
return err
}
return c.writer.WriteMessages(c.ctx, kafka.Message{Key: []byte(m.Id), Value: val})
}
func (c *kClient) Fetch() (*core.Msg, error) {
message, err := c.reader.FetchMessage(c.ctx)
if err != nil {
return nil, err
}
return core.DecodeMsgFromBytes(message.Value)
}
func (c *kClient) FetchDelayMsgs() ([]*core.Msg, error) {
panic("not supported")
}
type KProducer struct {
producerCore *core.ProducerCore
client *kClient
}
// NewKProducer returns a producer and an error
func NewKProducer(cfg *KConf, opts ...core.ProducerOption) (*KProducer, error) {
conn, err := kafka.Dial("tcp", cfg.Kafka.Brokers[0])
if err != nil {
return nil, err
}
writer := &kafka.Writer{
Addr: kafka.TCP(cfg.Kafka.Brokers...),
Topic: cfg.Topic,
AllowAutoTopicCreation: cfg.Kafka.AutoCreateTopic,
Balancer: &kafka.LeastBytes{},
Compression: kafka.Snappy,
}
if cfg.Kafka.AutoCreateTopic {
// although writer can create topic if missing, but partitions count and replications count are important for efficiency,
// but writer doesn't ensure that
err = conn.CreateTopics(kafka.TopicConfig{
Topic: cfg.Topic,
NumPartitions: cfg.Kafka.Partitions,
ReplicationFactor: cfg.Kafka.Replications,
})
if err != nil {
return nil, err
}
}
producerCore := &core.ProducerCore{
Ctx: context.Background(),
Topic: cfg.Topic,
IdCreator: core.NewSnowflakeCreator(1),
}
for _, opt := range opts {
opt(producerCore)
}
client := &kClient{
ctx: producerCore.Ctx,
conn: conn,
writer: writer,
reader: nil,
}
return &KProducer{
producerCore: producerCore,
client: client,
}, nil
}
// MustNewKProducer returns a producer or panic if fails
func MustNewKProducer(cfg *KConf, opts ...core.ProducerOption) *KProducer {
producer, err := NewKProducer(cfg, opts...)
if err != nil {
panic(err)
}
return producer
}
// Send sends data to message queue
func (p *KProducer) Send(data any, opts ...core.MsgOption) (string, error) {
return p.producerCore.Send(p.client, core.NewMsg(data, opts...))
}
type KConsumer struct {
consumerCore *core.ConsumerCore
client *kClient
}
// NewKConsumer returns a consumer and error
func NewKConsumer(cfg *KConf, ConsumeFunc core.ConsumeFunc, opts ...core.ConsumerOption) (*KConsumer, error) {
readerConfig := kafka.ReaderConfig{
Brokers: cfg.Kafka.Brokers,
GroupID: cfg.Kafka.Group,
Topic: cfg.Topic,
MinBytes: cfg.Kafka.MinBytes,
MaxBytes: cfg.Kafka.MaxBytes,
StartOffset: kafka.FirstOffset,
}
if len(cfg.Kafka.Username) > 0 && len(cfg.Kafka.Password) > 0 {
readerConfig.Dialer = &kafka.Dialer{
SASLMechanism: plain.Mechanism{
Username: cfg.Kafka.Username,
Password: cfg.Kafka.Password,
},
}
}
if len(cfg.Kafka.CaFile) > 0 {
caCert, err := os.ReadFile(cfg.Kafka.CaFile)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
return nil, errors.New("certificate file is invalid")
}
readerConfig.Dialer.TLS = &tls.Config{
RootCAs: caCertPool,
InsecureSkipVerify: true,
}
}
// connect and get partitions count
conn, err := kafka.Dial("tcp", cfg.Kafka.Brokers[0])
if err != nil {
return nil, err
}
partitions, err := conn.ReadPartitions(cfg.Topic)
if err != nil {
return nil, err
}
if cfg.Workers <= 0 {
// set topic partition count or 1 as default consumer count
cfg.Workers = len(partitions)
} else if cfg.Workers < len(partitions) {
// warning, it's not the best practice
}
reader := kafka.NewReader(readerConfig)
var batchProcess *BatchProcessConf
if cfg.BatchProcess == nil {
batchProcess = &BatchProcessConf{}
} else {
batchProcess = cfg.BatchProcess
}
consumerCore := &core.ConsumerCore{
Ctx: context.Background(),
ConsumeFunc: ConsumeFunc,
Processors: goset.NewSortedSet[core.ProcessorType](),
WorkersNum: cfg.Workers,
Topic: cfg.Topic,
BatchProcessCnt: batchProcess.Batch,
BatchProcessTimeout: time.Duration(batchProcess.Timeout) * time.Second,
}
for _, opt := range opts {
opt(consumerCore)
}
client := &kClient{
ctx: consumerCore.Ctx,
conn: conn,
writer: nil,
reader: reader,
}
return &KConsumer{
consumerCore: consumerCore,
client: client,
}, nil
}
// MustNewKConsumer returns a consumer, if it fails, panic
func MustNewKConsumer(cfg *KConf, ConsumeFunc core.ConsumeFunc, opts ...core.ConsumerOption) *KConsumer {
consumer, err := NewKConsumer(cfg, ConsumeFunc, opts...)
if err != nil {
panic(err)
}
return consumer
}
// LoopConsume blocks and consumes msgs in loop with multi goroutine
func (c *KConsumer) LoopConsume() {
c.consumerCore.LoopConsume(c.client)
}