From a64a6527977be683360b9335a629c989d615459d Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Thu, 24 Oct 2024 14:42:05 +0200 Subject: [PATCH] Replace `int64` with `atomic.Int64` where applicable Instead of declaring a field as `int64` and using helper functions for atomic operations, it is better and recommended to use the new atomic types like `atomic.Int64`. --- cmd/icingadb/main.go | 4 ++-- pkg/icingaredis/heartbeat.go | 8 ++++---- pkg/icingaredis/telemetry/heartbeat.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/icingadb/main.go b/cmd/icingadb/main.go index 4c0bb8f7e..de2cbdcfc 100644 --- a/cmd/icingadb/main.go +++ b/cmd/icingadb/main.go @@ -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 { @@ -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) diff --git a/pkg/icingaredis/heartbeat.go b/pkg/icingaredis/heartbeat.go index 840445a23..bb1e83796 100644 --- a/pkg/icingaredis/heartbeat.go +++ b/pkg/icingaredis/heartbeat.go @@ -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{} @@ -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. @@ -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 { @@ -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() } diff --git a/pkg/icingaredis/telemetry/heartbeat.go b/pkg/icingaredis/telemetry/heartbeat.go index c29b15335..d7c4fcaa1 100644 --- a/pkg/icingaredis/telemetry/heartbeat.go +++ b/pkg/icingaredis/telemetry/heartbeat.go @@ -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() @@ -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()