agent: add lock for gatherStats

This commit is contained in:
henrygd
2025-02-21 00:20:41 -05:00
parent 1fb60e05d7
commit 96f9128d1a

View File

@@ -8,11 +8,13 @@ import (
"log/slog" "log/slog"
"os" "os"
"strings" "strings"
"sync"
"github.com/shirou/gopsutil/v4/common" "github.com/shirou/gopsutil/v4/common"
) )
type Agent struct { type Agent struct {
sync.Mutex // Used to lock agent while collecting data
debug bool // true if LOG_LEVEL is set to debug debug bool // true if LOG_LEVEL is set to debug
zfs bool // true if system has arcstats zfs bool // true if system has arcstats
memCalc string // Memory calculation formula memCalc string // Memory calculation formula
@@ -99,6 +101,8 @@ func GetEnv(key string) (value string, exists bool) {
} }
func (a *Agent) gatherStats() system.CombinedData { func (a *Agent) gatherStats() system.CombinedData {
a.Lock()
defer a.Unlock()
slog.Debug("Getting stats") slog.Debug("Getting stats")
systemData := system.CombinedData{ systemData := system.CombinedData{
Stats: a.getSystemStats(), Stats: a.getSystemStats(),