-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
152 lines (129 loc) · 3.87 KB
/
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
// Copyright 2017-2019 Skroutz S.A.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"sync"
rdkafka "github.com/confluentinc/confluent-kafka-go/kafka"
)
type ConsumerID string
type Consumer struct {
id ConsumerID
consumer *rdkafka.Consumer
topics []string
cfg rdkafka.ConfigMap
cancel context.CancelFunc
log *log.Logger
mu *sync.Mutex
terminated bool
}
type TopicPartition struct {
Topic string
Partition int32
}
type OffsetEntry struct {
tp TopicPartition
offset rdkafka.Offset
}
func NewConsumer(id ConsumerID, topics []string, cfg rdkafka.ConfigMap) (*Consumer, error) {
var err error
c := Consumer{
id: id,
topics: topics,
cfg: cfg,
log: log.New(os.Stderr, fmt.Sprintf("[consumer-%s] ", id), log.Ldate|log.Ltime),
mu: new(sync.Mutex),
}
c.consumer, err = rdkafka.NewConsumer(&cfg)
if err != nil {
return nil, err
}
return &c, nil
}
func (c *Consumer) Run(ctx context.Context) {
c.consumer.SubscribeTopics(c.topics, nil)
c.log.Printf("Started working (%v)...", c.cfg)
<-ctx.Done()
c.mu.Lock()
defer c.mu.Unlock()
// Close() will also trigger a commit since enable.auto.commit is true
// so we don't need to commit explicitly
err := c.consumer.Close()
if err != nil {
c.log.Printf("Error closing: %s", err)
}
c.terminated = true
c.log.Println("Bye")
}
// filterOutInvalidOffsets filters out any Offsets of type OffsetInvalid from the given OffsetsCommitted argument.
func filterOutInvalidOffsets(offsets rdkafka.OffsetsCommitted) rdkafka.OffsetsCommitted {
updatedParts := []rdkafka.TopicPartition{}
for _, part := range offsets.Offsets {
if part.Offset != rdkafka.OffsetInvalid {
updatedParts = append(updatedParts, part)
}
}
return rdkafka.OffsetsCommitted{offsets.Error, updatedParts}
}
func (c *Consumer) Poll(timeoutMS int) (*rdkafka.Message, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.terminated {
return nil, nil
}
ev := c.consumer.Poll(timeoutMS)
if ev == nil {
return nil, nil
}
switch e := ev.(type) {
case *rdkafka.Message:
return e, nil
case rdkafka.OffsetsCommitted:
c.log.Print(filterOutInvalidOffsets(e))
case rdkafka.Error:
// Treat errors with error code ErrTransport as transient And just log them.
// For now all other errors cause a failure.
// https://github.com/edenhill/librdkafka/issues/1987#issuecomment-422008750
if e.Code() != rdkafka.ErrTransport {
return nil, errors.New(e.String())
}
c.log.Printf("Consumer: Poll: Transient error: %s , code: %d", e.String(), e.Code())
default:
c.log.Printf("Unknown event type: %T", e)
}
return nil, nil
}
// SetOffset sets the offset for the given topic and partition to pos.
// Commiting the offset to Kafka is handled by librdkafka in the background.
func (c *Consumer) SetOffset(topic string, partition int32, pos rdkafka.Offset) error {
if pos < 0 {
return fmt.Errorf("offset cannot be negative, got %d", pos)
}
// Calling StoreOffsets manually prohibits the caller from using
// `enable.auto.offset.store` option.
// See https://github.com/edenhill/librdkafka/blob/v0.11.4/src/rdkafka.h#L2665
_, err := c.consumer.StoreOffsets([]rdkafka.TopicPartition{
{
Topic: &topic,
Partition: partition,
Offset: pos,
},
})
return err
}