fix data race for system info

This commit is contained in:
yusing 2025-02-25 04:29:17 +08:00
parent 767560804d
commit 8114b04ab6

View file

@ -9,7 +9,6 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/shirou/gopsutil/v4/cpu" "github.com/shirou/gopsutil/v4/cpu"
@ -97,12 +96,6 @@ var allQueries = []string{
var Poller = period.NewPoller("system_info", getSystemInfo, aggregate) var Poller = period.NewPoller("system_info", getSystemInfo, aggregate)
var bufPool = sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, 1024))
},
}
func init() { func init() {
Poller.Start() Poller.Start()
} }
@ -293,9 +286,7 @@ func (s *SystemInfo) collectSensorsInfo(ctx context.Context) error {
// explicitly implement MarshalJSON to avoid reflection // explicitly implement MarshalJSON to avoid reflection
func (s *SystemInfo) MarshalJSON() ([]byte, error) { func (s *SystemInfo) MarshalJSON() ([]byte, error) {
b := bufPool.Get().(*bytes.Buffer) b := bytes.NewBuffer(make([]byte, 0, 1024))
b.Reset()
defer bufPool.Put(b)
b.WriteRune('{') b.WriteRune('{')
@ -561,9 +552,7 @@ func aggregate(entries []*SystemInfo, query url.Values) (total int, result Aggre
} }
func (result Aggregated) MarshalJSON() ([]byte, error) { func (result Aggregated) MarshalJSON() ([]byte, error) {
buf := bufPool.Get().(*bytes.Buffer) buf := bytes.NewBuffer(make([]byte, 0, 1024))
buf.Reset()
defer bufPool.Put(buf)
buf.WriteByte('[') buf.WriteByte('[')
i := 0 i := 0