Skip to content

Commit

Permalink
fix comment.
Browse files Browse the repository at this point in the history
Signed-off-by: imxyb <[email protected]>
  • Loading branch information
imxyb committed Dec 13, 2019
1 parent 7fb327e commit 80e5bf5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 12 additions & 11 deletions cluster/loadbalance/consistent_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"sort"
"strconv"
"strings"
)

import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
Expand All @@ -39,17 +41,19 @@ const (
HashArguments = "hash.arguments"
)

var selectors = make(map[string]*ConsistentHashSelector)
var re = regexp.MustCompile(constant.COMMA_SPLIT_PATTERN)
var (
selectors = make(map[string]*ConsistentHashSelector)
re = regexp.MustCompile(constant.COMMA_SPLIT_PATTERN)
)

func init() {
extension.SetLoadbalance(ConsistentHash, NewConsistentHash)
extension.SetLoadbalance(ConsistentHash, NewConsistentHashLoadBalance)
}

type ConsistentHashLoadBalance struct {
}

func NewConsistentHash() cluster.LoadBalance {
func NewConsistentHashLoadBalance() cluster.LoadBalance {
return &ConsistentHashLoadBalance{}
}

Expand All @@ -62,14 +66,14 @@ func (lb *ConsistentHashLoadBalance) Select(invokers []protocol.Invoker, invocat
for _, invoker := range invokers {
b, err := json.Marshal(invoker)
if err != nil {
panic(fmt.Sprintf("parse json failed:%s", err.Error()))
return nil
}
bs = append(bs, b...)
}
hashCode := crc32.ChecksumIEEE(bs)
selector, ok := selectors[key]
if !ok || selector.hashCode != hashCode {
selectors[key] = NewConsistentHashSelector(invokers, methodName, hashCode)
selectors[key] = newConsistentHashSelector(invokers, methodName, hashCode)
selector = selectors[key]
}
return selector.Select(invocation)
Expand Down Expand Up @@ -97,11 +101,8 @@ type ConsistentHashSelector struct {
argumentIndex []int
}

func NewConsistentHashSelector(invokers []protocol.Invoker, methodName string,
func newConsistentHashSelector(invokers []protocol.Invoker, methodName string,
hashCode uint32) *ConsistentHashSelector {
if len(invokers) == 0 {
panic("none of invokers")
}

selector := &ConsistentHashSelector{}
selector.virtualInvokers = make(map[uint32]protocol.Invoker)
Expand All @@ -112,7 +113,7 @@ func NewConsistentHashSelector(invokers []protocol.Invoker, methodName string,
for _, index := range indices {
i, err := strconv.Atoi(index)
if err != nil {
panic(err)
return nil
}
selector.argumentIndex = append(selector.argumentIndex, i)
}
Expand Down
8 changes: 6 additions & 2 deletions cluster/loadbalance/consistent_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package loadbalance
import (
"context"
"testing"
)

import (
"github.com/stretchr/testify/suite"
)

import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
Expand All @@ -43,7 +47,7 @@ func (s *consistentHashSelectorSuite) SetupTest() {
url, _ := common.NewURL(context.TODO(),
"dubbo://192.168.1.0:20000/org.apache.demo.HelloService?methods.echo.hash.arguments=0,1")
invokers = append(invokers, protocol.NewBaseInvoker(url))
s.selector = NewConsistentHashSelector(invokers, "echo", 999944)
s.selector = newConsistentHashSelector(invokers, "echo", 999944)
}

func (s *consistentHashSelectorSuite) TestToKey() {
Expand Down Expand Up @@ -92,7 +96,7 @@ func (s *consistentHashLoadBalanceSuite) SetupTest() {
s.invoker3 = protocol.NewBaseInvoker(s.url3)

s.invokers = append(s.invokers, s.invoker1, s.invoker2, s.invoker3)
s.lb = NewConsistentHash()
s.lb = NewConsistentHashLoadBalance()
}

func (s *consistentHashLoadBalanceSuite) TestSelect() {
Expand Down

0 comments on commit 80e5bf5

Please sign in to comment.