Original post

I tried googling for the answer and what I found on stackexchange did not work, so I figured I’d try asking here. Below is some rough pseudo- code covering the important aspects of our app. The main loop waits for messages on a channel and processes them. If anything should cause a panic, I want to catch this, recover, and keep on looping.

“` func main() { myChan := getChannel() shutdownChan := getShutdownChannel() // closes channel on SIGTERM Main: for { select { case newMessage = <-myChan // wait here for new messages break case <-shutdownChan break Main } defer func() { if r := recover(); r != nil { log.Error(“Panic Recovered in main loop:”, r) } }()

 // do whatever processing of the newMessage ... ... 

} doShutdownCleanup() // you get here from breaking out of main loop on SIGTERM } “`

The above code catches the panic, but the loop does not continue. The app shuts down. How do I need to “rearrange” this code so that I can still do ctrl-c/SIGTERM to have clean shutdown/exit, and also watch/wait on the channel for new messages, and handle panic’s to keep the loop going?

submitted by /u/utdrmac
[link] [comments]