dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/random/loadbalance.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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 random
    19  
    20  import (
    21  	"math/rand"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
    26  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    27  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    28  	"dubbo.apache.org/dubbo-go/v3/protocol"
    29  )
    30  
    31  func init() {
    32  	extension.SetLoadbalance(constant.LoadBalanceKeyRandom, NewRandomLoadBalance)
    33  }
    34  
    35  type randomLoadBalance struct{}
    36  
    37  // NewRandomLoadBalance returns a random load balance instance.
    38  //
    39  // Set random probabilities by weight, and the request will be sent to provider randomly.
    40  func NewRandomLoadBalance() loadbalance.LoadBalance {
    41  	return &randomLoadBalance{}
    42  }
    43  
    44  func (lb *randomLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
    45  
    46  	// Number of invokers
    47  	var length int
    48  	if length = len(invokers); length == 1 {
    49  		return invokers[0]
    50  	}
    51  
    52  	// Every invoker has the same weight?
    53  	sameWeight := true
    54  	// the maxWeight of every invokers, the minWeight = 0 or the maxWeight of the last invoker
    55  	weights := make([]int64, length)
    56  	// The sum of weights
    57  	var totalWeight int64 = 0
    58  
    59  	for i := 0; i < length; i++ {
    60  		weight := loadbalance.GetWeight(invokers[i], invocation)
    61  		//Sum
    62  		totalWeight += weight
    63  		// save for later use
    64  		weights[i] = totalWeight
    65  		if sameWeight && totalWeight != weight*(int64(i+1)) {
    66  			sameWeight = false
    67  		}
    68  	}
    69  
    70  	if totalWeight > 0 && !sameWeight {
    71  		// If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
    72  		offset := rand.Int63n(totalWeight)
    73  
    74  		for i := 0; i < length; i++ {
    75  			if offset < weights[i] {
    76  				return invokers[i]
    77  			}
    78  		}
    79  	}
    80  	// If all invokers have the same weight value or totalWeight=0, return evenly.
    81  	return invokers[rand.Intn(length)]
    82  }