With the client code below (and a listening web server on port 8088 on this box), I am rarely able to get more than 23000 hits before this error pops up from the client.Get()
:
panic: Get http://localhost:8088/: dial tcp 127.0.0.1:8088: can't assign requested address
Oddly, if I increase the timer delay (i.e. from a millisecond to a microsecond) it takes far more hits to get the error, 170,000 or even more.
Looking at the network traffic, each client connection is used only a handful of times before it disconnects (i.e. the client side sends a FIN). So clearly it's making many TCP connections and overflowing the socket table. Given that the Golang HTTP docs say that keepalives are enabled by default, I would't expect this. A kernel trace shows no errors being emitted by the underlying socket before the close (other than EAGAIN, which is expected and doesn't always precede a socket close).
This is with Go 1.4.2 on OSX (14.4.0). Why are the client connections not being reused the whole time?
package main
import (
"io/ioutil"
"net/http"
"runtime"
"sync"
"time"
)
var reqnum = 0
func hit(client *http.Client) {
resp, err := client.Get("http://localhost:8088/")
if err != nil {
println(reqnum)
panic(err)
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
reqnum++ // not thread safe, but shouldn't cause errors.
}
func main() {
var wg sync.WaitGroup
runtime.GOMAXPROCS(runtime.NumCPU())
client := &http.Client{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(time.Microsecond * 1000)
for j := 0; j < 120000; j++ {
<-ticker.C
hit(client)
}
ticker.Stop()
}()
}
wg.Wait()
}