I have some code, there are 3 timers,
- GracefulExecutionTimeout - total running time
- WaitTimeout - time to initial wait for first message
- IdleTimeout - time to wait for subsequent messages
If any of the timers are reached the app should cleanly exit. I have it working below
msgs := make(chan string)
go func() {
time.Sleep(time.Second)
msgs <- "test"
}()
// graceful max execution time
gracefulMaxTimeout := time.Second * time.Duration(10)
gracefulMaxTimer := time.NewTimer(gracefulMaxTimeout)
// idleTimeout
idleTimeout := time.Second * time.Duration(5)
idleTimer := time.NewTimer(idleTimeout)
// waitTimeout
waitTimeout := time.Second * time.Duration(2)
waitTimer := time.NewTimer(waitTimeout)
for {
select {
case <-gracefulMaxTimer.C:
fmt.Println("GracefulMaxExecutionTimeout Reached")
// graceful exit
os.Exit(0)
case <-idleTimer.C:
fmt.Println("IdleTimeout Reached")
// graceful exit
os.Exit(0)
case <-waitTimer.C:
fmt.Println("WaitTimeout Reached")
// graceful exit
os.Exit(0)
case msg := <-msgs:
// stop wait timer
waitTimer.Stop()
fmt.Println(msg)
// Reset idle timer
if !idleTimer.Stop() {
<-idleTimer.C
}
fmt.Println("IdleIimeout Reset")
idleTimer.Reset(idleTimeout)
}
}
I want to make the WaitTimeout optional but not sure how to approach it. If i surround the construction of the waitTimer
with an if
statement then it wont work as the waitTimer
isnt defined for the select
statement ... How can i make the WaitTimeout conditional ?
I can just .Stop()
the timer after its created but that seems a little dirty ...