github.com/cloudwego/kitex@v0.9.0/pkg/loadbalance/weighted_random.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package loadbalance 18 19 import ( 20 "context" 21 22 "github.com/bytedance/gopkg/lang/fastrand" 23 24 "github.com/cloudwego/kitex/pkg/discovery" 25 ) 26 27 type weightedRandomPicker struct { 28 instances []discovery.Instance 29 weightSum int 30 } 31 32 func newWeightedRandomPickerWithSum(instances []discovery.Instance, weightSum int) Picker { 33 return &weightedRandomPicker{ 34 instances: instances, 35 weightSum: weightSum, 36 } 37 } 38 39 // Next implements the Picker interface. 40 func (wp *weightedRandomPicker) Next(ctx context.Context, request interface{}) (ins discovery.Instance) { 41 weight := fastrand.Intn(wp.weightSum) 42 for i := 0; i < len(wp.instances); i++ { 43 weight -= wp.instances[i].Weight() 44 if weight < 0 { 45 return wp.instances[i] 46 } 47 } 48 return nil 49 } 50 51 type randomPicker struct { 52 instances []discovery.Instance 53 } 54 55 func newRandomPicker(instances []discovery.Instance) Picker { 56 return &randomPicker{ 57 instances: instances, 58 } 59 } 60 61 // Next implements the Picker interface. 62 func (rp *randomPicker) Next(ctx context.Context, request interface{}) (ins discovery.Instance) { 63 idx := fastrand.Intn(len(rp.instances)) 64 return rp.instances[idx] 65 }