mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
BUG FIXED: RoundRobinCounter.Add will cause a out of range crash
when index >= len(rrc.Values)
This commit is contained in:
parent
c65b9588e2
commit
eaad4fa3c4
|
@ -23,14 +23,11 @@ func NewRoundRobinCounter(slots int) *RoundRobinCounter {
|
||||||
return &RoundRobinCounter{LastIndex: -1, Values: make([]int64, slots), Counts: make([]int64, slots)}
|
return &RoundRobinCounter{LastIndex: -1, Values: make([]int64, slots), Counts: make([]int64, slots)}
|
||||||
}
|
}
|
||||||
func (rrc *RoundRobinCounter) Add(index int, val int64) {
|
func (rrc *RoundRobinCounter) Add(index int, val int64) {
|
||||||
for rrc.LastIndex != index {
|
|
||||||
rrc.LastIndex++
|
|
||||||
if rrc.LastIndex >= len(rrc.Values) {
|
|
||||||
if index >= len(rrc.Values) {
|
if index >= len(rrc.Values) {
|
||||||
break //just avoid endless loop
|
return
|
||||||
}
|
|
||||||
rrc.LastIndex = 0
|
|
||||||
}
|
}
|
||||||
|
for rrc.LastIndex != index {
|
||||||
|
rrc.LastIndex = (rrc.LastIndex + 1) % len(rrc.Values)
|
||||||
rrc.Values[rrc.LastIndex] = 0
|
rrc.Values[rrc.LastIndex] = 0
|
||||||
rrc.Counts[rrc.LastIndex] = 0
|
rrc.Counts[rrc.LastIndex] = 0
|
||||||
}
|
}
|
||||||
|
|
19
go/stats/duration_counter_test.go
Normal file
19
go/stats/duration_counter_test.go
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
package stats
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRobinCounter(t *testing.T) {
|
||||||
|
rrc := NewRoundRobinCounter(60)
|
||||||
|
rrc.Add(0, 1)
|
||||||
|
rrc.Add(50, 2)
|
||||||
|
if rrc.Count() != 2 {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
if rrc.Sum() != 3 {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
index out of range
|
||||||
|
*/
|
||||||
|
rrc.Add(61, 1)
|
||||||
|
}
|
Loading…
Reference in a new issue