-
Notifications
You must be signed in to change notification settings - Fork 1
/
kafka.go
59 lines (46 loc) · 1.21 KB
/
kafka.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
package main
import (
"log"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
type kafkaClient interface {
Events() chan kafka.Event
SubscribeTopics([]string, kafka.RebalanceCb) error
}
type Kafka struct {
consumer kafkaClient
}
func NewKafka(bootstrapServers, topic string) (k Kafka, err error) {
k.consumer, err = kafka.NewConsumer(&kafka.ConfigMap{
"bootstrap.servers": bootstrapServers,
"auto.offset.reset": "earliest",
"group.id": "kafka-to-redisearch",
"session.timeout.ms": 30000,
"go.events.channel.enable": true,
"go.application.rebalance.enable": false,
"enable.partition.eof": true,
})
if err != nil {
return
}
err = k.consumer.SubscribeTopics([]string{topic}, nil)
return
}
func (k Kafka) ConsumerLoop(c chan MessageWithEnvelope) (err error) {
for ev := range k.consumer.Events() {
switch ev.(type) {
case *kafka.Message:
msg, err := ParseMessage(ev.(*kafka.Message).Value)
if err != nil {
log.Printf("Kafka Loop: invalid message: %+v", err)
continue
}
c <- msg
case kafka.Error:
return ev.(kafka.Error)
default:
log.Printf("Kafka Loop: %+v, %T", ev, ev)
}
}
return
}