| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package loadbalance |
| |
| import ( |
| "math/rand" |
| ) |
| |
| import ( |
| "github.com/apache/dubbo-go/cluster" |
| "github.com/apache/dubbo-go/common/extension" |
| "github.com/apache/dubbo-go/protocol" |
| ) |
| |
| const ( |
| name = "random" |
| ) |
| |
| func init() { |
| extension.SetLoadbalance(name, NewRandomLoadBalance) |
| } |
| |
| type randomLoadBalance struct { |
| } |
| |
| // NewRandomLoadBalance returns a random load balance instance. |
| // |
| // Set random probabilities by weight, and the request will be sent to provider randomly. |
| func NewRandomLoadBalance() cluster.LoadBalance { |
| return &randomLoadBalance{} |
| } |
| |
| func (lb *randomLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker { |
| var length int |
| if length = len(invokers); length == 1 { |
| return invokers[0] |
| } |
| sameWeight := true |
| weights := make([]int64, length) |
| |
| firstWeight := GetWeight(invokers[0], invocation) |
| totalWeight := firstWeight |
| weights[0] = firstWeight |
| |
| for i := 1; i < length; i++ { |
| weight := GetWeight(invokers[i], invocation) |
| weights[i] = weight |
| |
| totalWeight += weight |
| if sameWeight && weight != firstWeight { |
| sameWeight = false |
| } |
| } |
| |
| if totalWeight > 0 && !sameWeight { |
| // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight. |
| offset := rand.Int63n(totalWeight) |
| |
| for i := 0; i < length; i++ { |
| offset -= weights[i] |
| if offset < 0 { |
| return invokers[i] |
| } |
| } |
| } |
| // If all invokers have the same weight value or totalWeight=0, return evenly. |
| return invokers[rand.Intn(length)] |
| } |