nezha/service/singleton/server.go

77 lines
2.3 KiB
Go
Raw Normal View History

2022-04-12 01:16:33 -04:00
package singleton
import (
"sort"
"sync"
2022-04-14 04:41:34 -04:00
"github.com/naiba/nezha/model"
2022-04-12 01:16:33 -04:00
)
var (
2022-05-16 23:21:27 -04:00
ServerList map[uint64]*model.Server // [ServerID] -> model.Server
SecretToID map[string]uint64 // [ServerSecret] -> ServerID
ServerTagToIDList map[string][]uint64 // [ServerTag] -> ServerID
ServerLock sync.RWMutex
2022-04-12 01:16:33 -04:00
SortedServerList []*model.Server // 用于存储服务器列表的 slice按照服务器 ID 排序
SortedServerListForGuest []*model.Server
SortedServerLock sync.RWMutex
2022-04-12 01:16:33 -04:00
)
// InitServer 初始化 ServerID <-> Secret 的映射
func InitServer() {
ServerList = make(map[uint64]*model.Server)
SecretToID = make(map[string]uint64)
2022-05-16 23:21:27 -04:00
ServerTagToIDList = make(map[string][]uint64)
2022-04-12 01:16:33 -04:00
}
// LoadServers 加载服务器列表并根据ID排序
2022-04-12 01:16:33 -04:00
func LoadServers() {
InitServer()
var servers []model.Server
DB.Find(&servers)
for _, s := range servers {
innerS := s
innerS.Host = &model.Host{}
innerS.State = &model.HostState{}
ServerList[innerS.ID] = &innerS
SecretToID[innerS.Secret] = innerS.ID
2022-05-16 23:21:27 -04:00
ServerTagToIDList[innerS.Tag] = append(ServerTagToIDList[innerS.Tag], innerS.ID)
2022-04-12 01:16:33 -04:00
}
ReSortServer()
}
// ReSortServer 根据服务器ID 对服务器列表进行排序ID越大越靠前
func ReSortServer() {
ServerLock.RLock()
defer ServerLock.RUnlock()
SortedServerLock.Lock()
defer SortedServerLock.Unlock()
SortedServerList = []*model.Server{}
SortedServerListForGuest = []*model.Server{}
2022-04-12 01:16:33 -04:00
for _, s := range ServerList {
SortedServerList = append(SortedServerList, s)
if !s.HideForGuest {
filteredStat := *s
filteredStat.DDNSDomain = "redacted"
SortedServerListForGuest = append(SortedServerListForGuest, &filteredStat)
}
2022-04-12 01:16:33 -04:00
}
// 按照服务器 ID 排序的具体实现ID越大越靠前
sort.SliceStable(SortedServerList, func(i, j int) bool {
if SortedServerList[i].DisplayIndex == SortedServerList[j].DisplayIndex {
return SortedServerList[i].ID < SortedServerList[j].ID
}
return SortedServerList[i].DisplayIndex > SortedServerList[j].DisplayIndex
})
sort.SliceStable(SortedServerListForGuest, func(i, j int) bool {
if SortedServerListForGuest[i].DisplayIndex == SortedServerListForGuest[j].DisplayIndex {
return SortedServerListForGuest[i].ID < SortedServerListForGuest[j].ID
}
return SortedServerListForGuest[i].DisplayIndex > SortedServerListForGuest[j].DisplayIndex
})
2022-04-12 01:16:33 -04:00
}