3 Star 1 Fork 0

NightTC / Gobige

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ServerMap.go 3.97 KB
一键复制 编辑 原始数据 按行查看 历史
package serverMgr
import (
"fmt"
"sync"
)
type IServerMap interface {
//加入服务器信息
Put(sinfo IServerInfo)
//删除一个服务器信息
Delete(path string)
//更新Load值
UpLoad(path string, val int32) IServerInfo
//根据负载逻辑,获取服务器
GetBalancing(sType, sGroup uint32) (result IServerInfo, err error)
//获取指定类型服务器列表
GetListByType(sType uint32) ([]IServerInfo, error)
//获取组的指定类型服务器列表
GetListByTypeAGroup(sType, sGroup uint32) ([]IServerInfo, error)
//获取指定服务器
GetServerByID(sType, sGroup uint32, serverId uint64) (result IServerInfo, err error)
//删除所有服务器信息
DeleteAll()
}
// 随机负载
type ServerMapByRand struct {
Lock *sync.RWMutex
//服务器信息
Srvli map[uint64]map[uint64]IServerInfo
}
func NewServerMapByRand() (result *ServerMapByRand) {
result = new(ServerMapByRand)
result.Lock = new(sync.RWMutex)
result.Srvli = make(map[uint64]map[uint64]IServerInfo)
return
}
// 加入服务器信息
func (this *ServerMapByRand) Put(sinfo IServerInfo) {
this.Lock.Lock()
defer this.Lock.Unlock()
key := uint64(sinfo.GetSrvType())<<32 + uint64(sinfo.GetGroupID())
if li, ok := this.Srvli[key]; !ok {
li = make(map[uint64]IServerInfo)
this.Srvli[key] = li
li[sinfo.GetServerID()] = sinfo
} else {
li[sinfo.GetServerID()] = sinfo
}
}
// 删除一个服务器信息
func (this *ServerMapByRand) Delete(path string) {
groupid, stype, serverid := GetPathServerInfo(path)
this.Lock.Lock()
defer this.Lock.Unlock()
key := uint64(stype)<<32 + uint64(groupid)
if li, ok := this.Srvli[key]; ok {
delete(li, serverid)
}
}
// 更新Load值
func (this *ServerMapByRand) UpLoad(path string, val int32) IServerInfo {
this.Lock.Lock()
defer this.Lock.Unlock()
groupid, stype, sid := GetPathServerInfo(path)
key := uint64(stype)<<32 + uint64(groupid)
if li, ok := this.Srvli[key]; ok {
if sinfo, ok := li[sid]; ok {
sinfo.SetLoad(val)
return sinfo
}
}
return nil
}
// 根据负载逻辑,获取服务器
func (this *ServerMapByRand) 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.GetLoad() < 80 {
return v, nil
} else if v.GetLoad() < 90 {
if result == nil {
result = v
} else if result != nil && result.GetLoad() > v.GetLoad() {
result = v
}
} else if result == nil {
result = v
}
}
}
if result == nil {
return result, fmt.Errorf("Server list is empty, Type %d", stype)
}
return
}
// 获取指定类型服务器列表
func (this *ServerMapByRand) GetListByType(styp uint32) (result []IServerInfo, err error) {
this.Lock.RLock()
defer this.Lock.RUnlock()
result = make([]IServerInfo, 0, len(this.Srvli)*10)
for _, li := range this.Srvli {
for _, v := range li {
if v.GetSrvType() != styp {
break
}
result = append(result, v)
}
}
return
}
// 获取组的指定类型服务器列表
func (this *ServerMapByRand) GetListByTypeAGroup(sType, sGroup uint32) (result []IServerInfo, err error) {
this.Lock.RLock()
defer this.Lock.RUnlock()
key := uint64(sType)<<32 + uint64(sGroup)
if li, ok := this.Srvli[key]; ok {
result = make([]IServerInfo, len(li))
i := 0
for _, v := range li {
result[i] = v
i++
}
}
return
}
// 获取指定服务器
func (this *ServerMapByRand) GetServerByID(stype, groupid uint32, serverId uint64) (result IServerInfo, err error) {
this.Lock.RLock()
defer this.Lock.RUnlock()
key := uint64(stype)<<32 + uint64(groupid)
if li, ok := this.Srvli[key]; ok {
if sinfo, ok := li[serverId]; ok {
return sinfo, nil
}
}
return nil, fmt.Errorf("Cant get server, Type %d Id %d not existed", stype, serverId)
}
// 删除所有服务器信息
func (this *ServerMapByRand) DeleteAll() {
this.Lock.RLock()
defer this.Lock.RUnlock()
this.Srvli = make(map[uint64]map[uint64]IServerInfo)
}
Go
1
https://gitee.com/night-tc/gobige.git
git@gitee.com:night-tc/gobige.git
night-tc
gobige
Gobige
00125336c61c

搜索帮助

53164aa7 5694891 3bd8fe86 5694891