Timeout detection identifies whether an operation failed because of a network timeout, allowing the caller to report the problem or retry it. Reliable detection checks several error forms because timeout methods are not always exposed consistently. section 11.1.2
Listing 11.4 Detecting a network timeout from error
func hasTimedOut(err error) bool { #1
switch err := err.(type) { #2
case *url.Error: #3
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
return true
}
case net.Error:
if err.Timeout() { #4
return true
}
case *net.OpError:
if err.Timeout() {
return true
}
}
errTxt := "use of closed network connection" #5
if err != nil && strings.Contains(err.Error(), #5
errTxt) { #5
return true
}
return false
}
Checks wrapped URL errors, net errors, network operation errors, and the closed-connection message before classifying an operation as timed out. section 11.1.2
