前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo-go的roundRobinLoadBalance

聊聊dubbo-go的roundRobinLoadBalance

作者头像
code4it
发布2020-08-17 15:35:40
2980
发布2020-08-17 15:35:40
举报
文章被收录于专栏:码匠的流水账码匠的流水账

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

roundRobinLoadBalance

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

代码语言:javascript
复制
const (
    // RoundRobin ...
    RoundRobin = "roundrobin"

    // COMPLETE ...
    COMPLETE = 0
    // UPDATING ...
    UPDATING = 1
)

var (
    methodWeightMap sync.Map          // [string]invokers
    state           = int32(COMPLETE) // update lock acquired ?
    recyclePeriod   = 60 * time.Second.Nanoseconds()
)

func init() {
    extension.SetLoadbalance(RoundRobin, NewRoundRobinLoadBalance)
}

type roundRobinLoadBalance struct{}

// NewRoundRobinLoadBalance ...
func NewRoundRobinLoadBalance() cluster.LoadBalance {
    return &roundRobinLoadBalance{}
}
  • roundRobinLoadBalance的NewRoundRobinLoadBalance方法创建了roundRobinLoadBalance

Select

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

代码语言:javascript
复制
func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
    count := len(invokers)
    if count == 0 {
        return nil
    }
    if count == 1 {
        return invokers[0]
    }

    key := invokers[0].GetUrl().Path + "." + invocation.MethodName()
    cache, _ := methodWeightMap.LoadOrStore(key, &cachedInvokers{})
    cachedInvokers := cache.(*cachedInvokers)

    var (
        clean               = false
        totalWeight         = int64(0)
        maxCurrentWeight    = int64(math.MinInt64)
        now                 = time.Now()
        selectedInvoker     protocol.Invoker
        selectedWeightRobin *weightedRoundRobin
    )

    for _, invoker := range invokers {
        var weight = GetWeight(invoker, invocation)
        if weight < 0 {
            weight = 0
        }

        identifier := invoker.GetUrl().Key()
        loaded, found := cachedInvokers.LoadOrStore(identifier, &weightedRoundRobin{weight: weight})
        weightRobin := loaded.(*weightedRoundRobin)
        if !found {
            clean = true
        }

        if weightRobin.Weight() != weight {
            weightRobin.setWeight(weight)
        }

        currentWeight := weightRobin.increaseCurrent()
        weightRobin.lastUpdate = &now

        if currentWeight > maxCurrentWeight {
            maxCurrentWeight = currentWeight
            selectedInvoker = invoker
            selectedWeightRobin = weightRobin
        }
        totalWeight += weight
    }

    cleanIfRequired(clean, cachedInvokers, &now)

    if selectedWeightRobin != nil {
        selectedWeightRobin.Current(totalWeight)
        return selectedInvoker
    }

    // should never happen
    return invokers[0]
}
  • 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

doc

  • round_robin
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • roundRobinLoadBalance
  • Select
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档