Best Practices for Timeout Management in Go Language http.Transport
Introduction:
When using Go language to make network requests, it is very important to set the timeout reasonably to avoid blocking of requests and resources. of waste. This article will introduce the best practices of using http.Transport for timeout management in Go language, and provide some sample code for reference.
Set timeout:
Before sending a request using http.Transport, we can use some methods to set the request timeout. For example, we can use the Timeout
field of the http.Client
structure to set the timeout for the entire request, for example:
client := &http.Client{ Timeout: time.Second * 10, // 设置超时时间为10秒 }
In the above code, Timeout# The ## field indicates that the timeout of the entire request is 10 seconds. When the processing time of the request exceeds 10 seconds, the request will return a
net.DialTimeout error.
Timeout of
http.Request before sending the request using
http.Transport field to set the timeout for a single request. For example:
req, err := http.NewRequest("GET", "http://example.com", nil) if err != nil { log.Fatal(err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", "MyClient/1.0") req.Timeout = time.Second * 5 // 设置单个请求的超时时间为5秒 resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Println(string(body))
req.Timeout. When the processing time of the request exceeds 5 seconds, the request will return A
net.DialTimeout error.
When a request times out, we should handle this error reasonably. A common way is to use the
context package to control request timeouts. We can create a
context.Context with a timeout and pass it to the request's
http.Request. For example:
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() req, err := http.NewRequestWithContext(ctx, "GET", "http://example.com", nil) if err != nil { log.Fatal(err) }
context.Context with a 10-second timeout through
context.WithTimeout, and use
http .NewRequestWithContext passes the
Context to the request.
context.Context to monitor timeout events. For example:
resp, err := client.Do(req.WithContext(ctx)) if err != nil { if err == context.DeadlineExceeded { log.Println("请求超时") return } log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Println(string(body))
context.DeadlineExceeded. If it is a timeout event, we can do some corresponding processing operations, such as returning error information or retrying the request.
When using Go language to make network requests, it is very important to set a reasonable timeout. This article introduces best practices for timeout management using http.Transport in Go language and provides some sample code. By setting the timeout appropriately and using context.Context to monitor timeout events, we can better manage request timeouts and improve the stability and reliability of the program.
The above is the detailed content of Best practices for timeout management in Go language http.Transport. For more information, please follow other related articles on the PHP Chinese website!