forked from san-services/kafkaclient
/
kafkago_consumer.go
161 lines (128 loc) · 3.86 KB
/
kafkago_consumer.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
package kafkaclient
import (
"context"
"crypto/tls"
"time"
logger "github.com/disturb16/apilogger"
"github.com/segmentio/kafka-go"
)
type kafkagoConsumer struct {
gen *kafka.Generation
group *kafka.ConsumerGroup
topicNames []string
brokers []string
initialized chan bool
topicConfig map[string]TopicConfig
procDependencies ProcessorDependencies
failMessages chan failedMessage
}
func newKafkagoConsumer(groupID string, brokers []string,
topicNames []string, topicConf map[string]TopicConfig,
pd ProcessorDependencies, tls *tls.Config) (c kafkagoConsumer, e error) {
lg := logger.New(context.Background(), "")
d := &kafka.Dialer{
Timeout: 10 * time.Second,
DualStack: true,
TLS: tls}
group, e := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{
ID: groupID,
Brokers: brokers,
Topics: topicNames,
StartOffset: kafka.LastOffset,
Dialer: d})
if e != nil {
lg.Error(logger.LogCatUncategorized, e)
return
}
c = kafkagoConsumer{
topicConfig: topicConf,
topicNames: topicNames,
brokers: brokers,
group: group,
procDependencies: pd}
c.initialized <- true
return
}
func (c *kafkagoConsumer) startConsume(ctx context.Context) {
lg := logger.New(ctx, "")
var e error
for {
// get the next (latest) generation of a consumer group - every time
// a member (service instance in our case) enters or exits the group,
// a new generation occurs, which results in a new *kafka.Generation instance
c.gen, e = c.group.Next(context.TODO())
if e != nil {
lg.Error(logger.LogCatUncategorized, e)
break
}
for _, t := range c.topicNames {
go c.consumeTopic(ctx, t)
}
}
return
}
func (c *kafkagoConsumer) consumeTopic(ctx context.Context, topic string) {
lg := logger.New(ctx, "")
conf := c.topicConfig[topic]
// get this consumer's partition assignments by topic
assignments := c.gen.Assignments[topic]
// loop through each partition assigned, find the offset, and
// start the work or processing that should be done (gen.start func param)
// for each message
for _, assignment := range assignments {
partition, offset := assignment.ID, assignment.Offset
c.gen.Start(func(ctx context.Context) {
// create reader for this topic/partition.
reader := c.reader(topic, partition)
defer reader.Close()
// tell the reader where to read from -
// the last committed offset for this partition
reader.SetOffset(offset)
// read messages
for {
msg, e := reader.ReadMessage(ctx)
if e != nil {
lg.Error(logger.LogCatUncategorized, errMsgRead(e))
return
}
offset = msg.Offset
m := newKafkaGoMessage(msg, conf.messageCodec)
e = conf.MessageProcessor(ctx, c.procDependencies, m)
if e != nil {
// failed message processing
if conf.FailedProcessingTopic != "" {
c.failMessages <- newFailedMessage(m, conf.FailedProcessingTopic, e)
}
e = reader.SetOffset(offset)
if e != nil {
lg.Error(logger.LogCatUncategorized, errSetOffset(e))
}
c.commitOffset(ctx, topic, partition, offset-1)
}
// successful message processing
c.commitOffset(ctx, topic, partition, offset)
}
})
}
}
func (c *kafkagoConsumer) commitOffset(
ctx context.Context, topic string, partition int, offset int64) {
lg := logger.New(ctx, "")
e := c.gen.CommitOffsets(
map[string]map[int]int64{topic: {partition: offset}})
if e != nil {
lg.Error(logger.LogCatUncategorized, errCommit(e))
// retry
e := c.gen.CommitOffsets(
map[string]map[int]int64{topic: {partition: offset}})
if e != nil {
lg.Error(logger.LogCatUncategorized, errCommit(e))
}
}
}
func (c *kafkagoConsumer) reader(topic string, partition int) *kafka.Reader {
return kafka.NewReader(kafka.ReaderConfig{
Brokers: c.brokers,
Topic: topic,
Partition: partition})
}