When I use my go(1.8) http lib to do normal GET/POST method, it works fine, If I try to upload file to server with the http lib, the client will create a lot of sockets. In my test, files are cut in pieces to upload in 5 goroutines, the client remain 250 sockets. I already add defer resp.Body.Close()
,here the key codes:
const (
MaxIdleConns int = 40
MaxIdleConnsPerHost int = 40
)
transport := &http.Transport{
MaxIdleConns: MaxIdleConns,
MaxIdleConnsPerHost: MaxIdleConnsPerHost,
IdleConnTimeout: 15 * time.Second,
ResponseHeaderTimeout: Time.Hour,
}
client := &http.Client{
Transport: transport,
Timeout: time.Second * 30,
}
those 250 sockets only recycle after client exit。
It just a silly question,the lib doesn't reuse http client, thanks for help.here my new transport define:
const (
MaxIdleConnsPerHost = 10
MaxIdleConns = 100
)
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: MaxIdleConns,
MaxIdleConnsPerHost: MaxIdleConnsPerHost,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 5 * time.Second,
}
and i cache the http client instead of new http.Client for every http request. go doc recommand reuse the client
// The Client's Transport typically has internal state (cached TCP // connections), so Clients should be reused instead of created as // needed. Clients are safe for concurrent use by multiple goroutines. https://golang.org/src/net/http/client.go