Hello,
the small program below could be run as
./ program -url <someURL>
or
./ program -skipbody -url <someURL>
On my machine, running it without -skipbody parameter, in doGet() function, "ioutl.ReadAll()" is performed, which seems
to cause to keep open the connection (seen with "lsof | grep program") to http server, so every second http.Get() returns error:
Get <someURL>: read tcp <ip>:80: connection reset by peer
In web server log no such error requests are logged, so it seems that these requests tries to reuse previous connection, which is closed.
If I add -skipbody parameter then no error occurs.
I'm using:
* web server - nginx
* OS - ubuntu 12.04
* go version devel +b279bb7f7c3b Tue Sep 16 19:54:26 2014 -0700 linux/amd64
Am I doing something wrong? Or is it a bug of some net/* package?
Any comments?
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"time"
)
var (
skipBody bool
url string
)
func init() {
flag.BoolVar(&skipBody, "skipbody", false, "skip body")
flag.StringVar(&url, "url", "", "url")
}
func main() {
flag.Parse()
if url == "" {
log.Fatal("Need URL!")
}
go func(url string, skipBody bool) {
i := 0
for {
i++
log.Printf("R #%d", i)
err := doGet(url, skipBody)
if err != nil {
log.Println(err)
} else {
log.Println("OK")
}
time.Sleep(60 * time.Second)
}
}(url, skipBody)
time.Sleep(10 * time.Minute)
}
func doGet(url string, skipBody bool) (err error) {
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
if skipBody {
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
log.Printf("len(data) = %d ", len(data))
time.Sleep(5 * time.Second) // wait a little bit
return
}
Thanks,
--
Saulius