Skip to content
Snippets Groups Projects
Commit bbed15cd authored by zonghaishang's avatar zonghaishang
Browse files

format code

parent c9f432e3
No related branches found
No related tags found
No related merge requests found
...@@ -14,43 +14,44 @@ ...@@ -14,43 +14,44 @@
package loadbalance package loadbalance
import ( import (
"github.com/dubbo/go-for-apache-dubbo/cluster"
"github.com/dubbo/go-for-apache-dubbo/common/extension"
"github.com/dubbo/go-for-apache-dubbo/protocol"
"math" "math"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
) )
import (
"github.com/dubbo/go-for-apache-dubbo/cluster"
"github.com/dubbo/go-for-apache-dubbo/common/extension"
"github.com/dubbo/go-for-apache-dubbo/protocol"
)
const ( const (
roundRobin = "roundrobin" RoundRobin = "roundrobin"
complete = 0 COMPLETE = 0
updating = 1 UPDATING = 1
) )
var ( var (
methodWeightMap sync.Map // [string]invokers methodWeightMap sync.Map // [string]invokers
state int32 = complete // update lock acquired ? state int32 = COMPLETE // update lock acquired ?
recyclePeriod int64 = 60 * time.Second.Nanoseconds() recyclePeriod int64 = 60 * time.Second.Nanoseconds()
) )
func init() { func init() {
extension.SetLoadbalance(roundRobin, NewRoundRobinLoadBalance) extension.SetLoadbalance(RoundRobin, NewRoundRobinLoadBalance)
} }
type roundRobinLoadBalance struct { type roundRobinLoadBalance struct{}
}
func NewRoundRobinLoadBalance() cluster.LoadBalance { func NewRoundRobinLoadBalance() cluster.LoadBalance {
return &roundRobinLoadBalance{} return &roundRobinLoadBalance{}
} }
func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker { func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
count := len(invokers) count := len(invokers)
if invokers == nil || count == 0 { if count == 0 {
return nil return nil
} }
if count == 1 { if count == 1 {
...@@ -69,7 +70,6 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation ...@@ -69,7 +70,6 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation
now := time.Now() now := time.Now()
for _, invoker := range invokers { for _, invoker := range invokers {
var weight = GetWeight(invoker, invocation) var weight = GetWeight(invoker, invocation)
if weight < 0 { if weight < 0 {
weight = 0 weight = 0
...@@ -108,8 +108,8 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation ...@@ -108,8 +108,8 @@ func (lb *roundRobinLoadBalance) Select(invokers []protocol.Invoker, invocation
} }
func cleanIfRequired(clean bool, invokers cachedInvokers, now *time.Time) { func cleanIfRequired(clean bool, invokers cachedInvokers, now *time.Time) {
if atomic.LoadInt32(&state) < updating && clean && atomic.CompareAndSwapInt32(&state, complete, updating) { if atomic.LoadInt32(&state) < UPDATING && clean && atomic.CompareAndSwapInt32(&state, COMPLETE, UPDATING) {
defer atomic.CompareAndSwapInt32(&state, updating, complete) defer atomic.CompareAndSwapInt32(&state, UPDATING, COMPLETE)
invokers.Range(func(identify, robin interface{}) bool { invokers.Range(func(identify, robin interface{}) bool {
weightedRoundRobin := robin.(weightedRoundRobin) weightedRoundRobin := robin.(weightedRoundRobin)
if now.Sub(*weightedRoundRobin.lastUpdate).Nanoseconds() > recyclePeriod { if now.Sub(*weightedRoundRobin.lastUpdate).Nanoseconds() > recyclePeriod {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment