update code
This commit is contained in:
47
implement/rand.go
Normal file
47
implement/rand.go
Normal file
@ -0,0 +1,47 @@
|
||||
// Package dispatch...
|
||||
//
|
||||
// Description : dispatch...
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 2021-04-01 5:58 下午
|
||||
package implement
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/gateway/balance/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
"math/rand"
|
||||
|
||||
"git.zhangdeman.cn/gateway/balance/define"
|
||||
)
|
||||
|
||||
// NewRand ...
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 6:51 下午 2021/4/1
|
||||
func NewRand() abstract.IBalance {
|
||||
return &Rand{}
|
||||
}
|
||||
|
||||
// Rand 随机选择
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 6:01 下午 2021/4/1
|
||||
type Rand struct {
|
||||
}
|
||||
|
||||
// Get 获取 host + 端口
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 6:01 下午 2021/4/1
|
||||
func (r Rand) Get(nodeList []*define.SeverNode) (string, exception.IException) {
|
||||
if len(nodeList) == 0 {
|
||||
return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空")
|
||||
}
|
||||
node := nodeList[rand.Intn(len(nodeList))]
|
||||
return fmt.Sprintf("%s:%d", node.Host, node.Port), nil
|
||||
}
|
61
implement/round_robin.go
Normal file
61
implement/round_robin.go
Normal file
@ -0,0 +1,61 @@
|
||||
// Package dispatch ...
|
||||
//
|
||||
// Description : Round Robin 轮询选择服务器
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 2021-04-01 8:04 下午
|
||||
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"
|
||||
)
|
||||
|
||||
// NewRoundRobin 轮询调度
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 8:07 下午 2021/4/1
|
||||
func NewRoundRobin() abstract.IBalance {
|
||||
return &RoundRobin{
|
||||
lock: easylock.NewLock(),
|
||||
nextNodeIndex: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// RoundRobin 轮询选择机器
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 8:06 下午 2021/4/1
|
||||
type RoundRobin struct {
|
||||
lock easylock.EasyLock
|
||||
nextNodeIndex int
|
||||
}
|
||||
|
||||
// Get ...
|
||||
//
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 8:05 下午 2021/4/1
|
||||
func (r *RoundRobin) Get(nodeList []*define.SeverNode) (string, exception.IException) {
|
||||
if len(nodeList) == 0 {
|
||||
return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空")
|
||||
}
|
||||
_ = r.lock.Lock()
|
||||
defer func() {
|
||||
_ = r.lock.Unlock()
|
||||
}()
|
||||
if r.nextNodeIndex >= len(nodeList) {
|
||||
// 记录过索引之后, 在下次访问之前, 可能移除了某些节点, 所以要检测越界
|
||||
r.nextNodeIndex = len(nodeList) - 1
|
||||
}
|
||||
node := fmt.Sprintf("%s:%d", nodeList[r.nextNodeIndex].Host, nodeList[r.nextNodeIndex].Port)
|
||||
r.nextNodeIndex = (r.nextNodeIndex + 1) % len(nodeList)
|
||||
return node, nil
|
||||
}
|
Reference in New Issue
Block a user