-
Notifications
You must be signed in to change notification settings - Fork 1
/
group_reader.go
123 lines (103 loc) · 2.56 KB
/
group_reader.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
package krater
import (
"fmt"
"io"
"strconv"
"sync"
"github.com/ORBAT/krater/kafkaconsumer"
)
var grIdGen = sequentialIntGen()
type GroupReader struct {
started bool
group string
sub kafkaconsumer.Subscription
zkConn string
cgConf *kafkaconsumer.Config
closeCh chan chan error
writeMut sync.Mutex
log StdLogger
id string
newCg func() (kafkaconsumer.Consumer, error)
}
func NewGroupReader(group string, topics []string, zookeeper string, cgConf *kafkaconsumer.Config) (gr *GroupReader, err error) {
if cgConf == nil {
cgConf = kafkaconsumer.NewConfig()
}
// this _must_ be false since the consumer's error channel is never read from in kafkaconsumer
cgConf.Config.Consumer.Return.Errors = false
if err = cgConf.Validate(); err != nil {
return
}
id := fmt.Sprintf("%s (%s)", "grprd-"+strconv.Itoa(grIdGen()), group)
log := newLogger(id, nil)
log.Println("Created")
return &GroupReader{
group: group,
sub: kafkaconsumer.TopicSubscription(topics...),
zkConn: zookeeper,
cgConf: cgConf,
log: log,
closeCh: make(chan chan error, 1),
id: id,
}, nil
}
func (gr GroupReader) String() string {
return gr.id
}
// WriteTo joins the consumer group and starts consuming from its topics.
func (gr *GroupReader) WriteTo(w io.Writer) (n int64, err error) {
gr.writeMut.Lock()
defer gr.writeMut.Unlock()
gr.log.Println("Starting WriteTo")
var cg kafkaconsumer.Consumer
if gr.newCg != nil { // newCg is only used in tests for now
cg, err = gr.newCg()
} else {
cg, err = kafkaconsumer.Join(gr.group, gr.sub, gr.zkConn, gr.cgConf)
}
if err != nil {
gr.log.Printf("Couldn't join consumer group: %s", err)
return
}
go func() {
if cerr, ok := <-cg.Errors(); ok && cerr != nil {
gr.log.Printf("Consumer gave us an error: %s", cerr)
err = cerr
gr.Close()
}
}()
msgCh := cg.Messages()
msgLoop:
for {
select {
case msg, ok := <-msgCh:
if !ok {
gr.log.Println("Consumer message channel closed")
break msgLoop
}
gr.log.Printf("Got message %p", msg)
nw, werr := w.Write(msg.Value)
gr.log.Printf("Wrote %d bytes (%+v)", nw, werr)
if werr != nil {
err = werr
break msgLoop
}
cg.Ack(msg)
gr.log.Printf("Acked %p", msg)
n += int64(nw)
case errCh := <-gr.closeCh:
errCh <- cg.Close()
break msgLoop
}
}
gr.log.Printf("Finished WriteTo with n %d, err %+v", n, err)
return
}
func (gr *GroupReader) Close() (err error) {
gr.log.Println("Closing")
ch := make(chan error)
gr.closeCh <- ch
err = <-ch
gr.log.Printf("Closed, error was %+v", err)
return
}