Created
March 19, 2016 10:57
-
-
Save asim/11aa2e31ddece57b41bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.