forked from dfuse-io/dkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
135 lines (115 loc) · 3.19 KB
/
sender.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
package dkafka
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
"go.uber.org/zap"
)
type sender interface {
Send(msg *kafka.Message) error
CommitIfAfter(ctx context.Context, cursor string, minimumDelay time.Duration) error
Commit(ctx context.Context, cursor string) error
}
type kafkaSender struct {
sync.RWMutex
lastCommit time.Time
trxStarted bool
producer *kafka.Producer
cp checkpointer
useTransactions bool
}
func (s *kafkaSender) Send(msg *kafka.Message) error {
s.RLock()
defer s.RUnlock()
return s.producer.Produce(msg, nil)
}
func (s *kafkaSender) Close(ctx context.Context) {
if s.useTransactions {
if err := s.producer.CommitTransaction(ctx); err != nil {
zlog.Error("cannot commit transaction on close", zap.Error(err))
}
}
s.producer.Close()
}
func (s *kafkaSender) CommitIfAfter(ctx context.Context, cursor string, minimumDelay time.Duration) error {
if time.Since(s.lastCommit) > minimumDelay {
zlog.Debug("commiting cursor")
return s.Commit(ctx, cursor)
}
return nil
}
func (s *kafkaSender) Commit(ctx context.Context, cursor string) error {
s.Lock() // full write lock
defer s.Unlock()
if err := s.cp.Save(cursor); err != nil {
return fmt.Errorf("saving cursor: %w", err)
}
s.lastCommit = time.Now()
if s.useTransactions {
if err := s.producer.CommitTransaction(ctx); err != nil {
return fmt.Errorf("committing transaction: %w", err)
}
if err := s.producer.BeginTransaction(); err != nil {
return fmt.Errorf("beginning transaction: %w", err)
}
}
return nil
}
func getKafkaProducer(conf kafka.ConfigMap, name string) (*kafka.Producer, error) {
producerConfig := cloneConfig(conf)
if name != "" {
producerConfig["transactional.id"] = name
}
return kafka.NewProducer(&producerConfig)
}
func getKafkaSender(producer *kafka.Producer, cp checkpointer, useTransactions bool) (*kafkaSender, error) {
if useTransactions {
ctx := context.Background() //FIXME
if err := producer.InitTransactions(ctx); err != nil {
return nil, fmt.Errorf("running InitTransactions: %w", err)
}
// initial transaction
if err := producer.BeginTransaction(); err != nil {
return nil, fmt.Errorf("running BeginTransaction: %w", err)
}
}
return &kafkaSender{
cp: cp,
producer: producer,
useTransactions: useTransactions,
}, nil
}
type dryRunSender struct{}
type fakeMessage struct {
Topic string `json:"topic"`
Headers []string `json:"headers"`
Partition int `json:"partition"`
Offset int `json:"offset"`
TS uint64 `json:"ts"`
Key string `json:"key"`
Payload string `json:"payload"`
}
func (s *dryRunSender) Send(msg *kafka.Message) error {
out := &fakeMessage{
Payload: string(msg.Value),
Key: string(msg.Key),
}
for _, h := range msg.Headers {
out.Headers = append(out.Headers, h.Key, string(h.Value))
}
outjson, err := json.Marshal(out)
if err != nil {
return err
}
fmt.Println(string(outjson))
return nil
}
func (s *dryRunSender) CommitIfAfter(context.Context, string, time.Duration) error {
return nil
}
func (s *dryRunSender) Commit(context.Context, string) error {
return nil
}