diff --git a/README.md b/README.md
index 789c2c2..ced720e 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
LOGO designed by 熊大 .
-
+
:trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。
diff --git a/cmd/dashboard/controller/common_page.go b/cmd/dashboard/controller/common_page.go index 6594d04..707fc78 100644 --- a/cmd/dashboard/controller/common_page.go +++ b/cmd/dashboard/controller/common_page.go @@ -12,6 +12,7 @@ import ( "github.com/gorilla/websocket" "github.com/hashicorp/go-uuid" "golang.org/x/crypto/bcrypt" + "golang.org/x/sync/singleflight" "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/mygin" @@ -32,6 +33,7 @@ type commonPage struct { r *gin.Engine terminals map[string]*terminalContext terminalsLock *sync.Mutex + requestGroup singleflight.Group } func (cp *commonPage) serve() { @@ -108,11 +110,32 @@ func (p *commonPage) service(c *gin.Context) { })) } +func (cp *commonPage) getServerStat() ([]byte, error) { + v, err, _ := cp.requestGroup.Do("serverStats", func() (any, error) { + singleton.SortedServerLock.RLock() + defer singleton.SortedServerLock.RUnlock() + return utils.Json.Marshal(Data{ + Now: time.Now().Unix() * 1000, + Servers: singleton.SortedServerList, + }) + }) + return v.([]byte), err +} + func (cp *commonPage) home(c *gin.Context) { - singleton.SortedServerLock.RLock() - defer singleton.SortedServerLock.RUnlock() + stat, err := cp.getServerStat() + if err != nil { + mygin.ShowErrorPage(c, mygin.ErrInfo{ + Code: http.StatusInternalServerError, + Title: "系统错误", + Msg: "服务器状态获取失败", + Link: "/", + Btn: "返回首页", + }, true) + return + } c.HTML(http.StatusOK, "theme-"+singleton.Conf.Site.Theme+"/home", mygin.CommonEnvironment(c, gin.H{ - "Servers": singleton.SortedServerList, + "Servers": string(stat), "CustomCode": singleton.Conf.Site.CustomCode, })) } @@ -140,23 +163,17 @@ func (cp *commonPage) ws(c *gin.Context) { return } defer conn.Close() - var bytesToWrite []byte count := 0 for { - singleton.SortedServerLock.RLock() - bytesToWrite, err = utils.Json.Marshal(Data{ - Now: time.Now().Unix() * 1000, - Servers: singleton.SortedServerList, - }) - singleton.SortedServerLock.RUnlock() + stat, err := cp.getServerStat() if err != nil { - break + continue } writer, err := conn.NextWriter(websocket.TextMessage) if err != nil { break } - _, err = writer.Write(bytesToWrite) + _, err = writer.Write(stat) if err != nil { break } diff --git a/go.mod b/go.mod index 193ef92..ea1d158 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/stretchr/testify v1.7.1 golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a + golang.org/x/sync v0.0.0-20210220032951-036812b2e83c google.golang.org/grpc v1.45.0 google.golang.org/protobuf v1.27.1 gopkg.in/yaml.v2 v2.4.0 @@ -75,7 +76,6 @@ require ( github.com/ulikunitz/xz v0.5.10 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect - golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect golang.org/x/text v0.3.7 // indirect diff --git a/resource/template/theme-daynight/home.html b/resource/template/theme-daynight/home.html index a24b9bf..e4e38a4 100644 --- a/resource/template/theme-daynight/home.html +++ b/resource/template/theme-daynight/home.html @@ -169,7 +169,7 @@ -{{end}} +{{end}} \ No newline at end of file diff --git a/resource/template/theme-hotaru/home.html b/resource/template/theme-hotaru/home.html index e267307..f078eef 100644 --- a/resource/template/theme-hotaru/home.html +++ b/resource/template/theme-hotaru/home.html @@ -151,7 +151,7 @@