mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
⚡️ refactor: 将排序的服务器列表的锁拆分出来
This commit is contained in:
parent
d059835877
commit
bf71aeaaf9
@ -90,8 +90,8 @@ func (p *commonPage) service(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (cp *commonPage) home(c *gin.Context) {
|
||||
dao.ServerLock.RLock()
|
||||
defer dao.ServerLock.RUnlock()
|
||||
dao.SortedServerLock.RLock()
|
||||
defer dao.SortedServerLock.RUnlock()
|
||||
|
||||
c.HTML(http.StatusOK, "theme-"+dao.Conf.Site.Theme+"/home", mygin.CommonEnvironment(c, gin.H{
|
||||
"Servers": dao.SortedServerList,
|
||||
@ -115,9 +115,9 @@ func (cp *commonPage) ws(c *gin.Context) {
|
||||
}
|
||||
defer conn.Close()
|
||||
for {
|
||||
dao.ServerLock.RLock()
|
||||
dao.SortedServerLock.RLock()
|
||||
err = conn.WriteJSON(dao.SortedServerList)
|
||||
dao.ServerLock.RUnlock()
|
||||
dao.SortedServerLock.RUnlock()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
@ -53,11 +53,11 @@ func (ma *memberAPI) delete(c *gin.Context) {
|
||||
var err error
|
||||
switch c.Param("model") {
|
||||
case "server":
|
||||
dao.ServerLock.Lock()
|
||||
defer dao.ServerLock.Unlock()
|
||||
err = dao.DB.Delete(&model.Server{}, "id = ?", id).Error
|
||||
if err == nil {
|
||||
dao.ServerLock.Lock()
|
||||
delete(dao.ServerList, id)
|
||||
dao.ServerLock.Unlock()
|
||||
dao.ReSortServer()
|
||||
}
|
||||
case "notification":
|
||||
@ -109,14 +109,10 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
|
||||
s.ID = sf.ID
|
||||
s.Tag = sf.Tag
|
||||
if sf.ID == 0 {
|
||||
dao.ServerLock.Lock()
|
||||
defer dao.ServerLock.Unlock()
|
||||
s.Secret = com.MD5(fmt.Sprintf("%s%s%d", time.Now(), sf.Name, admin.ID))
|
||||
s.Secret = s.Secret[:10]
|
||||
err = dao.DB.Create(&s).Error
|
||||
} else {
|
||||
dao.ServerLock.RLock()
|
||||
defer dao.ServerLock.RUnlock()
|
||||
isEdit = true
|
||||
err = dao.DB.Save(&s).Error
|
||||
}
|
||||
@ -129,13 +125,18 @@ func (ma *memberAPI) addOrEditServer(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if isEdit {
|
||||
dao.ServerLock.RLock()
|
||||
s.Host = dao.ServerList[s.ID].Host
|
||||
s.State = dao.ServerList[s.ID].State
|
||||
dao.ServerList[s.ID] = &s
|
||||
dao.ServerLock.RUnlock()
|
||||
} else {
|
||||
s.Host = &model.Host{}
|
||||
s.State = &model.HostState{}
|
||||
dao.ServerLock.Lock()
|
||||
dao.ServerList[s.ID] = &s
|
||||
dao.ServerLock.Unlock()
|
||||
}
|
||||
dao.ServerList[s.ID] = &s
|
||||
dao.ReSortServer()
|
||||
c.JSON(http.StatusOK, model.Response{
|
||||
Code: http.StatusOK,
|
||||
|
@ -29,8 +29,8 @@ func (mp *memberPage) serve() {
|
||||
}
|
||||
|
||||
func (mp *memberPage) server(c *gin.Context) {
|
||||
dao.ServerLock.RLock()
|
||||
defer dao.ServerLock.RUnlock()
|
||||
dao.SortedServerLock.RLock()
|
||||
defer dao.SortedServerLock.RUnlock()
|
||||
c.HTML(http.StatusOK, "dashboard/server", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": "服务器管理",
|
||||
"Servers": dao.SortedServerList,
|
||||
|
@ -31,7 +31,7 @@ func DispatchTask(duration time.Duration) {
|
||||
var tasks []model.Monitor
|
||||
var hasAliveAgent bool
|
||||
dao.DB.Find(&tasks)
|
||||
dao.ServerLock.RLock()
|
||||
dao.SortedServerLock.RLock()
|
||||
startedAt := time.Now()
|
||||
for i := 0; i < len(tasks); i++ {
|
||||
if index >= uint64(len(dao.SortedServerList)) {
|
||||
@ -50,7 +50,7 @@ func DispatchTask(duration time.Duration) {
|
||||
dao.SortedServerList[index].TaskStream.Send(tasks[i].PB())
|
||||
index++
|
||||
}
|
||||
dao.ServerLock.RUnlock()
|
||||
dao.SortedServerLock.RUnlock()
|
||||
time.Sleep(time.Until(startedAt.Add(duration)))
|
||||
}
|
||||
}
|
||||
|
@ -22,13 +22,19 @@ var Cache *cache.Cache
|
||||
var DB *gorm.DB
|
||||
|
||||
var ServerList map[uint64]*model.Server
|
||||
var SortedServerList []*model.Server
|
||||
|
||||
var ServerLock sync.RWMutex
|
||||
|
||||
var SortedServerList []*model.Server
|
||||
var SortedServerLock sync.RWMutex
|
||||
|
||||
var Version = "v0.2.4"
|
||||
|
||||
func ReSortServer() {
|
||||
ServerLock.RLock()
|
||||
defer ServerLock.RUnlock()
|
||||
SortedServerLock.Lock()
|
||||
defer SortedServerLock.Unlock()
|
||||
|
||||
SortedServerList = []*model.Server{}
|
||||
for _, s := range ServerList {
|
||||
SortedServerList = append(SortedServerList, s)
|
||||
|
Loading…
Reference in New Issue
Block a user