Home  >  Article  >  Backend Development  >  Let you know how to set network timeout in Golang

Let you know how to set network timeout in Golang

PHPz
PHPzOriginal
2023-03-30 09:11:371636browse

When doing network programming, you often encounter the need to set a timeout to avoid network connection timeouts that may cause lags. Setting the network timeout in Golang is very simple. This article will show you how to set the network timeout in Golang.

1. Network timeout in Golang

When communicating on the network, both the client and the server need to set a certain timeout to ensure the robustness of the program. In Golang, network timeout can be set in two ways:

  1. Context timeout

Context package is a commonly used package, which can be used to control multiple Goroutine cancellation, timeout, deadline, etc. When using Context in network programming, we can set the timeout through the underlying WithDeadline or WithTimeout method, as shown below:

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

resp, err := http.Get("https://www.baidu.com")
if err != nil {
    log.Fatalln(err)
}
defer resp.Body.Close()

In the above code, we set a Context with a timeout of 5 seconds through the WithTimeout method. . When the timeout reaches, we cancel this Context through defer cancel(), so that the code can exit in time.

  1. net.Dial timeout

In Golang, we can create a tcp or udp connection through the net.Dial function. This function has multiple overloaded forms, including the form that sets the timeout. For example, to set a tcp connection with a timeout of 5 seconds, we can write like this:

conn, err := net.DialTimeout("tcp", "www.baidu.com:80", time.Second*5)
if err != nil {
    log.Fatalln(err)
}

Here we use the net.DialTimeout function to create a tcp connection. If the connection is successfully established within 5 seconds, a net.Conn object is returned. Otherwise, the function will return an error, and we need to exit the program promptly after this error is returned.

2. How to choose the timeout method

So which method should be chosen for timeout setting when doing network programming? This depends on the specific business context.

In general, using Context timeout is the more recommended way. This is because using Context timeout can be more easily integrated into other parts of the code, such as goroutine control. Moreover, through the cancel function, we can understand the running status of the Context more intuitively, so as to better handle errors.

However, in some scenarios, the net.Dial timeout method is more appropriate. For example, when making http requests, we can use the Transport structure under the net/http package to reuse the connection pool. At this time, using the net.Dial method can better control the connection status.

To sum up, in actual development, we need to decide which method to use for timeout setting based on specific business needs.

3. Summary

Through the explanation of this article, I believe that readers have mastered the method of setting network timeout in Golang. When doing network programming, we must always be vigilant to avoid program freezes due to network connection timeouts. Therefore, setting the timeout is very necessary, and readers must apply it.

The above is the detailed content of Let you know how to set network timeout in Golang. 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