Unverified Commit c3ecce9c authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #11 from libp2p/fix/issue-65

fix bug in meter traversal logic
parents c3e55399 45424fab
...@@ -89,7 +89,6 @@ func (sw *sweeper) update() { ...@@ -89,7 +89,6 @@ func (sw *sweeper) update() {
timeMultiplier := float64(time.Second) / float64(tdiff) timeMultiplier := float64(time.Second) / float64(tdiff)
// Calculate the bandwidth for all active meters. // Calculate the bandwidth for all active meters.
newLen := len(sw.meters)
for i, m := range sw.meters[:sw.activeMeters] { for i, m := range sw.meters[:sw.activeMeters] {
total := atomic.LoadUint64(&m.accumulator) total := atomic.LoadUint64(&m.accumulator)
diff := total - m.snapshot.Total diff := total - m.snapshot.Total
...@@ -147,8 +146,7 @@ func (sw *sweeper) update() { ...@@ -147,8 +146,7 @@ func (sw *sweeper) update() {
// Reset the rate, keep the total. // Reset the rate, keep the total.
m.registered = false m.registered = false
m.snapshot.Rate = 0 m.snapshot.Rate = 0
newLen-- sw.meters[i] = nil
sw.meters[i] = sw.meters[newLen]
} }
// Re-add the total to all the newly active accumulators and set the snapshot to the total. // 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() { ...@@ -162,10 +160,15 @@ func (sw *sweeper) update() {
m.snapshot.Total = total m.snapshot.Total = total
} }
// trim the meter list // compress and trim the meter list
for i := newLen; i < len(sw.meters); i++ { var newLen int
sw.meters[i] = nil for _, m := range sw.meters {
if m != nil {
sw.meters[newLen] = m
newLen++
}
} }
sw.meters = sw.meters[:newLen] sw.meters = sw.meters[:newLen]
// Finally, mark all meters still in the list as "active". // Finally, mark all meters still in the list as "active".
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment