Make the powerlocker logic a little more idiomatic

This commit is contained in:
Dane Everitt 2022-01-30 11:46:27 -05:00
parent 11ae5e69ed
commit d7c7155802

View File

@ -73,10 +73,11 @@ func (pl *powerLocker) IsLocked() bool {
func (pl *powerLocker) Acquire() error { func (pl *powerLocker) Acquire() error {
pl.mu.Lock() pl.mu.Lock()
defer pl.mu.Unlock() defer pl.mu.Unlock()
if len(pl.ch) == 1 { select {
case pl.ch <- true:
default:
return errors.WithStack(ErrPowerLockerLocked) return errors.WithStack(ErrPowerLockerLocked)
} }
pl.ch <- true
return nil return nil
} }
@ -95,11 +96,13 @@ func (pl *powerLocker) TryAcquire(ctx context.Context) error {
} }
// Release will drain the locker channel so that we can properly re-acquire it // Release will drain the locker channel so that we can properly re-acquire it
// at a later time. // at a later time. If the channel is not currently locked this function is a
// no-op and will immediately return.
func (pl *powerLocker) Release() { func (pl *powerLocker) Release() {
pl.mu.Lock() pl.mu.Lock()
if len(pl.ch) == 1 { select {
<-pl.ch case <-pl.ch:
default:
} }
pl.mu.Unlock() pl.mu.Unlock()
} }
@ -108,8 +111,9 @@ func (pl *powerLocker) Release() {
func (pl *powerLocker) Destroy() { func (pl *powerLocker) Destroy() {
pl.mu.Lock() pl.mu.Lock()
if pl.ch != nil { if pl.ch != nil {
if len(pl.ch) == 1 { select {
<-pl.ch case <-pl.ch:
default:
} }
close(pl.ch) close(pl.ch)
} }