google.golang.org/grpc@v1.72.2/internal/wrr/random.go (about) 1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package wrr 19 20 import ( 21 "fmt" 22 rand "math/rand/v2" 23 "sort" 24 ) 25 26 // weightedItem is a wrapped weighted item that is used to implement weighted random algorithm. 27 type weightedItem struct { 28 item any 29 weight int64 30 accumulatedWeight int64 31 } 32 33 func (w *weightedItem) String() string { 34 return fmt.Sprint(*w) 35 } 36 37 // randomWRR is a struct that contains weighted items implement weighted random algorithm. 38 type randomWRR struct { 39 items []*weightedItem 40 // Are all item's weights equal 41 equalWeights bool 42 } 43 44 // NewRandom creates a new WRR with random. 45 func NewRandom() WRR { 46 return &randomWRR{} 47 } 48 49 var randInt64n = rand.Int64N 50 51 func (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 69 func (rw *randomWRR) Add(item any, weight int64) { 70 accumulatedWeight := weight 71 equalWeights := true 72 if len(rw.items) > 0 { 73 lastItem := rw.items[len(rw.items)-1] 74 accumulatedWeight = lastItem.accumulatedWeight + weight 75 equalWeights = rw.equalWeights && weight == lastItem.weight 76 } 77 rw.equalWeights = equalWeights 78 rItem := &weightedItem{item: item, weight: weight, accumulatedWeight: accumulatedWeight} 79 rw.items = append(rw.items, rItem) 80 } 81 82 func (rw *randomWRR) String() string { 83 return fmt.Sprint(rw.items) 84 }