Go, tcp too many open files debug

前端 未结 6 866
有刺的猬
有刺的猬 2020-12-08 20:32

Here\'s a straightforward Go http (tcp) connection test script

func main() {
    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *htt         


        
6条回答
  •  一生所求
    2020-12-08 20:39

    HTTP/1.1 uses persistent connections by default:
    A significant difference between HTTP/1.1 and earlier versions of HTTP is that persistent connections are the default behavior of any HTTP connection.
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
    The solution was to inform the server that the client wants to close the connection after the transaction is complete. This can be done by setting the Connection header,
    req.Header.Set("Connection", "close") or by setting the Close property to true on the http.Request:
    req.Close = true After doing that, the “too many open files” issue went away as the program was no longer keeping HTTP connections open and thus not using up file descriptors.
    

    I solved this by adding req.Close = true and req.Header.Set("Connection", "close"). I think it's better than changing ulimit.

    source: http://craigwickesser.com/2015/01/golang-http-to-many-open-files/

提交回复
热议问题