I've put up an http server written in Go and it's getting over a thousands visitors a day. I have an accumulating Goroutine problem(never freed). Over the course of a day I seem to get a little over a thousand of thousand of new Goroutines from the http server.I used pprof
to check where the problem comes from and I got that:
Link: memory consumption: SVG pprof
Heap:
Below are tow of my goroutines
500 @ 0x410255 0x5a9255 0x5a9e25 0x5aa615 0x5990cf 0x5ada95 0x59d23f 0x4367b1
# 0x5a9255 net._C2func_getaddrinfo+0x55 /usr/local/go/src/net/:26
# 0x5a9e25 net.cgoLookupIPCNAME+0x1c5 /usr/local/go/src/net/cgo_unix.go:96
# 0x5aa615 net.cgoLookupIP+0x65 /usr/local/go/src/net/cgo_unix.go:148
# 0x5990cf net.lookupIP+0x5f /usr/local/go/src/net/lookup_unix.go:64
# 0x5ada95 net.func·026+0x55 /usr/local/go/src/net/lookup.go:79
# 0x59d23f net.(*singleflight).doCall+0x2f /usr/local/go/src/net/singleflight.go:91
157871 @ 0x423985 0x4239f8 0x411464 0x410c93 0x5a9d68 0x5aa615 0x5990cf 0x5ada95 0x59d23f 0x4367b1
# 0x5a9d68 net.cgoLookupIPCNAME+0x108 /usr/local/go/src/net/cgo_unix.go:85
# 0x5aa615 net.cgoLookupIP+0x65 /usr/local/go/src/net/cgo_unix.go:148
# 0x5990cf net.lookupIP+0x5f /usr/local/go/src/net/lookup_unix.go:64
# 0x5ada95 net.func·026+0x55 /usr/local/go/src/net/lookup.go:79
# 0x59d23f net.(*singleflight).doCall+0x2f /usr/local/go/src/net/singleflight.go:91
Here we can see that singleflight.go took the most of goroutints, it's a native library of Go.
my code bolocked in this function
func getXmlVast(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", errors.New("request error A(" + err.Error() + ")")
}
defer resp.Body.Close()
// read xml http response
xmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("request error B(" + err.Error() + ")")
}
return string(xmlData), nil
}
Why Go never freed the goroutines and what singleflight.go do.
I updated my Go version from 1.4 to 1.5 and it soloves the problem.
I did some reserch before to find where the problem comes from I noticed that a lot of peopel have the same problem and no one know why.I think thtat the problem was in the http/net
library because as I said in my question the function who took the most of goroutints is singleflight and this function is called by http.Get(url)