MCPcopy
hub / github.com/grpc/grpc-go / Next

Method Next

internal/wrr/random.go:51–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

49var randInt64n = rand.Int64N
50
51func (rw *randomWRR) Next() (item any) {
52 if len(rw.items) == 0 {
53 return nil
54 }
55 if rw.equalWeights {
56 return rw.items[randInt64n(int64(len(rw.items)))].item
57 }
58
59 sumOfWeights := rw.items[len(rw.items)-1].accumulatedWeight
60 // Random number in [0, sumOfWeights).
61 randomWeight := randInt64n(sumOfWeights)
62 // Item's accumulated weights are in ascending order, because item's weight >= 0.
63 // Binary search rw.items to find first item whose accumulatedWeight > randomWeight
64 // The return i is guaranteed to be in range [0, len(rw.items)) because randomWeight < last item's accumulatedWeight
65 i := sort.Search(len(rw.items), func(i int) bool { return rw.items[i].accumulatedWeight > randomWeight })
66 return rw.items[i].item
67}
68
69func (rw *randomWRR) Add(item any, weight int64) {
70 accumulatedWeight := weight

Callers

nothing calls this directly

Calls 1

SearchMethod · 0.65

Tested by

no test coverage detected