5 Star 20 Fork 10

NightTC/Gobige

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ServerMapByMinID.go 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
buguang 提交于 2025-03-24 17:40 +08:00 . feat(server): 增加服务器状态管理功能
package serverMgr
import (
"fmt"
"gitee.com/night-tc/gobige/msgdef/protomsg"
)
//防止后续逻辑有修改,先不直接用嵌套了 looby{rand}
// 优先分配小服务ID
type ServerMapByMinID struct {
ServerMapByRand
}
func NewServerMapByMinID() (result *ServerMapByMinID) {
result = new(ServerMapByMinID)
result.Srvli = make(map[uint64]map[uint64]IServerInfo)
return
}
// 根据负载逻辑,获取服务器
func (this *ServerMapByMinID) GetBalancing(stype, groupid uint32) (result IServerInfo, err error) {
this.Lock.RLock()
defer this.Lock.RUnlock()
result = nil
key := uint64(stype)<<32 + uint64(groupid)
if li, ok := this.Srvli[key]; ok {
//优先使用服务器号更小的
for _, v := range li {
if v.GetStatus() != protomsg.ServerStatus_DefaultOK {
continue
}
if result == nil {
result = v
continue
}
if result.GetServerID() > v.GetServerID() {
result = v
continue
}
}
if result != nil {
if result.GetLoad() < 80 {
return result, nil
}
}
//如果最小id服务器负载不满足,这里会使用基本分配规则
for _, v := range li {
if v.GetLoad() < 80 {
return v, nil
}
if result == nil {
result = v
continue
}
if v.GetLoad() < 90 {
if result != nil && result.GetLoad() > v.GetLoad() {
result = v
}
}
}
}
if result == nil {
return result, fmt.Errorf("Server list is empty, Type %d", stype)
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/night-tc/gobige.git
git@gitee.com:night-tc/gobige.git
night-tc
gobige
Gobige
3e91623f3517

搜索帮助