Skip to content

Commit

Permalink
fix: data race bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Jul 30, 2024
1 parent 75fd628 commit d75c0b1
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"runtime"
"strconv"
"sync/atomic"
"text/template"
"time"

Expand Down Expand Up @@ -176,21 +177,25 @@ type statsEntity struct {
var memstats = &statsEntity{Stats: &runtime.MemStats{}}

type StatsMgr struct {
last int64
Ctx context.Context
Cancel context.CancelFunc
last atomic.Int64
ctx context.Context
cancel context.CancelFunc
}

func NewStatsMgr(ctx context.Context) *StatsMgr {
s := &StatsMgr{}
s.Ctx, s.Cancel = context.WithCancel(ctx)
s.ctx, s.cancel = context.WithCancel(ctx)
go s.polling()

return s
}

func (s *StatsMgr) Tick() {
s.last = time.Now().Unix() + int64(float64(Interval())/1000.0)*2
s.last.Store(time.Now().Unix() + int64(float64(Interval())/1000.0)*2)
}

func (s *StatsMgr) Close() {
s.cancel()
}

func (s *StatsMgr) polling() {
Expand All @@ -200,11 +205,11 @@ func (s *StatsMgr) polling() {
for {
select {
case <-ticker.C:
if s.last > time.Now().Unix() {
if s.last.Load() > time.Now().Unix() {
runtime.ReadMemStats(memstats.Stats)
memstats.T = time.Now().Format(defaultCfg.TimeFormat)
}
case <-s.Ctx.Done():
case <-s.ctx.Done():
return
}
}
Expand Down Expand Up @@ -261,7 +266,9 @@ func newBasicView(route string) *charts.Line {
Theme: string(defaultCfg.Theme),
}),
)
graph.SetXAxis([]string{}).SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{Smooth: opts.Bool(true)}))
graph.SetXAxis([]string{}).SetSeriesOptions(
charts.WithLineChartOpts(opts.LineChart{Smooth: opts.Bool(true)}),
)
graph.AddJSFuncs(genViewTemplate(graph.ChartID, route))
return graph
}

0 comments on commit d75c0b1

Please sign in to comment.