fix: 概览页磁盘显示优化 (#3096)

release-1.8
ssongliu 1 year ago committed by wanghe-fit2cloud
parent 03774bbba6
commit 71869b7264

@ -1,9 +1,9 @@
package service package service
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -209,26 +209,25 @@ func loadDiskInfo() []dto.DiskInfo {
mounts = append(mounts, diskInfo{Type: fields[1], Device: fields[0], Mount: fields[6]}) 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 var wg sync.WaitGroup
wg.Add(len(mounts)) wg.Add(len(mounts))
for i := 0; i < len(mounts); i++ { for i := 0; i < len(mounts); i++ {
go func(index int) { go func(timeoutCh <-chan time.Time, mount diskInfo) {
defer wg.Done() defer wg.Done()
var itemData dto.DiskInfo
itemData.Path = mount.Mount
itemData.Type = mount.Type
itemData.Device = mount.Device
select { select {
case <-ctx.Done(): case <-timeoutCh:
global.LOG.Errorf("load disk info from %s failed, err: timeout", mounts[index].Mount) datas = append(datas, itemData)
return global.LOG.Errorf("load disk info from %s failed, err: timeout", mount.Mount)
default: default:
state, err := disk.Usage(mounts[index].Mount) state, err := disk.Usage(mount.Mount)
if err != nil { if err != nil {
return 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.Total = state.Total
itemData.Free = state.Free itemData.Free = state.Free
itemData.Used = state.Used itemData.Used = state.Used
@ -239,8 +238,12 @@ func loadDiskInfo() []dto.DiskInfo {
itemData.InodesUsedPercent = state.InodesUsedPercent itemData.InodesUsedPercent = state.InodesUsedPercent
datas = append(datas, itemData) datas = append(datas, itemData)
} }
}(i) }(time.After(5*time.Second), mounts[i])
} }
wg.Wait() wg.Wait()
sort.Slice(datas, func(i, j int) bool {
return datas[i].Path < datas[j].Path
})
return datas return datas
} }

Loading…
Cancel
Save