From 03774bbba6321c0d1f5ae0b604d3b063a20317f7 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 27 Nov 2023 17:42:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A6=82=E8=A7=88=E9=A1=B5=E7=A3=81?= =?UTF-8?q?=E7=9B=98=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4=20(#3074)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #2825 --- backend/app/service/dashboard.go | 52 +++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/backend/app/service/dashboard.go b/backend/app/service/dashboard.go index 0a37ecfda..315068851 100644 --- a/backend/app/service/dashboard.go +++ b/backend/app/service/dashboard.go @@ -1,9 +1,11 @@ package service import ( + "context" "encoding/json" "fmt" "strings" + "sync" "time" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -173,7 +175,7 @@ type diskInfo struct { func loadDiskInfo() []dto.DiskInfo { var datas []dto.DiskInfo - stdout, err := cmd.Exec("df -hT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev") + stdout, err := cmd.ExecWithTimeOut("df -hT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev", 2*time.Second) if err != nil { return datas } @@ -207,24 +209,38 @@ func loadDiskInfo() []dto.DiskInfo { mounts = append(mounts, diskInfo{Type: fields[1], Device: fields[0], Mount: fields[6]}) } + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + var wg sync.WaitGroup + wg.Add(len(mounts)) for i := 0; i < len(mounts); i++ { - state, err := disk.Usage(mounts[i].Mount) - if err != nil { - continue - } - var itemData dto.DiskInfo - itemData.Path = mounts[i].Mount - itemData.Type = mounts[i].Type - itemData.Device = mounts[i].Device - itemData.Total = state.Total - itemData.Free = state.Free - itemData.Used = state.Used - itemData.UsedPercent = state.UsedPercent - itemData.InodesTotal = state.InodesTotal - itemData.InodesUsed = state.InodesUsed - itemData.InodesFree = state.InodesFree - itemData.InodesUsedPercent = state.InodesUsedPercent - datas = append(datas, itemData) + go func(index int) { + defer wg.Done() + select { + case <-ctx.Done(): + global.LOG.Errorf("load disk info from %s failed, err: timeout", mounts[index].Mount) + return + default: + state, err := disk.Usage(mounts[index].Mount) + if err != nil { + return + } + var itemData dto.DiskInfo + itemData.Path = mounts[index].Mount + itemData.Type = mounts[index].Type + itemData.Device = mounts[index].Device + itemData.Total = state.Total + itemData.Free = state.Free + itemData.Used = state.Used + itemData.UsedPercent = state.UsedPercent + itemData.InodesTotal = state.InodesTotal + itemData.InodesUsed = state.InodesUsed + itemData.InodesFree = state.InodesFree + itemData.InodesUsedPercent = state.InodesUsedPercent + datas = append(datas, itemData) + } + }(i) } + wg.Wait() return datas }