diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index 80ead0d..13e7dc5 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -485,7 +485,7 @@ func (this *MemoryStorage) startFlush() { statCount = 0 // delay some time to reduce load if needed - if !fsutils.DiskIsFast { + if !fsutils.DiskIsFast() { loadStat, err := load.Avg() if err == nil && loadStat != nil { if loadStat.Load1 > 10 { diff --git a/internal/utils/fs/disk.go b/internal/utils/fs/disk.go index c063cef..3d24551 100644 --- a/internal/utils/fs/disk.go +++ b/internal/utils/fs/disk.go @@ -64,11 +64,21 @@ func CheckDiskIsFast() (speedMB float64, isFast bool, err error) { if err != nil { return } - isFast = speedMB > 120 + isFast = speedMB > 150 + + if speedMB > 1000 { + DiskSpeed = SpeedExtremelyFast + } else if speedMB > 150 { + DiskSpeed = SpeedFast + } else if speedMB > 60 { + DiskSpeed = SpeedLow + } else { + DiskSpeed = SpeedExtremelySlow + } + calculateDiskMaxWrites() if speedMB > DiskSpeedMB { DiskSpeedMB = speedMB - DiskIsFast = isFast } return diff --git a/internal/utils/fs/status.go b/internal/utils/fs/status.go index 9d3302d..5b769b3 100644 --- a/internal/utils/fs/status.go +++ b/internal/utils/fs/status.go @@ -8,9 +8,33 @@ import ( "time" ) +type Speed int + +func (this Speed) String() string { + switch this { + case SpeedExtremelyFast: + return "extremely fast" + case SpeedFast: + return "fast" + case SpeedLow: + return "low" + case SpeedExtremelySlow: + return "extremely slow" + } + return "unknown" +} + +const ( + SpeedExtremelyFast Speed = 1 + SpeedFast Speed = 2 + SpeedLow Speed = 3 + SpeedExtremelySlow Speed = 4 +) + var ( - DiskIsFast bool - DiskSpeedMB float64 + DiskSpeed = SpeedLow + DiskMaxWrites int32 = 32 + DiskSpeedMB float64 ) func init() { @@ -38,17 +62,14 @@ func init() { }() } +func DiskIsFast() bool { + return DiskSpeed == SpeedExtremelyFast || DiskSpeed == SpeedFast +} + var countWrites int32 = 0 -const MaxWrites = 32 -const MaxFastWrites = 128 - func WriteReady() bool { - var count = atomic.LoadInt32(&countWrites) - if DiskIsFast { - return count < MaxFastWrites - } - return count <= MaxWrites + return atomic.LoadInt32(&countWrites) < DiskMaxWrites } func WriteBegin() { @@ -58,3 +79,18 @@ func WriteBegin() { func WriteEnd() { atomic.AddInt32(&countWrites, -1) } + +func calculateDiskMaxWrites() { + switch DiskSpeed { + case SpeedExtremelyFast: + DiskMaxWrites = 256 + case SpeedFast: + DiskMaxWrites = 128 + case SpeedLow: + DiskMaxWrites = 32 + case SpeedExtremelySlow: + DiskMaxWrites = 16 + default: + DiskMaxWrites = 16 + } +} diff --git a/internal/utils/fs/status_test.go b/internal/utils/fs/status_test.go index c322984..2e30cff 100644 --- a/internal/utils/fs/status_test.go +++ b/internal/utils/fs/status_test.go @@ -11,7 +11,7 @@ import ( func TestWrites(t *testing.T) { var a = assert.NewAssertion(t) - for i := 0; i < fsutils.MaxWrites+1; i++ { + for i := 0; i < int(fsutils.DiskMaxWrites); i++ { fsutils.WriteBegin() } a.IsFalse(fsutils.WriteReady())