Merge pull request #857 from Gui13/fix_837

Do not cache boot time for linux. Fix #837
This commit is contained in:
Lomanic 2020-05-17 22:47:08 +02:00 committed by GitHub
commit c89193f22d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 12 deletions

View File

@ -10,7 +10,6 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
"time" "time"
) )
@ -54,14 +53,7 @@ func NumProcs() (uint64, error) {
return cnt, nil return cnt, nil
} }
// cachedBootTime must be accessed via atomic.Load/StoreUint64
var cachedBootTime uint64
func BootTimeWithContext(ctx context.Context) (uint64, error) { func BootTimeWithContext(ctx context.Context) (uint64, error) {
t := atomic.LoadUint64(&cachedBootTime)
if t != 0 {
return t, nil
}
system, role, err := Virtualization() system, role, err := Virtualization()
if err != nil { if err != nil {
@ -94,8 +86,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
t = uint64(b) t := uint64(b)
atomic.StoreUint64(&cachedBootTime, t)
return t, nil return t, nil
} }
} }
@ -108,8 +99,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
t = uint64(time.Now().Unix()) - uint64(b) t := uint64(time.Now().Unix()) - uint64(b)
atomic.StoreUint64(&cachedBootTime, t)
return t, nil return t, nil
} }