-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumersingle.go
219 lines (182 loc) · 5.27 KB
/
consumersingle.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
207
208
209
210
211
212
213
214
215
216
217
218
219
package wkafka
import (
"context"
"errors"
"fmt"
"time"
"github.com/twmb/franz-go/pkg/kgo"
)
type consumerSingle[T any] struct {
// Process is nil for DLQ consumer.
Process func(ctx context.Context, msg T) error
// ProcessDLQ is nil for main consumer.
ProcessDLQ func(ctx context.Context, msg T) error
Cfg *ConsumerConfig
Decode func(raw []byte, r *kgo.Record) (T, error)
// PreCheck is a function that is called before the callback and decode.
PreCheck func(ctx context.Context, r *kgo.Record) error
Option optionConsumer
ProduceDLQ func(ctx context.Context, err *DLQError, records []*kgo.Record) error
Skip func(cfg *ConsumerConfig, r *kgo.Record) bool
Logger Logger
PartitionHandler *partitionHandler
IsDLQ bool
Meter Meter
}
func (c *consumerSingle[T]) setPreCheck(fn func(ctx context.Context, r *kgo.Record) error) {
c.PreCheck = fn
}
func (c *consumerSingle[T]) Consume(ctx context.Context, cl *kgo.Client) error {
for {
// flush the partition handler, it will be ready next poll
c.PartitionHandler.Flush()
fetch := cl.PollRecords(ctx, c.Cfg.MaxPollRecords)
if fetch.IsClientClosed() {
return errClientClosed
}
if err := fetch.Err(); err != nil {
return fmt.Errorf("poll fetch err: %w", err)
}
// TODO check is needed?
if fetch.Empty() {
continue
}
if err := c.iteration(ctx, cl, fetch); err != nil {
return err
}
}
}
////////////////////
// SINGLE - ITERATION
////////////////////
func (c *consumerSingle[T]) iteration(ctx context.Context, cl *kgo.Client, fetch kgo.Fetches) error {
for iter := fetch.RecordIter(); !iter.Done(); {
r := iter.Next()
// check partition is revoked
if c.PartitionHandler.IsRevokedRecord(r) {
continue
}
start := time.Now()
if c.IsDLQ {
// listening DLQ topics
// check partition is revoked and not commit it!
// when error return than it will not be committed
if err := c.iterationDLQ(ctx, r); err != nil {
c.Meter.Meter(start, 1, r.Topic, err, true)
if errors.Is(err, errPartitionRevoked) {
// don't commit revoked partition
// above check also skip others on that partition
continue
}
return wrapErr(r, err, c.IsDLQ)
} else {
c.Meter.Meter(start, 1, r.Topic, nil, true)
}
} else {
// listening main topics
if err := c.iterationMain(ctx, r); err != nil {
c.Meter.Meter(start, 1, r.Topic, err, false)
return wrapErr(r, err, c.IsDLQ)
} else {
c.Meter.Meter(start, 1, r.Topic, nil, false)
}
// maybe working on that record is too long and partition is revoked
// not commit it, mess up the commit offest
// callback function need to be awere of getting same message again and just need skip it without error
// also error is ok due to it will be push in DLQ
if c.PartitionHandler.IsRevokedRecord(r) {
continue
}
}
// commit if not see any error
if err := cl.CommitRecords(ctx, r); err != nil {
return wrapErr(r, fmt.Errorf("commit records failed: %w", err), c.IsDLQ)
}
}
return nil
}
// iterationDLQ is used to listen DLQ topics, error usually comes from context cancellation.
// any kind of error will be retry with interval.
func (c *consumerSingle[T]) iterationDLQ(ctx context.Context, r *kgo.Record) error {
wait := waitRetry{
Interval: c.Cfg.DLQ.RetryInterval,
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if c.PartitionHandler.IsRevokedRecord(r) {
return errPartitionRevoked
}
if err := c.iterationRecord(ctx, r); err != nil {
errOrg, ok := isDQLError(err)
var errWrapped error
if ok {
errWrapped = wrapErr(r, errOrg.Err, c.IsDLQ)
} else {
errWrapped = wrapErr(r, err, c.IsDLQ)
}
c.Logger.Error("DLQ process failed", "err", errWrapped, "retry_interval", wait.CurrentInterval().String())
if err := wait.Sleep(ctx); err != nil {
return err
}
continue
}
break
}
return nil
}
// iterationMain is used to listen main topics.
func (c *consumerSingle[T]) iterationMain(ctx context.Context, r *kgo.Record) error {
if err := c.iterationRecord(ctx, r); err != nil {
errDLQ, ok := isDQLError(err)
if !ok {
// it is not DLQ error, return it
return err
}
// send to DLQ if enabled
if c.ProduceDLQ != nil {
if err := c.ProduceDLQ(ctx, errDLQ, []*kgo.Record{r}); err != nil {
return fmt.Errorf("produce to DLQ failed: %w", err)
}
return nil
}
return err
}
return nil
}
func (c *consumerSingle[T]) iterationRecord(ctx context.Context, r *kgo.Record) error {
if c.Skip(c.Cfg, r) {
c.Logger.Info("record skipped", "topic", r.Topic, "partition", r.Partition, "offset", r.Offset)
return nil
}
if c.PreCheck != nil {
if err := c.PreCheck(ctx, r); err != nil {
if errors.Is(err, ErrSkip) {
return nil
}
return fmt.Errorf("pre check failed: %w", err)
}
}
data, err := c.Decode(r.Value, r)
if err != nil {
if errors.Is(err, ErrSkip) {
return nil
}
return fmt.Errorf("decode record failed: %w", err)
}
ctxCallback := context.WithValue(ctx, KeyRecord, r)
if c.IsDLQ {
ctxCallback = context.WithValue(ctxCallback, KeyIsDLQProcess, true)
if err := c.ProcessDLQ(ctxCallback, data); err != nil {
return err
}
} else {
if err := c.Process(ctxCallback, data); err != nil {
return err
}
}
return nil
}