Skip to content

Commit 06cbab2

Browse files
committed
group: sync result collection. #47
1 parent d6b6ce5 commit 06cbab2

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

group.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,7 @@ func (cmd *groupCmd) run(args []string, q chan struct{}) {
9595
for _, grp := range groups {
9696
for _, top := range topics {
9797
go func(grp, topic string) {
98-
target := group{Name: grp, Topic: topic, Offsets: map[int32]groupOffset{}}
99-
cmd.fetchGroupTopicOffset(target.Offsets, grp, topic)
100-
if len(target.Offsets) > 0 {
101-
buf, _ := json.Marshal(target)
102-
fmt.Println(string(buf))
103-
}
98+
cmd.printGroupTopicOffset(grp, topic)
10499
wg.Done()
105100
}(grp, top)
106101
}
@@ -114,19 +109,47 @@ func (cmd *groupCmd) run(args []string, q chan struct{}) {
114109
}
115110
}
116111

117-
func (cmd *groupCmd) fetchGroupTopicOffset(target map[int32]groupOffset, grp, top string) {
112+
func (cmd *groupCmd) printGroupTopicOffset(grp, top string) {
113+
target := group{Name: grp, Topic: top, Offsets: map[int32]groupOffset{}}
114+
results := make(chan groupOffsetResult)
115+
done := make(chan struct{})
118116
parts := []int32{cmd.partition}
119117
if cmd.partition == -23 {
120118
parts = cmd.fetchPartitions(top)
121119
}
122120
wg := &sync.WaitGroup{}
123121
wg.Add(len(parts))
124122
for _, part := range parts {
125-
go cmd.fetchGroupOffset(wg, target, grp, top, part)
123+
go cmd.fetchGroupOffset(wg, grp, top, part, results)
124+
}
125+
go func() { wg.Wait(); close(done) }()
126+
127+
awaitGroupOffsets:
128+
for {
129+
select {
130+
case res := <-results:
131+
if !res.empty {
132+
target.Offsets[res.partition] = groupOffset{Offset: res.offset, Lag: res.lag}
133+
}
134+
case <-done:
135+
break awaitGroupOffsets
136+
}
126137
}
127-
wg.Wait()
138+
139+
if len(target.Offsets) > 0 {
140+
buf, _ := json.Marshal(target)
141+
fmt.Println(string(buf))
142+
}
143+
}
144+
145+
type groupOffsetResult struct {
146+
partition int32
147+
offset int64
148+
lag int64
149+
empty bool
128150
}
129-
func (cmd *groupCmd) fetchGroupOffset(wg *sync.WaitGroup, target map[int32]groupOffset, grp, top string, part int32) {
151+
152+
func (cmd *groupCmd) fetchGroupOffset(wg *sync.WaitGroup, grp, top string, part int32, results chan groupOffsetResult) {
130153
var (
131154
err error
132155
groupOff int64
@@ -139,13 +162,17 @@ func (cmd *groupCmd) fetchGroupOffset(wg *sync.WaitGroup, target map[int32]group
139162
if offsetManager, err = sarama.NewOffsetManagerFromClient(grp, cmd.client); err != nil {
140163
failf("failed to create client err=%v", err)
141164
}
165+
defer logClose("offset manager", offsetManager)
166+
142167
pom, err := offsetManager.ManagePartition(top, part)
143168
if err != nil {
144169
failf("failed to manage partition group=%s topic=%s partition=%d err=%v", grp, top, part, err)
145170
}
171+
defer logClose("partition offset manager", pom)
172+
146173
groupOff, _ = pom.NextOffset()
147174
if groupOff == sarama.OffsetNewest {
148-
// no offset recorded yet
175+
results <- groupOffsetResult{empty: true}
149176
return
150177
}
151178

@@ -158,9 +185,7 @@ func (cmd *groupCmd) fetchGroupOffset(wg *sync.WaitGroup, target map[int32]group
158185
failf("failed to read partition offset for topic=%s partition=%d err=%v", top, part, err)
159186
}
160187

161-
target[part] = groupOffset{Offset: groupOff, Lag: partOff - groupOff}
162-
logClose("partition offset manager", pom)
163-
logClose("offset manager", offsetManager)
188+
results <- groupOffsetResult{partition: part, offset: groupOff, lag: partOff - groupOff}
164189
}
165190

166191
func (cmd *groupCmd) fetchTopics() []string {

0 commit comments

Comments
 (0)