-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconsumergroup.go
150 lines (135 loc) · 3.05 KB
/
consumergroup.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
package kafkactl
import (
"github.com/Shopify/sarama"
cluster "github.com/bsm/sarama-cluster"
)
type DEBUG struct {
HasData bool
IsNote bool
IsErr bool
Err error
Notification
}
type Notification struct {
Type string
Claimed map[string][]int32
Released map[string][]int32
Current map[string][]int32
}
type ConsumerGroup struct {
consumer *cluster.Consumer
debugChan chan *DEBUG
haveDebugChan chan bool
}
// CG returns the underlying Consumer from sarama-cluster
func (cg *ConsumerGroup) CG() *cluster.Consumer {
return cg.consumer
}
func (cg *ConsumerGroup) Close() error {
close(cg.debugChan)
return cg.consumer.Close()
}
// NewConsumerGroup returns a new ConsumerGroup
func (kc *KClient) NewConsumerGroup(groupID string, debug bool, topics ...string) (*ConsumerGroup, error) {
var cg ConsumerGroup
var dChan chan *DEBUG
kc.config.Producer.RequiredAcks = sarama.WaitForAll
kc.config.Producer.Return.Successes = true
config := cluster.NewConfig()
conf := kc.config
config.Config = *conf
config.Group.Mode = cluster.ConsumerModePartitions
if debug {
config.Consumer.Return.Errors = true
config.Group.Return.Notifications = true
dChan = make(chan *DEBUG, 256)
}
consumer, err := cluster.NewConsumer([]string{kc.bootStrap}, groupID, topics, config)
if err != nil {
return &cg, err
}
cg.consumer = consumer
if debug {
cg.debugChan = dChan
cg.haveDebugChan = make(chan bool, 1)
go startDEBUG(&cg)
}
return &cg, nil
}
// ConsumeMessages here
func (cg *ConsumerGroup) ConsumeMessages(pc cluster.PartitionConsumer, iterator func(msg *Message) bool) {
ProcessMessages:
for m := range pc.Messages() {
message := convertMsg(m)
if !iterator(message) {
break ProcessMessages
}
cg.consumer.MarkOffset(m, "") // mark message as processed
}
cg.consumer.CommitOffsets()
}
// DeBug here
func (cg *ConsumerGroup) DeBug() <-chan *DEBUG {
return cg.debugChan
}
// DebugAvailale here
func (cg *ConsumerGroup) DebugAvailale() <-chan bool {
return cg.haveDebugChan
}
// DeBug here
func startDEBUG(cg *ConsumerGroup) {
for {
select {
case note, ok := <-cg.consumer.Notifications():
if !ok {
break
}
//tmp := *note
//claimed := tmp.Claimed
/*
n := Notification{
Type: tmp.Type.String(),
Claimed: claimed,
Released: tmp.Released,
Current: tmp.Current,
}
*/
d := DEBUG{}
d.Type = note.Type.String()
d.Claimed = note.Claimed
d.Released = note.Released
d.Current = note.Current
d.HasData = true
d.IsNote = true
//fmt.Printf("%+v\n", d)
cg.debugChan <- &d
cg.haveDebugChan <- true
/*
cg.debugChan <- &DEBUG{
HasData: true,
IsNote: true,
Notification: n,
}
*/
case err, ok := <-cg.consumer.Errors():
if !ok {
break
}
if err != nil {
cg.debugChan <- &DEBUG{
HasData: true,
Err: err,
}
}
}
}
}
// ProcessDEBUG here
func (cg *ConsumerGroup) ProcessDEBUG(iterator func(d *DEBUG) bool) {
ProcessDEBUG:
for m := range cg.debugChan {
if !iterator(m) {
break ProcessDEBUG
}
}
}