亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 1572 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號