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
設置上吧。
最后,我還是預感題主要問的問題跟我說的其實不搭界,啊,題主快現身把你的題目寫清楚吧~
- 2 回答
- 0 關注
- 1572 瀏覽
添加回答
舉報
0/150
提交
取消