How can I efficiently reconnect to an external database when I find out that the persistent connection is down? If ExtClient loses connection, it would return "Broken pipe" on err.
func ListenForWork(cmdChannel <-chan *WorkCmd) {
for {
cmd, ok := <- cmdChannel
if !ok {
break
}
for { // Retry request until it's OK (`Broken pipe error` might destroy it)
_, err := ExtClient.Request(cmd.Key, cmd.Value)
if err == nil {
break
}
}
}
}
How can I, from this or another method, reconnect in an efficient way? Any improvements in this code is welcome as well. ExtClient does not reconnect on its own and is a global variable.