Skip to content

Instantly share code, notes, and snippets.

@asim
Created March 19, 2016 10:57
Show Gist options
  • Save asim/11aa2e31ddece57b41bf to your computer and use it in GitHub Desktop.
Save asim/11aa2e31ddece57b41bf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
func foo(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, []byte(`hello world`))
}
func call(i int, wg *sync.WaitGroup){
defer wg.Done()
for j := 0; j < i; j++ {
_, err := http.Get("http://localhost:9099")
if err != nil {
fmt.Println("call err: ", err)
return
}
}
}
func main() {
http.HandleFunc("/", foo)
go http.ListenAndServe(":9099", nil)
wg := new(sync.WaitGroup)
var start, stop int
start = time.Now().Nanosecond()
fmt.Println("\n--- Call example ---\n")
wg.Add(5)
go call(10000, wg)
go call(10000, wg)
go call(10000, wg)
go call(10000, wg)
go call(10000, wg)
wg.Wait()
stop = time.Now().Nanosecond()
println("Call example Time used: ", stop-start)
}
@henrypfhu
Copy link

If try to use net/http client.Get("http://localhost:9099") to replace http.Get("http://localhost:9099"), can avoid the error: "call err: Get http://localhost:9099: dial tcp [::1]:9099: socket: too many open files"

net/http client.Get/Post can utilize net/http transport.go Transport connection pool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment