diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index 7198032..e1552a1 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -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 } diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index 4231a85..41d0f59 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -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, diff --git a/cmd/dashboard/controller/member_page.go b/cmd/dashboard/controller/member_page.go index 3fdb698..e155557 100644 --- a/cmd/dashboard/controller/member_page.go +++ b/cmd/dashboard/controller/member_page.go @@ -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, diff --git a/cmd/dashboard/rpc/rpc.go b/cmd/dashboard/rpc/rpc.go index 4b3928c..2aaf9fb 100644 --- a/cmd/dashboard/rpc/rpc.go +++ b/cmd/dashboard/rpc/rpc.go @@ -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))) } } diff --git a/service/dao/dao.go b/service/dao/dao.go index 69b96d0..3724af6 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -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)