Skip to content

Commit

Permalink
Cleanup ticker allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
vishjain committed Jul 25, 2021
1 parent 82ac168 commit e0c3403
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions cmd/auto-pause/auto-pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,39 @@ func main() {
// TODO: #10595 make this configurable
const interval = time.Minute * 1

// Check if interval is greater than 0 so NewTicker does not panic.
if interval <= 0 {
exit.Message(reason.Usage, "Auto-pause interval must be greater than 0,"+
" not current value of {{.interval}}", out.V{"interval": interval.String()})
}
tickerChannel := time.NewTicker(interval)
defer tickerChannel.Stop()

// Check current state
alreadyPaused()

// channel for incoming messages
go func() {
for {
// On each iteration new timer is created
select {
// TODO: #10596 make it memory-leak proof
case <-time.After(interval):
case <-tickerChannel.C:
runPause()
case <-unpauseRequests:
fmt.Printf("Got request\n")
if runtimePaused {
runUnpause()

// Reset once cluster has been unpaused.
tickerChannel.Reset(interval)

// Avoid race where tick happens before Reset call and after unPause.
for {
select {
case <-tickerChannel.C:
default:
break
}
}
}

done <- struct{}{}
Expand Down

0 comments on commit e0c3403

Please sign in to comment.