Skip to content

Commit

Permalink
Merge pull request #829 from Icinga/use-builtin-atomic-types
Browse files Browse the repository at this point in the history
Replace `int64` with `atomic.Int64` where applicable
  • Loading branch information
yhabteab authored Oct 24, 2024
2 parents 1564a03 + a64a652 commit 47d2cde
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/icingadb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func run() int {
})

syncStart := time.Now()
atomic.StoreInt64(&telemetry.OngoingSyncStartMilli, syncStart.UnixMilli())
telemetry.OngoingSyncStartMilli.Store(syncStart.UnixMilli())

logger.Info("Starting config sync")
for _, factory := range v1.ConfigFactories {
Expand Down Expand Up @@ -244,7 +244,7 @@ func run() int {

g.Go(func() error {
configInitSync.Wait()
atomic.StoreInt64(&telemetry.OngoingSyncStartMilli, 0)
telemetry.OngoingSyncStartMilli.Store(0)

syncEnd := time.Now()
elapsed := syncEnd.Sub(syncStart)
Expand Down
8 changes: 4 additions & 4 deletions pkg/icingaredis/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Timeout = time.Minute
type Heartbeat struct {
active bool
events chan *HeartbeatMessage
lastReceivedMs int64
lastReceivedMs atomic.Int64
cancelCtx context.CancelFunc
client *redis.Client
done chan struct{}
Expand Down Expand Up @@ -59,7 +59,7 @@ func (h *Heartbeat) Events() <-chan *HeartbeatMessage {

// LastReceived returns the last heartbeat's receive time in ms.
func (h *Heartbeat) LastReceived() int64 {
return atomic.LoadInt64(&h.lastReceivedMs)
return h.lastReceivedMs.Load()
}

// Close stops the heartbeat controller loop, waits for it to finish, and returns an error if any.
Expand Down Expand Up @@ -138,7 +138,7 @@ func (h *Heartbeat) controller(ctx context.Context) {
h.active = true
}

atomic.StoreInt64(&h.lastReceivedMs, m.received.UnixMilli())
h.lastReceivedMs.Store(m.received.UnixMilli())
h.sendEvent(m)
case <-time.After(Timeout):
if h.active {
Expand All @@ -149,7 +149,7 @@ func (h *Heartbeat) controller(ctx context.Context) {
h.logger.Warn("Waiting for Icinga heartbeat")
}

atomic.StoreInt64(&h.lastReceivedMs, 0)
h.lastReceivedMs.Store(0)
case <-ctx.Done():
return ctx.Err()
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/icingaredis/telemetry/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func GetCurrentDbConnErr() (string, int64) {
}

// OngoingSyncStartMilli is to be updated by the main() function.
var OngoingSyncStartMilli int64
var OngoingSyncStartMilli atomic.Int64

var boolToStr = map[bool]string{false: "0", true: "1"}
var startTime = time.Now().UnixMilli()
Expand All @@ -100,7 +100,7 @@ func StartHeartbeat(
periodic.Start(ctx, interval, func(tick periodic.Tick) {
heartbeat := heartbeat.LastReceived()
responsibleTsMilli, responsible, otherResponsible := ha.State()
ongoingSyncStart := atomic.LoadInt64(&OngoingSyncStartMilli)
ongoingSyncStart := OngoingSyncStartMilli.Load()
lastSync := syncStats.Load()
dbConnErr, dbConnErrSinceMilli := GetCurrentDbConnErr()
now := time.Now()
Expand Down

0 comments on commit 47d2cde

Please sign in to comment.