Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the timeout setting of net.http Library in golang

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to achieve the timeout setting of net.http library in golang. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Timeout setting in net.http package

Server side

App.Server.ReadTimeout

App.Server.WriteTimeout

Is the option set for all requests

Net.http keepalived is enabled by default, and the timeout is 3 minutes, as follows

1942 type tcpKeepAliveListener struct {

1943 * net.TCPListener

1944}

1945

1946 func (ln tcpKeepAliveListener) Accept () (c net.Conn, err error) {

1947 tc, err: = ln.AcceptTCP ()

1948 if err! = nil {

1949 return

1950}

1951 tc.SetKeepAlive (true)

1952 tc.SetKeepAlivePeriod (3 * time.Minute)

1953 return tc, nil

1954}

Do not see the configuration option of keeping alive timeout, the production environment can be banned.

Client

As I just looked at, the golang http library client has five timeout settings, one is the Timeout in Client, one is the ResponseHeaderTimeout of Transport used by Client, and three are the timeout settings of Dialer in Transport, Timeout,KeepAlive,Deadline.

By looking at the golang code, it is found that before executing the http request, if Client.Timeout > 0, a fallback function is defined using time.AfterFunc, which is called after the timeout, and the function cancels the request in progress.

Timeout and Deadline of Dialer is the connection timeout, which is used in the process of establishing a connection.

The request is sent and the response is received by two co-programs respectively. After the request is sent, the timeout ResponseHeaderTimeout in Transport starts to count, so it refers to the timeout waiting for a response.

On "how to achieve net.http library timeout settings in golang" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report