consistent
consistent copied to clipboard
AverageLoad() function panics with "divide by zero" when no members are in the hash ring
If there are no members in the hashring, calling the AverageLoad() function results in a "divide-by-zero" panic.
Example, this will result in a panic:
package main
import (
"fmt"
"github.com/buraksezer/consistent"
"github.com/cespare/xxhash"
)
type myMember string
func (m myMember) String() string {
return string(m)
}
type hasher struct{}
func (h hasher) Sum64(data []byte) uint64 {
return xxhash.Sum64(data)
}
func main() {
cfg := consistent.Config{
PartitionCount: 7,
ReplicationFactor: 20,
Load: 1.25,
Hasher: hasher{},
}
c := consistent.New(nil, cfg)
fmt.Printf("average load: %f", c.AverageLoad())
}
The AverageLoad() function should check if the number of members is > 0, and if it is not simply return a fixed value (such as 0 or -1).
+1
// AverageLoad exposes the current average load.
func (c *Consistent) AverageLoad() float64 {
avgLoad := float64(c.partitionCount/uint64(len(c.members))) * c.config.Load
return math.Ceil(avgLoad)
}
len(c.members) may be equal to zero