Skip to content

Commit

Permalink
Use atomic.Bool instead of atomic.Value for leader status
Browse files Browse the repository at this point in the history
Lease pool's leader status field was an atomic.Value that is always used
for booleans only. This requires pre-initializing it through
Store(false) and also typecasting when retrieving.

By using atomic.Bool, the zero value is false and the value type is
always bool.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed May 22, 2024
1 parent a3bc75d commit 6282754
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/component/controller/leaderelector/leasepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type LeasePool struct {

invocationID string
stopCh chan struct{}
leaderStatus atomic.Value
leaderStatus atomic.Bool
kubeClientFactory kubeutil.ClientFactoryInterface
leaseCancel context.CancelFunc

Expand All @@ -41,19 +41,18 @@ type LeasePool struct {
name string
}

var _ Interface = (*LeasePool)(nil)
var _ manager.Component = (*LeasePool)(nil)
var (
_ Interface = (*LeasePool)(nil)
_ manager.Component = (*LeasePool)(nil)
)

// NewLeasePool creates a new leader elector using a Kubernetes lease pool.
func NewLeasePool(invocationID string, kubeClientFactory kubeutil.ClientFactoryInterface, name string) *LeasePool {
d := atomic.Value{}
d.Store(false)
return &LeasePool{
invocationID: invocationID,
stopCh: make(chan struct{}),
kubeClientFactory: kubeClientFactory,
log: logrus.WithFields(logrus.Fields{"component": "poolleaderelector"}),
leaderStatus: d,
name: name,
}
}
Expand Down Expand Up @@ -120,5 +119,5 @@ func (l *LeasePool) Stop() error {
}

func (l *LeasePool) IsLeader() bool {
return l.leaderStatus.Load().(bool)
return l.leaderStatus.Load()
}

0 comments on commit 6282754

Please sign in to comment.