-
Notifications
You must be signed in to change notification settings - Fork 10
/
remote_consumer.go
97 lines (78 loc) · 2.6 KB
/
remote_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
package kafka_wrapper
import (
"context"
"fmt"
"github.com/IBM/sarama"
"github.com/Trendyol/kafka-wrapper/params"
"strings"
)
type remoteKafkaConsumer struct {
remoteConsumer sarama.ConsumerGroup
localConsumer sarama.ConsumerGroup
}
func NewRemoteConsumer(remoteParams params.ConnectionParameters, localParams params.ConnectionParameters) (Consumer, error) {
remote, err := sarama.NewConsumerGroup(strings.Split(remoteParams.Brokers, ","), remoteParams.ConsumerGroupID, remoteParams.Conf)
local, err := sarama.NewConsumerGroup(strings.Split(localParams.Brokers, ","), localParams.ConsumerGroupID, localParams.Conf)
if err != nil {
return nil, err
}
return &remoteKafkaConsumer{
remoteConsumer: remote,
localConsumer: local,
}, nil
}
func (c *remoteKafkaConsumer) SubscribeToTopic(topicParams params.TopicsParameters, handler EventHandler) {
c.Subscribe([]params.TopicsParameters{topicParams}, handler)
}
func (c *remoteKafkaConsumer) Subscribe(topicParams []params.TopicsParameters, handler EventHandler) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
if err := c.remoteConsumer.Consume(ctx, params.JoinMainTopics(topicParams), handler); err != nil {
fmt.Printf("Error from consumer: %+v \n", err.Error())
}
fmt.Printf("Subscribed topics: %+v to remote kafka server %+v \n", params.JoinMainTopics(topicParams), c.remoteConsumer)
if ctx.Err() != nil {
fmt.Printf("Error from consumer: %+v \n", ctx.Err().Error())
}
}
}()
go func() {
for {
if err := c.localConsumer.Consume(ctx, params.JoinSecondaryTopics(topicParams), handler); err != nil {
fmt.Printf("Error from consumer: %+v \n", err.Error())
}
fmt.Printf("Subscribed topics: %+v to local kafka server \n", params.JoinSecondaryTopics(topicParams))
if ctx.Err() != nil {
fmt.Printf("Error from consumer: %+v \n", ctx.Err().Error())
}
}
}()
go func() {
for err := range c.remoteConsumer.Errors() {
fmt.Printf("Error from consumer group: %+v \n", err.Error())
}
for err := range c.localConsumer.Errors() {
fmt.Printf("Error from consumer group: %+v \n", err.Error())
}
}()
go func() {
for {
select {
case <-ctx.Done():
fmt.Println("terminating: context cancelled")
cancel()
}
}
}()
fmt.Printf("Kafka consumer listens topic : %+v \n", topicParams)
}
func (c *remoteKafkaConsumer) Unsubscribe() {
if err := c.remoteConsumer.Close(); err != nil {
fmt.Printf("Client wasn't closed: %+v \n", err)
}
if err := c.localConsumer.Close(); err != nil {
fmt.Printf("Client wasn't closed: %+v \n", err)
}
fmt.Println("Kafka consumer closed")
}