2020-11-02 00:53:08 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2021-03-29 19:57:13 +00:00
|
|
|
// TimeoutHandler recovers from an http timeout or panic.
|
2020-11-02 00:53:08 +00:00
|
|
|
type TimeoutHandler func()
|
|
|
|
|
|
|
|
// NewTimeoutHandler returns a new Timeout handler.
|
|
|
|
func NewTimeoutHandler(name, addr string, noFail bool) TimeoutHandler {
|
|
|
|
return func() {
|
2020-11-02 23:20:06 +00:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
out := fmt.Sprintf("Failed to authenticate with %s @ %s: Timed out", name, addr)
|
|
|
|
if noFail {
|
|
|
|
log.Print(out)
|
|
|
|
} else {
|
|
|
|
log.Fatalf(out)
|
|
|
|
}
|
2020-11-02 00:53:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|