Home  >  Article  >  Backend Development  >  golang http request timeout

golang http request timeout

WBOY
WBOYOriginal
2023-05-13 10:37:072248browse

When using golang to develop http services, you may encounter request timeout problems. This kind of problem may affect the stability and reliability of the service, so a solution needs to be found. This article will introduce golang's http request timeout problem and its solution.

  1. Problem Description

In http services, request timeout is a common problem. For example, when the client sends a request, if the server does not respond within the specified time, or an error occurs during the response, the request will time out.

In golang, this problem can be solved by setting the Timeout attribute in Client or the Timeout attribute in http.Transport.

  1. Set Client Timeout

http.Client in golang provides a Timeout attribute for setting the request timeout. By default, the Timeout property is set to zero, indicating no timeout limit. When the Timeout attribute is set to a non-zero value, the server will wait for a response within the specified time.

For example, the following code sets a timeout limit of 2 seconds:

client := &http.Client{
    Timeout: time.Second * 2,
}

When the request times out, a timeout error will be returned (net/http: request canceled (Client.Timeout exceeded while awaiting headers)). Errors can be thrown or handled accordingly according to actual needs.

  1. Set http.Transport Timeout

In addition to setting the Timeout property of http.Client, you can also control the request timeout by setting the properties of http.Transport. Specifically, you can set properties such as DialTimeout, TLSHandshakeTimeout, ResponseHeaderTimeout, and ExpectContinueTimeout.

For example, the following code sets a DialTimeout of 2 seconds:

transport := &http.Transport{
    DialContext: (&net.Dialer{
        Timeout:   2 * time.Second,
        KeepAlive: 30 * time.Second,
    }).DialContext,
    MaxIdleConns:        100,
    IdleConnTimeout:     90 * time.Second,
    TLSHandshakeTimeout: 2 * time.Second,
}
client := &http.Client{
    Timeout:   time.Second * 2,
    Transport: transport,
}

When the timeout occurs, a timeout error (dial tcp: i/o timeout) will be returned. Errors can be thrown or handled accordingly according to actual needs.

  1. Summary

In golang, setting the request timeout is an important measure to ensure service stability and reliability. Request timeout control can be achieved by setting the Timeout property of http.Client or the property of http.Transport. In actual development, it is necessary to set an appropriate timeout based on requirements to ensure the normal operation of the service.

The above is the detailed content of golang http request timeout. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn