go的net/http中,client的timeout是否存在问题?
时间:2023-01-26
浏览:117
作者:佚名
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《go的net/http中,client的timeout是否存在问题?》,聊聊net、go,希望可以帮助到正在努力赚钱的你。
问题内容
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.
//
// If CheckRedirect is nil, the Client uses its default policy,
// which is to stop after 10 consecutive requests.
CheckRedirect func(req *Request, via []*Request) error
// Jar specifies the cookie jar.
// If Jar is nil, cookies are not sent in requests and ignored
// in responses.
Jar CookieJar
// Timeout specifies a time limit for requests made by this
// Client. The timeout includes connection time, any
// redirects, and reading the response body. The timer remains
// running after Get, Head, Post, or Do return and will
// interrupt reading of the Response.Body.
//
// A Timeout of zero means no timeout.
//
// The Client's Transport must support the CancelRequest
// method or Client will return errors when attempting to make
// a request with Get, Head, Post, or Do. Client's default
// Transport (DefaultTransport) supports CancelRequest.
Timeout time.Duration
}
以上是部分源码。之前有同学问,client的timeout存在问题,请问有其他人遇见了同样的问题么?
正确答案
题主的问题很有些莫名其妙,只说有问题但没说明究竟什么问题……不过我还是尝试答一下吧。
如果要说
Timeout有什么问题,需要注意注释文档里的这句话。
A Timeout of zero means no timeout.
如果你要发送很多请求,这个默认特性会害死人,在网络有问题的时候它会造成
Client生成的
Request和
Response对象以及相关 goroutine 得不到释放,逐渐就吃满内存导致程序挂掉。
所以使用 go 的时候最好不要直接使用
http.Get之类的函数,这些都是使用默认
Client的,没有
Timeout,如果非要用,那就把
http.DefaultClient的
Timeout设置上吧。
最后,我还是预感题主要问的问题跟我说的其实不搭界,啊,题主快现身把你的题目写清楚吧~
到这里,我们也就讲完了《go的net/http中,client的timeout是否存在问题?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于golang的知识点!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.30
11.30
11.30
11.30
11.30
11.30
11.30
热门专题
