增加平滑给予权重负载的调度策略实现
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
// Package dispatch...
|
||||
// Package implement ...
|
||||
//
|
||||
// Description : dispatch...
|
||||
//
|
||||
@ -8,7 +8,6 @@
|
||||
package implement
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/gateway/balance/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
"math/rand"
|
||||
@ -38,10 +37,10 @@ type Rand struct {
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 6:01 下午 2021/4/1
|
||||
func (r Rand) Get(nodeList []*define.SeverNode) (string, exception.IException) {
|
||||
func (r Rand) Get(nodeList []*define.SeverNode) (*define.SeverNode, exception.IException) {
|
||||
if len(nodeList) == 0 {
|
||||
return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空")
|
||||
return nil, exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空")
|
||||
}
|
||||
node := nodeList[rand.Intn(len(nodeList))]
|
||||
return fmt.Sprintf("%s:%d", node.Host, node.Port), nil
|
||||
return node, nil
|
||||
}
|
||||
|
@ -8,12 +8,10 @@
|
||||
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 轮询调度
|
||||
@ -23,7 +21,6 @@ import (
|
||||
// Date : 8:07 下午 2021/4/1
|
||||
func NewRoundRobin() abstract.IBalance {
|
||||
return &RoundRobin{
|
||||
lock: easylock.NewLock(),
|
||||
nextNodeIndex: 0,
|
||||
}
|
||||
}
|
||||
@ -34,7 +31,6 @@ func NewRoundRobin() abstract.IBalance {
|
||||
//
|
||||
// Date : 8:06 下午 2021/4/1
|
||||
type RoundRobin struct {
|
||||
lock easylock.EasyLock
|
||||
nextNodeIndex int
|
||||
}
|
||||
|
||||
@ -43,19 +39,14 @@ type RoundRobin struct {
|
||||
// Author : go_developer@163.com<张德满>
|
||||
//
|
||||
// Date : 8:05 下午 2021/4/1
|
||||
func (r *RoundRobin) Get(nodeList []*define.SeverNode) (string, exception.IException) {
|
||||
func (r *RoundRobin) Get(nodeList []*define.SeverNode) (*define.SeverNode, exception.IException) {
|
||||
if len(nodeList) == 0 {
|
||||
return "", exception.New(define.ErrorTypeNodeListEmpty, nil, "服务器可用节点为空")
|
||||
return nil, 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
|
||||
return nodeList[r.nextNodeIndex], nil
|
||||
}
|
||||
|
42
implement/weight_round_robin.go
Normal file
42
implement/weight_round_robin.go
Normal file
@ -0,0 +1,42 @@
|
||||
// Package implement ...
|
||||
//
|
||||
// Description : implement ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-23 23:24
|
||||
package implement
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/gateway/balance/abstract"
|
||||
"git.zhangdeman.cn/gateway/balance/define"
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
)
|
||||
|
||||
func NewWeightRoundRobin() abstract.IBalance {
|
||||
return &WeightRoundRobin{}
|
||||
}
|
||||
|
||||
// WeightRoundRobin 加权轮询选择机器
|
||||
type WeightRoundRobin struct {
|
||||
}
|
||||
|
||||
func (w *WeightRoundRobin) Get(nodeList []*define.SeverNode) (*define.SeverNode, exception.IException) {
|
||||
totalWeight := int64(0)
|
||||
var selectedNode *define.SeverNode
|
||||
// 计算总权重
|
||||
for _, node := range nodeList {
|
||||
node.CurrentWeight = node.CurrentWeight + node.EffectiveWeight // 每个节点的当前权重
|
||||
totalWeight += node.Weight
|
||||
if nil == selectedNode {
|
||||
selectedNode = node
|
||||
} else {
|
||||
if node.CurrentWeight > selectedNode.CurrentWeight {
|
||||
selectedNode = node
|
||||
}
|
||||
}
|
||||
}
|
||||
// 当前选中节点权重重置
|
||||
selectedNode.CurrentWeight = selectedNode.CurrentWeight - totalWeight
|
||||
return selectedNode, nil
|
||||
}
|
Reference in New Issue
Block a user