Skip to content

Commit c3ecce9

Browse files
authored
Merge pull request #11 from libp2p/fix/issue-65
fix bug in meter traversal logic
2 parents c3e5539 + 45424fa commit c3ecce9

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

sweeper.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ func (sw *sweeper) update() {
8989
timeMultiplier := float64(time.Second) / float64(tdiff)
9090

9191
// Calculate the bandwidth for all active meters.
92-
newLen := len(sw.meters)
9392
for i, m := range sw.meters[:sw.activeMeters] {
9493
total := atomic.LoadUint64(&m.accumulator)
9594
diff := total - m.snapshot.Total
@@ -147,8 +146,7 @@ func (sw *sweeper) update() {
147146
// Reset the rate, keep the total.
148147
m.registered = false
149148
m.snapshot.Rate = 0
150-
newLen--
151-
sw.meters[i] = sw.meters[newLen]
149+
sw.meters[i] = nil
152150
}
153151

154152
// Re-add the total to all the newly active accumulators and set the snapshot to the total.
@@ -162,10 +160,15 @@ func (sw *sweeper) update() {
162160
m.snapshot.Total = total
163161
}
164162

165-
// trim the meter list
166-
for i := newLen; i < len(sw.meters); i++ {
167-
sw.meters[i] = nil
163+
// compress and trim the meter list
164+
var newLen int
165+
for _, m := range sw.meters {
166+
if m != nil {
167+
sw.meters[newLen] = m
168+
newLen++
169+
}
168170
}
171+
169172
sw.meters = sw.meters[:newLen]
170173

171174
// Finally, mark all meters still in the list as "active".

0 commit comments

Comments
 (0)