Do not cache boot time for linux. Fix #837

This commit is contained in:
Guillaume Bienkowski 2020-04-15 13:51:31 +02:00
parent 53cec6b37e
commit aeb3b6b1c7
1 changed files with 2 additions and 12 deletions

View File

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