diff --git a/backend/app/service/dashboard.go b/backend/app/service/dashboard.go index 68adf8831..d81761fc1 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" @@ -179,7 +181,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 } @@ -213,24 +215,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 } diff --git a/frontend/src/views/home/index.vue b/frontend/src/views/home/index.vue index 62ae1b246..b1414d2d3 100644 --- a/frontend/src/views/home/index.vue +++ b/frontend/src/views/home/index.vue @@ -393,7 +393,7 @@ const onLoadBaseInfo = async (isInit: boolean, range: string) => { if (isActive.value && !globalStore.isOnRestart) { await onLoadCurrentInfo(); } - }, 300000); + }, 3000); } };