liveBook · Manning

Timeout detection

As explained in Go in Practice, Second Edition

Timeout detection is the process of identifying whether an operation failure was caused by a network timeout.

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

How Go in Practice, Second Edition covers it

How deep it goes
Checks wrapped and concrete error types plus a closed-connection message, then stops short of broader timeout policy design.
Examples
Uses hasTimedOut with an HTTP request and a resumable file download that retries timed-out requests.
What you can do after
Diagnose/debug with it by classifying timeout errors and routing them into bounded retry handling.
How it connects
Connects network errors with HTTP connections, retries, explicit timeouts, and error wrapping.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.