From 277c95057bc2bf9327a0dcfe910c2382d59eeef7 Mon Sep 17 00:00:00 2001 From: AtakanColak Date: Mon, 9 Nov 2020 10:50:09 +0300 Subject: [PATCH] tidy up loop control and mutex --- load/load_windows.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/load/load_windows.go b/load/load_windows.go index 73f7926..5f3c695 100644 --- a/load/load_windows.go +++ b/load/load_windows.go @@ -35,28 +35,21 @@ func loadAvgGoroutine() { ) counter, err := common.ProcessorQueueLengthCounter() - loadErr = err if err != nil || counter == nil { - loadAvgMutex.Unlock() - log.Println("unexpected processor queue length counter error, please file an issue on github") + log.Println("gopsutil: unexpected processor queue length counter error, please file an issue on github") return } tick := time.NewTicker(samplingFrequency).C for { - currentLoad, loadErr = counter.GetValue() - if loadErr != nil { - goto SKIP - } - + currentLoad, err = counter.GetValue() + loadAvgMutex.Lock() + loadErr = err loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M) loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M) loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M) - - SKIP: loadAvgMutex.Unlock() <-tick - loadAvgMutex.Lock() } } @@ -67,7 +60,6 @@ func Avg() (*AvgStat, error) { func AvgWithContext(ctx context.Context) (*AvgStat, error) { loadAvgGoroutineOnce.Do(func() { - loadAvgMutex.Lock() // lock to be unlocked by loadAvgGoroutine go loadAvgGoroutine() }) loadAvgMutex.RLock()