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 <[email protected]>
  • Loading branch information
kke committed May 22, 2024
1 parent 1b2fa89 commit f6e74fc
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,27 +32,26 @@ type LeasePool struct {

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

acquiredLeaseCallbacks []func()
lostLeaseCallbacks []func()
}

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) *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,
}
}

Expand Down Expand Up @@ -118,5 +117,5 @@ func (l *LeasePool) Stop() error {
}

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

0 comments on commit f6e74fc

Please sign in to comment.