为了账号安全,请及时绑定邮箱和手机立即绑定

go的net/http中,client的timeout是否存在问题?

go的net/http中,client的timeout是否存在问题?

Go
绝地无双 2018-08-31 18:37:02
type Client struct {    // Transport specifies the mechanism by which individual    // HTTP requests are made.    // If nil, DefaultTransport is used.    Transport RoundTripper    // CheckRedirect specifies the policy for handling redirects.    // If CheckRedirect is not nil, the client calls it before    // following an HTTP redirect. The arguments req and via are    // the upcoming request and the requests made already, oldest    // first. If CheckRedirect returns an error, the Client's Get    // method returns both the previous Response and    // CheckRedirect's error (wrapped in a url.Error) instead of    // issuing the Request req.    //    以上是部分源码。之前有同学问,client的timeout存在问题,请问有其他人遇见了同样的问题么?
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

题主的问题很有些莫名其妙,只说有问题但没说明究竟什么问题……不过我还是尝试答一下吧。

如果要说 Timeout 有什么问题,需要注意注释文档里的这句话。

A Timeout of zero means no timeout.

如果你要发送很多请求,这个默认特性会害死人,在网络有问题的时候它会造成 Client 生成的 Request 和 Response 对象以及相关 goroutine 得不到释放,逐渐就吃满内存导致程序挂掉。

所以使用 go 的时候最好不要直接使用 http.Get 之类的函数,这些都是使用默认 Client 的,没有 Timeout,如果非要用,那就把 http.DefaultClient 的 Timeout 设置上吧。

最后,我还是预感题主要问的问题跟我说的其实不搭界,啊,题主快现身把你的题目写清楚吧~


查看完整回答
反对 回复 2018-09-06
  • 2 回答
  • 0 关注
  • 1512 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信