Skip to content

Commit

Permalink
don't leak timers
Browse files Browse the repository at this point in the history
  • Loading branch information
directionless committed Jun 1, 2023
1 parent e70a4c0 commit 07fa160
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (l *locker) Lock(ctx context.Context) error {

}

timeout := time.NewTimer(wait)
defer timeout.Stop()

// Block until we get the lock, the context is canceled, or we time out.
select {
case l.c <- struct{}{}:
Expand All @@ -46,19 +49,19 @@ func (l *locker) Lock(ctx context.Context) error {
case <-ctx.Done():
// context has been canceled
return fmt.Errorf("context canceled: %w", ctx.Err())
case <-time.After(wait):
case <-timeout.C:
// timed out
return fmt.Errorf(timeoutError, wait)
}
}

// Unlock unlocks l
// Unlock unlocks l. It is a runtime error to unlock an unlocked locker.
func (l *locker) Unlock() {
select {
case <-l.c:
return
default:
// Calling Unlock on an unlocked mutex is a fatal error. We repeat that behavior here.
// Calling Unlock on an unlocked mutex is a fatal error. We mirror that behavior here.
panic("unlock of unlocked locker")
}

Expand Down

0 comments on commit 07fa160

Please sign in to comment.