From 76e7632b1b53596508628352a542d210298283bb Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 18 Sep 2024 11:23:41 +0200 Subject: [PATCH] `HA`: Use `sync/atomic#Pointer` instead of our own wrapper Go 1.19 introduced `sync/atomic#Pointer` among other things, so we no longer need to use the Atomic wrapper from our Icinga Go library. --- pkg/icingadb/ha.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/icingadb/ha.go b/pkg/icingadb/ha.go index 6460ac32d..b78f1fefc 100644 --- a/pkg/icingadb/ha.go +++ b/pkg/icingadb/ha.go @@ -7,7 +7,6 @@ import ( "encoding/hex" "github.com/google/uuid" "github.com/icinga/icinga-go-library/backoff" - "github.com/icinga/icinga-go-library/com" "github.com/icinga/icinga-go-library/database" "github.com/icinga/icinga-go-library/logging" "github.com/icinga/icinga-go-library/retry" @@ -19,6 +18,7 @@ import ( "github.com/pkg/errors" "go.uber.org/zap" "sync" + "sync/atomic" "time" ) @@ -35,7 +35,7 @@ type haState struct { // HA provides high availability and indicates whether a Takeover or Handover must be made. type HA struct { - state com.Atomic[haState] + state atomic.Pointer[haState] ctx context.Context cancelCtx context.CancelFunc instanceId types.Binary @@ -121,7 +121,8 @@ func (h *HA) Takeover() chan string { // State returns the status quo. func (h *HA) State() (responsibleTsMilli int64, responsible, otherResponsible bool) { - state, _ := h.state.Load() + state := h.state.Load() + return state.responsibleTsMilli, state.responsible, state.otherResponsible } @@ -428,7 +429,7 @@ func (h *HA) realize( h.signalTakeover(takeover) } else if otherResponsible { - if state, _ := h.state.Load(); !state.otherResponsible { + if state := h.state.Load(); !state.otherResponsible { state.otherResponsible = true h.state.Store(state) } @@ -496,7 +497,7 @@ func (h *HA) removeOldInstances(s *icingaredisv1.IcingaStatus, envId types.Binar // signalHandover gives up HA.responsible and notifies the HA.Handover chan. func (h *HA) signalHandover(reason string) { if h.responsible { - h.state.Store(haState{ + h.state.Store(&haState{ responsibleTsMilli: time.Now().UnixMilli(), responsible: false, otherResponsible: false, @@ -514,7 +515,7 @@ func (h *HA) signalHandover(reason string) { // signalTakeover claims HA.responsible and notifies the HA.Takeover chan. func (h *HA) signalTakeover(reason string) { if !h.responsible { - h.state.Store(haState{ + h.state.Store(&haState{ responsibleTsMilli: time.Now().UnixMilli(), responsible: true, otherResponsible: false,