Beispiel #1
0
// rebalance removes dead shards from the ring.
func (ring *Ring) rebalance() {
	defer ring.mx.Unlock()
	ring.mx.Lock()

	ring.hash = consistenthash.New(ring.nreplicas, ring.hasher)
	for name, shard := range ring.shards {
		if shard.IsUp() {
			ring.hash.Add(name)
		}
	}
}
Beispiel #2
0
func NewRing(opt *RingOptions) *Ring {
	ring := &Ring{
		opt:       opt,
		nreplicas: DefaultRingReplicas,
		shards:    make(map[string]*ringShard),
	}
	ring.commandable.process = ring.process

	if opt.NReplicas != 0 {
		ring.nreplicas = opt.NReplicas
	}
	if opt.Hasher != nil {
		ring.hasher = opt.Hasher
	}
	ring.hash = consistenthash.New(ring.nreplicas, ring.hasher)

	for name, addr := range opt.Addrs {
		clopt := opt.clientOptions()
		clopt.Addr = addr
		ring.addClient(name, NewClient(clopt))
	}
	go ring.heartbeat()
	return ring
}