-
Notifications
You must be signed in to change notification settings - Fork 3
/
kafka.go
56 lines (48 loc) · 926 Bytes
/
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
package arseeding
import (
"context"
"github.com/segmentio/kafka-go"
)
const (
ItemTopic = "arseeding_transaction"
BlockTopic = "arseeding_block"
)
type KWriter struct {
w *kafka.Writer
}
func NewKWriter(topic string, uri string) (*KWriter, error) {
w := &kafka.Writer{
Addr: kafka.TCP(uri),
Topic: topic,
Balancer: &kafka.LeastBytes{},
}
return &KWriter{
w: w,
}, nil
}
func (kw *KWriter) Write(body []byte) error {
err := kw.w.WriteMessages(
context.Background(),
kafka.Message{
Value: body,
},
)
return err
}
func (kw *KWriter) Close() {
kw.w.Close()
}
func NewKWriters(uri string) (map[string]*KWriter, error) {
itemWriter, err := NewKWriter(ItemTopic, uri)
if err != nil {
return nil, err
}
blockWriter, err := NewKWriter(BlockTopic, uri)
if err != nil {
return nil, err
}
return map[string]*KWriter{
ItemTopic: itemWriter,
BlockTopic: blockWriter,
}, nil
}