婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > golang http使用踩過的坑與填坑指南

golang http使用踩過的坑與填坑指南

熱門標簽:阿克蘇地圖標注 評價高的400電話辦理 百度地圖標注后傳給手機 電話機器人軟件免費 外呼系統顯本地手機號 excel地圖標注分布數據 涿州代理外呼系統 外呼系統用什么卡 壽光微信地圖標注

golang對http進行了很好的封裝, 使我們在開發基于http服務的時候, 十分的方便, 但是良好的封裝, 很容易是的我們忽略掉它們底層的實現細節。

如下是我踩過的一些坑, 以及相應的解決方法。

調用http服務

通常的實踐如下:

resp, err := http.Get("http://example.com/")
if err != nil {
               // handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// ...

陷阱一: Response body沒有及時關閉

網絡程序運行中, 過了一段時間, 比較常見的問題就是爆出錯誤:“socket: too many open files”, 這通常是由于打開的文件句柄沒有關閉造成的。

在http使用中, 最容易讓人忽視的, 就是http返回的response的body必須close,否則就會有內存泄露。

更不容易發現的問題是, 如果response.body的內容沒有被讀出來, 會造成socket鏈接泄露, 后續的服務無法使用。

這里, response.body是一個io.ReadCloser類型的接口, 包含了read和close接口。

 type Response struct { 
    // Body represents the response body.
    //
    // The response body is streamed on demand as the Body field
    // is read. If the network connection fails or the server
    // terminates the response, Body.Read calls return an error.
    //
    // The http Client and Transport guarantee that Body is always
    // non-nil, even on responses without a body or responses with
    // a zero-length body. It is the caller's responsibility to
    // close Body. The default HTTP client's Transport may not
    // reuse HTTP/1.x "keep-alive" TCP connections if the Body is
    // not read to completion and closed.
    //
    // The Body is automatically dechunked if the server replied
    // with a "chunked" Transfer-Encoding.
    Body io.ReadCloser
 }

如果沒有通過ioutil.ReadAll或者其他的接口讀取response.body的內容, 此次socket鏈接就無法被后續的連接復用, 造成的結果就是該連接一直存在。

盡管調用了ioutil.ReadAll就可以避免該連接的泄露, 我們還是建議在獲取response后, 就調用Close, 因為在response返回的地方與ReadAll之間, 萬一有條件判斷造成接口提前返回, 還是會造成泄露的。

defer resp.Body.Close()

另外, http.Request是不需要主動關閉的。

陷阱二: 默認的http的transport的設定不合適

在簡單的應用下, 采用默認的http client就可以滿足需要, 在稍微復雜一點的場景, 有其實想要保持長鏈接以及提高鏈接復用的效率等方面的控制, 這個時候就需要對client比較清楚的了解。

type Client struct {
    // Transport specifies the mechanism by which individual
    // HTTP requests are made.
    // If nil, DefaultTransport is used.
    Transport RoundTripper  
    // 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 cancels requests to the underlying Transport
    // as if the Request's Context ended.
    //
    // For compatibility, the Client will also use the deprecated
    // CancelRequest method on Transport if found. New
    // RoundTripper implementations should use the Request's Context
    // for cancelation instead of implementing CancelRequest.
    Timeout time.Duration
}

這里, 我們重點關注Transport與Timeout兩個字段, Transport記錄了本次請求的事務信息, 以及連接復用相關的信息。

Timeout記錄此次調用的超時時間以避免異常發生的時候的長時間等待。

通常我們使用的默認的Transport定義如下:

var DefaultTransport RoundTripper = Transport{
    Proxy: ProxyFromEnvironment,
    DialContext: (net.Dialer{
        Timeout:   30 * time.Second,
        KeepAlive: 30 * time.Second,
        DualStack: true,
    }).DialContext,
    MaxIdleConns:          100,
    IdleConnTimeout:       90 * time.Second,
    TLSHandshakeTimeout:   10 * time.Second,
    ExpectContinueTimeout: 1 * time.Second,
}

默認情況下, 它會保留打開的連接以備未來復用, 如果服務要連接很多的主機, 就會保存很多的空閑連接, IdleConnTimeout用來將超過一定時間的空閑連接回收;實際上, Defaulttransport 的MaxIdleConns是100, 在很多的場景下還是偏小的, 尤其是對于需要管理大的系統并且模塊之間交互頻繁的情況。

另外, 如果該連接需要定期 訪問很多的資源節點, 并列我們知道每個資源節點上面需要的連接數大于2, 那么就會出現很多的短連接, 因為對于每一臺資源機, DefaultTransport默認的最大連接數是2, 最大空閑連接是1.

 type Transport struct {
     // MaxIdleConnsPerHost, if non-zero, controls the maximum idle
    // (keep-alive) connections to keep per-host. If zero,
    // DefaultMaxIdleConnsPerHost is used.
    MaxIdleConnsPerHost int
    
    // MaxConnsPerHost optionally limits the total number of
    // connections per host, including connections in the dialing,
    // active, and idle states. On limit violation, dials will block.
    //
    // Zero means no limit.
    //
    // For HTTP/2, this currently only controls the number of new
    // connections being created at a time, instead of the total
    // number. In practice, hosts using HTTP/2 only have about one
    // idle connection, though.
    MaxConnsPerHost int
}

HTTP的長連接與TCP的長連接

在http1.1中, http默認保持長連接, 以備將來復用, 但是這個長連接通常是有時間限制的, 并且向我們上面開到的Transport里面的設定, 空閑的連接數是有最大限制的, 超過了該限制,其余新的連接就變成了短連接。

TCP協議本身是長連接, 它超過一定時間沒有數據傳送, 就會發送心跳來檢測該連接是否存活, 如果是, 該連接繼續有效。

補充:golang 設置 http response 響應頭的內容與坑

用 golang 寫 http server 時,可以很方便可通過 w.Header.Set(k, v) 來設置 http response 中 header 的內容。

例如:w.Header().Set("Access-Control-Allow-Origin", "*") 。

但是需要特別注意的是某些時候不僅要修改 http header ,還要修改 http status code。

修改 http status code 可以通過:w.WriteHeader(code) 來實現,例如:w.WriteHeader(404) 。

如果這兩種修改一起做,就必須讓 w.WriteHeader 在所有的 w.Header.Set 之后,也就是 w.WriteHeader 后 Set Header 是無效的。

今天就遇到了這個問題,在一段代碼中調用 w.Header.Set,怎么折騰都無效,最后才發現其它代碼段中先調用了 w.WriteHeader。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • go 原生http web 服務跨域restful api的寫法介紹
  • Go http client 連接池不復用的問題
  • Golang實現http server提供壓縮文件下載功能
  • golang語言http協議get拼接參數操作
  • 在go文件服務器加入http.StripPrefix的用途介紹
  • Golang 實現分片讀取http超大文件流和并發控制
  • Go 實現HTTP中間人代理的操作

標簽:雞西 汕頭 蘭州 重慶 欽州 銅川 吐魯番 梅河口

巨人網絡通訊聲明:本文標題《golang http使用踩過的坑與填坑指南》,本文關鍵詞  golang,http,使用,踩過,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《golang http使用踩過的坑與填坑指南》相關的同類信息!
  • 本頁收集關于golang http使用踩過的坑與填坑指南的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 南汇区| 宜阳县| 涿州市| 台山市| 兰溪市| 柳州市| 德兴市| 专栏| 建水县| 鄯善县| 揭阳市| 土默特右旗| 神木县| 太湖县| 柳河县| 东方市| 新平| 图片| 赤峰市| 皋兰县| 中卫市| 霍林郭勒市| 广安市| 德江县| 济阳县| 黎川县| 新晃| 莱州市| 黑龙江省| 富川| 九江县| 鹤庆县| 随州市| 鹿泉市| 同江市| 古丈县| 青州市| 根河市| 武川县| 新竹县| 防城港市|