title: multiple http.Requests gives "can't assign requested address," unless sped up
body: <p>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 <code>client.Get()</code>:</p>
  
  <pre><code>panic: Get http://localhost:8088/: dial tcp 127.0.0.1:8088: can't assign requested address
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>This is with Go 1.4.2 on OSX (14.4.0). Why are the client connections not being reused the whole time?</p>
  
  <pre><code>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 := &amp;http.Client{}
      for i := 0; i &lt; 10; i++ {
          wg.Add(1)
          go func() {
              defer wg.Done()
              ticker := time.NewTicker(time.Microsecond * 1000)
              for j := 0; j &lt; 120000; j++ {
                  &lt;-ticker.C
                  hit(client)
              }
              ticker.Stop()
          }()
      }
      wg.Wait()
  }
  </code></pre>