Skip to content

Commit

Permalink
HA: Use sync/atomic#Pointer instead of our own wrapper
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lippserd committed Sep 19, 2024
1 parent 5e81041 commit 76e7632
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pkg/icingadb/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/pkg/errors"
"go.uber.org/zap"
"sync"
"sync/atomic"
"time"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 76e7632

Please sign in to comment.