fix potential race (#86)
This commit is contained in:
parent
af41e4d843
commit
32481e2b6f
@ -16,6 +16,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/blang/semver"
|
"github.com/blang/semver"
|
||||||
@ -66,6 +67,8 @@ var (
|
|||||||
Timeout: time.Second * 30,
|
Timeout: time.Second * 30,
|
||||||
Transport: &http3.RoundTripper{},
|
Transport: &http3.RoundTripper{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hostStatus = new(atomic.Bool)
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -423,7 +426,7 @@ func doTask(task *pb.Task) {
|
|||||||
handleNATTask(task)
|
handleNATTask(task)
|
||||||
return
|
return
|
||||||
case model.TaskTypeReportHostInfo:
|
case model.TaskTypeReportHostInfo:
|
||||||
reportState(time.Time{})
|
reportHost()
|
||||||
return
|
return
|
||||||
case model.TaskTypeFM:
|
case model.TaskTypeFM:
|
||||||
handleFMTask(task)
|
handleFMTask(task)
|
||||||
@ -459,10 +462,25 @@ func reportState(lastReportHostInfo time.Time) time.Time {
|
|||||||
printf("reportState error: %v", err)
|
printf("reportState error: %v", err)
|
||||||
time.Sleep(delayWhenError)
|
time.Sleep(delayWhenError)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每10分钟重新获取一次硬件信息
|
// 每10分钟重新获取一次硬件信息
|
||||||
if lastReportHostInfo.Before(time.Now().Add(-10*time.Minute)) || monitor.GeoQueryIPChanged {
|
if lastReportHostInfo.Before(time.Now().Add(-10*time.Minute)) || monitor.GeoQueryIPChanged {
|
||||||
|
if reportHost() {
|
||||||
monitor.GeoQueryIPChanged = false
|
monitor.GeoQueryIPChanged = false
|
||||||
lastReportHostInfo = time.Now()
|
lastReportHostInfo = time.Now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lastReportHostInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func reportHost() bool {
|
||||||
|
if !hostStatus.CompareAndSwap(false, true) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer hostStatus.Store(false)
|
||||||
|
|
||||||
|
if client != nil && initialized {
|
||||||
client.ReportSystemInfo(context.Background(), monitor.GetHost().PB())
|
client.ReportSystemInfo(context.Background(), monitor.GetHost().PB())
|
||||||
if monitor.GeoQueryIP != "" {
|
if monitor.GeoQueryIP != "" {
|
||||||
geoip, err := client.LookupGeoIP(context.Background(), &pb.GeoIP{Ip: monitor.GeoQueryIP})
|
geoip, err := client.LookupGeoIP(context.Background(), &pb.GeoIP{Ip: monitor.GeoQueryIP})
|
||||||
@ -471,8 +489,8 @@ func reportState(lastReportHostInfo time.Time) time.Time {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return lastReportHostInfo
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程
|
// doSelfUpdate 执行更新检查 如果更新成功则会结束进程
|
||||||
|
@ -57,7 +57,7 @@ var statDataFetchAttempts = map[uint8]uint8{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
updateTempStatus = new(atomic.Int32)
|
updateTempStatus = new(atomic.Bool)
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitConfig(cfg *model.AgentConfig) {
|
func InitConfig(cfg *model.AgentConfig) {
|
||||||
@ -236,10 +236,10 @@ func getConns() (tcpConnCount, udpConnCount uint64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateTemperatureStat() {
|
func updateTemperatureStat() {
|
||||||
if !updateTempStatus.CompareAndSwap(0, 1) {
|
if !updateTempStatus.CompareAndSwap(false, true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer updateTempStatus.Store(0)
|
defer updateTempStatus.Store(false)
|
||||||
|
|
||||||
stat := tryStat(context.Background(), Temperatures, temperature.GetState)
|
stat := tryStat(context.Background(), Temperatures, temperature.GetState)
|
||||||
temperatureStat = stat
|
temperatureStat = stat
|
||||||
|
Loading…
x
Reference in New Issue
Block a user