-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsumer.go
155 lines (128 loc) · 3.56 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
153
154
155
package kafka
import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"github.com/Shopify/sarama"
)
type KafkaConsumer struct {
BrokerList string
Partitions string
Offset string
Verbose bool
BufferSize int
Messages chan *sarama.ConsumerMessage
Closing chan struct{}
WaitGroup sync.WaitGroup
}
func (this *KafkaConsumer) ConsumeMessage(topic string, funcs ...interface{}) {
var kafkaConsumer = KafkaConsumer{
BrokerList: this.BrokerList,
Partitions: this.Partitions,
Offset: this.Offset,
Verbose: this.Verbose,
BufferSize: this.BufferSize,
Messages: make(chan *sarama.ConsumerMessage, defaultBufferSize),
Closing: make(chan struct{}),
WaitGroup: this.WaitGroup,
}
fmt.Println("Start consumeMessage")
if kafkaConsumer.validate() == false {
os.Exit(1)
}
go kafkaConsumer.waitForKillSignal()
c, err := sarama.NewConsumer(strings.Split(kafkaConsumer.BrokerList, ","), nil)
if err != nil {
fmt.Println("Failed to start consumer:", err)
os.Exit(1)
}
defer kafkaConsumer.closeConsumer(c)
if kafkaConsumer.getMessageWithGoRoutine(c, topic) == false {
os.Exit(1)
}
go callback(kafkaConsumer.Messages, funcs...)
kafkaConsumer.WaitGroup.Wait() //program will wait here until receive KILL SIGNAL
fmt.Println("Done consuming topic", topic)
close(kafkaConsumer.Messages)
}
func (this *KafkaConsumer) closeConsumer(c sarama.Consumer) {
err := c.Close()
if err != nil {
fmt.Println("Failed to close Kafkaconsumer:", err)
}
}
func (this *KafkaConsumer) validate() bool {
if this.BrokerList == "" {
fmt.Println("You have to provide -brokers as a comma-separated list")
return false
}
return true
}
func (this *KafkaConsumer) getMessageWithGoRoutine(c sarama.Consumer, topic string) bool {
var initialOffset int64
switch this.Offset {
case "oldest":
initialOffset = sarama.OffsetOldest
case "newest":
initialOffset = sarama.OffsetNewest
default:
fmt.Println("-offset should be `oldest` or `newest`")
return false
}
partitionList, err := this.getPartitions(c, topic)
if err != nil {
fmt.Println("Failed to get the list of partitions:", err)
return false
}
for _, partition := range partitionList {
pc, err := c.ConsumePartition(topic, partition, initialOffset)
if err != nil {
fmt.Printf("Failed to start Kafkaconsumer for partition %d: %s\n", partition, err)
return false
}
go this.asynClosePartition(pc)
this.WaitGroup.Add(1)
go this.getMessagesFromPartition(pc)
}
return true
}
func (this *KafkaConsumer) asynClosePartition(pc sarama.PartitionConsumer) {
<-this.Closing
pc.AsyncClose()
}
func (this *KafkaConsumer) getMessagesFromPartition(pc sarama.PartitionConsumer) {
defer this.WaitGroup.Done()
for message := range pc.Messages() {
this.Messages <- message
}
}
func (this *KafkaConsumer) waitForKillSignal() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, terminatedSignals...)
<-signals
fmt.Println("Initiating shutdown of Kafkaconsumer...")
close(this.Closing)
}
func (this *KafkaConsumer) sleepReportSuccess() {
for msg := range this.Messages {
fmt.Printf("Partition=%d\tOffset=%d\tKey=%s\tValue=%s\n", msg.Partition, msg.Offset, string(msg.Key), string(msg.Value))
}
}
func (this *KafkaConsumer) getPartitions(c sarama.Consumer, topic string) ([]int32, error) {
if this.Partitions == "all" {
return c.Partitions(topic)
}
tmp := strings.Split(this.Partitions, ",")
var pList []int32
for i := range tmp {
val, err := strconv.ParseInt(tmp[i], 10, 32)
if err != nil {
return nil, err
}
pList = append(pList, int32(val))
}
return pList, nil
}