您的位置 首页 > 德语词汇

goround的(聊聊dubbo-go的roundRobinLoadBalance)

本篇文章给大家谈谈goround的,以及聊聊dubbo-go的roundRobinLoadBalance对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。

goround的(聊聊dubbo-go的roundRobinLoadBalance)

本文主要研究一下dubbo-go的roundRobinLoadBalance

dubbo-go-v1.4.2/cluster/loadbalance/round_robin.go

const(\n//RoundRobin...\nRoundRobin="roundrobin"\n?\n//COMPLETE...\nCOMPLETE=0\n//UPDATING...\nUPDATING=1\n)\n?\nvar(\nmethodWeightMapsync.Map//[string]invokers\nstate=int32(COMPLETE)//updatelockacquired?\nrecyclePeriod=60*time.Second.Nanoseconds()\n)\n?\nfuncinit(){\nextension.SetLoadbalance(RoundRobin,NewRoundRobinLoadBalance)\n}\n?\ntyperoundRobinLoadBalancestruct{}\n?\n//NewRoundRobinLoadBalance...\nfuncNewRoundRobinLoadBalance()cluster.LoadBalance{\nreturn&roundRobinLoadBalance{}\n}roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalanceSelect

dubbo-go-v1.4.2/cluster/loadbalance/round_robin.go

func(lb*roundRobinLoadBalance)Select(invokers[]protocol.Invoker,invocationprotocol.Invocation)protocol.Invoker{\ncount:=len(invokers)\nifcount==0{\nreturnnil\n}\nifcount==1{\nreturninvokers[0]\n}\n?\nkey:=invokers[0].GetUrl().Path+"."+invocation.MethodName()\ncache,_:=methodWeightMap.LoadOrStore(key,&cachedInvokers{})\ncachedInvokers:=cache.(*cachedInvokers)\n?\nvar(\nclean=false\ntotalWeight=int64(0)\nmaxCurrentWeight=int64(math.MinInt64)\nnow=time.Now()\nselectedInvokerprotocol.Invoker\nselectedWeightRobin*weightedRoundRobin\n)\n?\nfor_,invoker:=rangeinvokers{\nvarweight=GetWeight(invoker,invocation)\nifweight<0{\nweight=0\n}\n?\nidentifier:=invoker.GetUrl().Key()\nloaded,found:=cachedInvokers.LoadOrStore(identifier,&weightedRoundRobin{weight:weight})\nweightRobin:=loaded.(*weightedRoundRobin)\nif!found{\nclean=true\n}\n?\nifweightRobin.Weight()!=weight{\nweightRobin.setWeight(weight)\n}\n?\ncurrentWeight:=weightRobin.increaseCurrent()\nweightRobin.lastUpdate=&now\n?\nifcurrentWeight>maxCurrentWeight{\nmaxCurrentWeight=currentWeight\nselectedInvoker=invoker\nselectedWeightRobin=weightRobin\n}\ntotalWeight+=weight\n}\n?\ncleanIfRequired(clean,cachedInvokers,&now)\n?\nifselectedWeightRobin!=nil{\nselectedWeightRobin.Current(totalWeight)\nreturnselectedInvoker\n}\n?\n//shouldneverhappen\nreturninvokers[0]\n}Select方法遍历invokers,通过weightRobin.increaseCurrent()作为currentWeight,若currentWeight大于maxCurrentWeight则更新maxCurrentWeight,设置selectedInvoker为当前invoker,设置selectedWeightRobin为当前weightRobin;之后对于selectedWeightRobin不为nil的执行selectedWeightRobin.Current(totalWeight),返回selectedInvoker小结

roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalance;其Select方法遍历invokers,通过weightRobin.increaseCurrent()作为currentWeight,若currentWeight大于maxCurrentWeight则更新maxCurrentWeight,设置selectedInvoker为当前invoker,设置selectedWeightRobin为当前weightRobin

好了,文章到这里就结束啦,如果本次分享的goround的和聊聊dubbo-go的roundRobinLoadBalance问题对您有所帮助,还望关注下本站哦!

本站涵盖的内容、图片、视频等数据,部分未能与原作者取得联系。若涉及版权问题,请及时通知我们并提供相关证明材料,我们将及时予以删除!谢谢大家的理解与支持!

Copyright © 2023