Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of randomLoadBalance in dubbo-go

Shulou Source: shulou.com Published: 2022-06-02 10:12:30 10月03日 Update

In this issue, the editor will bring you about the role of randomLoadBalance in dubbo-go. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

RandomLoadBalance

Dubbo-go-v1.4.2/cluster/loadbalance/random.go

Const (name = "random") func init () {extension.SetLoadbalance (name, NewRandomLoadBalance)} type randomLoadBalance struct {} / / NewRandomLoadBalance... func NewRandomLoadBalance () cluster.LoadBalance {return & randomLoadBalance {}}

The NewRandomLoadBalance method of randomLoadBalance to create randomLoadBalance

Select

Dubbo-go-v1.4.2/cluster/loadbalance/random.go

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 If offset + {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)]}

The Select method first determines the number of invokers, if there is only one, it returns invokers [0]; then traverses invokers to calculate totalWeight and sameWeight, if totalWeight is greater than 0 and sameWeight is false, use rand.Int63n (totalWeight) to random an offset, then traverse weights, use offset to subtract weights [I], if offset is less than 0, return invokers [I]; if none is selected, invokersrand.Intn (length)]

Summary

RandomLoadBalance's NewRandomLoadBalance method creates randomLoadBalance; and its Select method uses the method with weight, that is, use rand.Int63n (totalWeight) to random an offset, then traverse weights, subtract weights one by one with offset. If offset is less than 0, invokers [I] is returned.

The above is what the role of randomLoadBalance in dubbo-go is shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Method function content that is analysis professional small and medium rich in content only summary quantity article more knowledge article industry perspective information information channel channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL OPPO Reno Shulou Tech Info Huawei vpn