Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of randomLoadBalance in dubbo-go

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report