update code
This commit is contained in:
parent
640e022a40
commit
e1597328d2
@ -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 {
|
||||
|
@ -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
|
@ -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() {
|
Loading…
x
Reference in New Issue
Block a user