liveBook · Manning

Timeout

As explained in Go in Practice, Second Edition

A timeout is a limit on how long an operation may wait before it is abandoned or reported as unsuccessful.

A timeout limits how long a network operation may wait before it is abandoned or reported as unsuccessful. In HTTP requests, the configured period includes reading the response body. Timeouts help detect external-service failures so an application can report them, retry when recovery is plausible, or let alerting handle the failure. section 11.1.2

Handle network timeouts by combining explicit configuration with defensive error inspection:

  • Set an HTTP client's Timeout when the request must complete within a bounded period. section 11.1.2
  • Inspect errors from net, http, and related packages because timeout methods and error types vary. section 11.1.2
  • Retry a timed-out operation when another attempt may reach a recovered or different server instance, and stop after the retry budget is exhausted. section 11.1.2
  • Treat non-timeout errors separately instead of retrying every failed request. section 11.1.2

Detecting a timed-out HTTP request

A helper can unwrap url.Error, inspect net.Error and net.OpError, and recognize the closed-connection message used by another timeout path. The caller can then distinguish a timed-out request from an unrelated failure and apply retry or alert logic. 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
}

Inspect the complete Go helper for handling explicit and implicit network timeout cases. section 11.1.2

How Go in Practice, Second Edition covers it

How deep it goes
Explains HTTP timeout configuration, error-shape inspection, and bounded retries, but does not develop a broader timeout policy.
Examples
Demonstrates hasTimedOut, an HTTP request, and a five-minute client timeout inside a resumable download with retries.
What it assumes
Assumes familiarity with Go HTTP and error handling while clarifying several timeout-related error cases.
What you can do after
Diagnoses network timeouts with Go errors and applies bounded retries to HTTP operations.
How it connects
Connects timeouts with HTTP requests, network errors, retries, alerting, and external services.

Also explained in

Compare how 9 books explain timeout →

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.