-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
60 lines (51 loc) · 1.31 KB
/
reader.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
package reader
import (
"context"
"testing"
"time"
"github.com/IBM/sarama"
"github.com/FluorescentTouch/testosteron/sync"
)
type Message struct {
Topic string `json:"topic"`
Partition int32 `json:"partition"`
Offset int64 `json:"offset"`
Value []byte `json:"value"`
Key []byte `json:"key,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
}
type Reader struct {
testing *testing.T
consumer sarama.Consumer
offsets sync.Map[int64]
}
func New(t *testing.T, c sarama.Consumer) *Reader {
r := &Reader{
testing: t,
consumer: c,
offsets: sync.MakeSyncMap[int64](),
}
return r
}
func (r *Reader) Stop() error {
return r.consumer.Close()
}
func (r *Reader) Read(ctx context.Context, timeout time.Duration, topic string) *sarama.ConsumerMessage {
if timeout == 0 {
timeout = time.Second
}
currentOffset, _ := r.offsets.Get(topic)
partitionConn, err := newPartitioner(r.consumer, topic, currentOffset)
if err != nil {
r.testing.Fatalf("kafka reader newPartitioner error: %s", err)
return nil
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer func() {
_ = partitionConn.partition.Close()
cancel()
}()
message := partitionConn.Message(ctx)
r.offsets.Set(topic, message.Offset+1)
return message
}