Skip to content

Commit d72f88c

Browse files
author
evelynwei
committed
fix: reporter push action data race bugfix
1 parent 9c68721 commit d72f88c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

plugin/metrics/prometheus/reporter.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net"
2525
"net/http"
2626
"sync"
27+
"sync/atomic"
2728
"time"
2829

2930
"github.com/prometheus/client_golang/prometheus"
@@ -212,6 +213,7 @@ func (s *PrometheusReporter) prepare() {
212213

213214
// Info 插件信息.
214215
func (s *PrometheusReporter) Info() model.StatInfo {
216+
s.prepare() // 确保 action 已初始化,避免并发读写竞态
215217
if s.action == nil {
216218
return model.StatInfo{}
217219
}
@@ -264,7 +266,7 @@ type PullAction struct {
264266
cfg *Config
265267
clientIP string
266268
bindIP string
267-
bindPort int32
269+
bindPort atomic.Int32 // 使用 atomic 保证并发安全
268270
ln net.Listener
269271
}
270272

@@ -279,7 +281,7 @@ func (pa *PullAction) Init(initCtx *plugin.InitContext, reporter *PrometheusRepo
279281
if len(pa.cfg.IP) != 0 {
280282
pa.bindIP = pa.cfg.IP
281283
}
282-
pa.bindPort = int32(pa.cfg.port)
284+
pa.bindPort.Store(int32(pa.cfg.port))
283285
}
284286

285287
func (pa *PullAction) Close() {
@@ -317,24 +319,24 @@ func (pa *PullAction) doAggregation(ctx context.Context) {
317319
}
318320

319321
func (pa *PullAction) Run(ctx context.Context) {
320-
if pa.bindPort < 0 {
322+
if pa.bindPort.Load() < 0 {
321323
return
322324
}
323325
go pa.doAggregation(ctx)
324326
go func() {
325-
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", pa.bindIP, pa.bindPort))
327+
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", pa.bindIP, pa.bindPort.Load()))
326328
if err != nil {
327329
log.GetBaseLogger().Errorf("[metrics][push] start metrics http-server fail: %v", err)
328-
pa.bindPort = -1
330+
pa.bindPort.Store(-1)
329331
return
330332
}
331333
pa.ln = ln
332-
pa.bindPort = int32(ln.Addr().(*net.TCPAddr).Port)
334+
pa.bindPort.Store(int32(ln.Addr().(*net.TCPAddr).Port))
333335
handler := metricsHttpHandler{
334336
handler: promhttp.HandlerFor(pa.reporter.registry, promhttp.HandlerOpts{}),
335337
}
336338

337-
log.GetBaseLogger().Infof("[metrics][push] start metrics http-server address : %s", fmt.Sprintf("%s:%d", pa.bindIP, pa.bindPort))
339+
log.GetBaseLogger().Infof("[metrics][push] start metrics http-server address : %s", fmt.Sprintf("%s:%d", pa.bindIP, pa.bindPort.Load()))
338340
if err := http.Serve(ln, &handler); err != nil {
339341
log.GetBaseLogger().Errorf("[metrics][push] start metrics http-server fail : %s", err)
340342
return
@@ -344,12 +346,13 @@ func (pa *PullAction) Run(ctx context.Context) {
344346

345347
// Info 插件信息.
346348
func (pa *PullAction) Info() model.StatInfo {
347-
if pa.bindPort <= 0 {
349+
port := pa.bindPort.Load()
350+
if port <= 0 {
348351
return model.StatInfo{}
349352
}
350353
return model.StatInfo{
351354
Target: PluginName,
352-
Port: uint32(pa.bindPort),
355+
Port: uint32(port),
353356
Path: "/metrics",
354357
Protocol: "http",
355358
}

0 commit comments

Comments
 (0)