优化代码

This commit is contained in:
GoEdgeLab
2023-08-15 15:49:23 +08:00
parent 98f33485c9
commit 42ebb4514d
4 changed files with 16 additions and 16 deletions

View File

@@ -890,7 +890,7 @@ func (this *FileStorage) Stop() {
// TotalDiskSize 消耗的磁盘尺寸 // TotalDiskSize 消耗的磁盘尺寸
func (this *FileStorage) TotalDiskSize() int64 { func (this *FileStorage) TotalDiskSize() int64 {
stat, err := fsutils.StatCache(this.options.Dir) stat, err := fsutils.StatDeviceCache(this.options.Dir)
if err == nil { if err == nil {
return int64(stat.UsedSize()) return int64(stat.UsedSize())
} }
@@ -1434,14 +1434,14 @@ func (this *FileStorage) checkDiskSpace() {
} }
if options != nil && len(options.Dir) > 0 { if options != nil && len(options.Dir) > 0 {
stat, err := fsutils.Stat(options.Dir) stat, err := fsutils.StatDevice(options.Dir)
if err == nil { if err == nil {
this.mainDiskIsFull = stat.FreeSize() < minFreeSize this.mainDiskIsFull = stat.FreeSize() < minFreeSize
} }
} }
var subDirs = this.subDirs // copy slice var subDirs = this.subDirs // copy slice
for _, subDir := range subDirs { for _, subDir := range subDirs {
stat, err := fsutils.Stat(subDir.Path) stat, err := fsutils.StatDevice(subDir.Path)
if err == nil { if err == nil {
subDir.IsFull = stat.FreeSize() < minFreeSize subDir.IsFull = stat.FreeSize() < minFreeSize
} }

View File

@@ -280,7 +280,7 @@ func (this *NodeStatusExecutor) updateCacheSpace(status *nodeconfigs.NodeStatus)
var result = []maps.Map{} var result = []maps.Map{}
var cachePaths = caches.SharedManager.FindAllCachePaths() var cachePaths = caches.SharedManager.FindAllCachePaths()
for _, path := range cachePaths { for _, path := range cachePaths {
stat, err := fsutils.Stat(path) stat, err := fsutils.StatDevice(path)
if err != nil { if err != nil {
return return
} }

View File

@@ -8,8 +8,8 @@ import (
"sync" "sync"
) )
// Stat device contains the path // StatDevice device contains the path
func Stat(path string) (*StatResult, error) { func StatDevice(path string) (*StatResult, error) {
var stat = &unix.Statfs_t{} var stat = &unix.Statfs_t{}
err := unix.Statfs(path, stat) err := unix.Statfs(path, stat)
if err != nil { if err != nil {
@@ -23,8 +23,8 @@ var cacheMap = map[string]*StatResult{} // path => StatResult
const cacheLife = 3 // seconds const cacheLife = 3 // seconds
// StatCache stat device with cache // StatDeviceCache stat device with cache
func StatCache(path string) (*StatResult, error) { func StatDeviceCache(path string) (*StatResult, error) {
locker.RLock() locker.RLock()
stat, ok := cacheMap[path] stat, ok := cacheMap[path]
if ok && stat.updatedAt >= fasttime.Now().Unix()-cacheLife { if ok && stat.updatedAt >= fasttime.Now().Unix()-cacheLife {
@@ -36,7 +36,7 @@ func StatCache(path string) (*StatResult, error) {
locker.Lock() locker.Lock()
defer locker.Unlock() defer locker.Unlock()
stat, err := Stat(path) stat, err := StatDevice(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -10,7 +10,7 @@ import (
) )
func TestStat(t *testing.T) { func TestStat(t *testing.T) {
stat, err := fsutils.Stat("/usr/local") stat, err := fsutils.StatDevice("/usr/local")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -19,7 +19,7 @@ func TestStat(t *testing.T) {
func TestStatCache(t *testing.T) { func TestStatCache(t *testing.T) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
stat, err := fsutils.StatCache("/usr/local") stat, err := fsutils.StatDeviceCache("/usr/local")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -40,16 +40,16 @@ func TestConcurrent(t *testing.T) {
go func() { go func() {
defer wg.Done() defer wg.Done()
_, _ = fsutils.Stat("/usr/local") _, _ = fsutils.StatDevice("/usr/local")
}() }()
} }
wg.Wait() wg.Wait()
} }
func BenchmarkStat(b *testing.B) { func BenchmarkStatDevice(b *testing.B) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
_, err := fsutils.Stat("/usr/local") _, err := fsutils.StatDevice("/usr/local")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
@@ -57,10 +57,10 @@ func BenchmarkStat(b *testing.B) {
}) })
} }
func BenchmarkStatCache(b *testing.B) { func BenchmarkStatCacheDevice(b *testing.B) {
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
_, err := fsutils.StatCache("/usr/local") _, err := fsutils.StatDeviceCache("/usr/local")
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }