diff --git a/balance_test.go b/balance_test.go index 11bfd69..1a5d6f7 100644 --- a/balance_test.go +++ b/balance_test.go @@ -13,7 +13,7 @@ import ( "github.com/go-playground/assert/v2" - "git.zhangdeman.cn/gateway/balance/dispatch" + "git.zhangdeman.cn/gateway/balance/implement" "git.zhangdeman.cn/gateway/balance/define" ) @@ -33,7 +33,7 @@ var list = []*define.SeverNode{ func TestRand(t *testing.T) { result := make(map[string]int) - s := NewServer(list, dispatch.NewRand()) + s := NewServer(list, implement.NewRand()) for i := 0; i < 10000; i++ { node, _ := s.Get() if _, exist := result[node]; !exist { @@ -52,7 +52,7 @@ func TestRand(t *testing.T) { func TestRoundRobin(t *testing.T) { result := make(map[string]int) - s := NewServer(list, dispatch.NewRoundRobin()) + s := NewServer(list, implement.NewRoundRobin()) for i := 0; i < 10000; i++ { node, _ := s.Get() if _, exist := result[node]; !exist { diff --git a/dispatch/rand.go b/implement/rand.go similarity index 68% rename from dispatch/rand.go rename to implement/rand.go index ee79e48..bdb4944 100644 --- a/dispatch/rand.go +++ b/implement/rand.go @@ -5,10 +5,12 @@ // Author : go_developer@163.com<张德满> // // Date : 2021-04-01 5:58 下午 -package dispatch +package implement import ( "fmt" + "git.zhangdeman.cn/gateway/balance/abstract" + "git.zhangdeman.cn/zhangdeman/exception" "math/rand" "git.zhangdeman.cn/gateway/balance/define" @@ -19,7 +21,7 @@ import ( // Author : go_developer@163.com<张德满> // // Date : 6:51 下午 2021/4/1 -func NewRand() IDispatch { +func NewRand() abstract.IBalance { return &Rand{} } @@ -36,9 +38,9 @@ type Rand struct { // Author : go_developer@163.com<张德满> // // Date : 6:01 下午 2021/4/1 -func (r Rand) Get(nodeList []*define.SeverNode) (string, *define.Error) { +func (r Rand) Get(nodeList []*define.SeverNode) (string, exception.IException) { if len(nodeList) == 0 { - return "", define.NewError(define.ErrorTypeNodeListEmpty, "服务器可用节点为空") + return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空") } node := nodeList[rand.Intn(len(nodeList))] return fmt.Sprintf("%s:%d", node.Host, node.Port), nil diff --git a/dispatch/round_robin.go b/implement/round_robin.go similarity index 82% rename from dispatch/round_robin.go rename to implement/round_robin.go index fe0117f..e4c3735 100644 --- a/dispatch/round_robin.go +++ b/implement/round_robin.go @@ -5,10 +5,12 @@ // Author : go_developer@163.com<张德满> // // Date : 2021-04-01 8:04 下午 -package dispatch +package implement import ( "fmt" + "git.zhangdeman.cn/gateway/balance/abstract" + "git.zhangdeman.cn/zhangdeman/exception" "git.zhangdeman.cn/gateway/balance/define" "git.zhangdeman.cn/zhangdeman/easylock" @@ -19,7 +21,7 @@ import ( // Author : go_developer@163.com<张德满> // // Date : 8:07 下午 2021/4/1 -func NewRoundRobin() IDispatch { +func NewRoundRobin() abstract.IBalance { return &RoundRobin{ lock: easylock.NewLock(), nextNodeIndex: 0, @@ -41,9 +43,9 @@ type RoundRobin struct { // Author : go_developer@163.com<张德满> // // Date : 8:05 下午 2021/4/1 -func (r *RoundRobin) Get(nodeList []*define.SeverNode) (string, *define.Error) { +func (r *RoundRobin) Get(nodeList []*define.SeverNode) (string, exception.IException) { if len(nodeList) == 0 { - return "", define.NewError(define.ErrorTypeNodeListEmpty, "服务器可用节点为空") + return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空") } _ = r.lock.Lock() defer func() {