I have both - a client and a server in a one program. With a function func Get(url string) ([]byte, error)
i make tons of requests to other server and my server handles one per second request from other clients. The function for request is:
var transport = &http.Transport{}
func Get(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
var client = &http.Client{
Transport: transport,
Timeout: 15 * time.Second,
}
resp, err := client.Do(req)
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
if resp != nil && resp.Body != nil {
io.Copy(ioutil.Discard, resp.Body)
}
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
return data, err
}
and the server is:
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello")
}
http.HandleFunc("/", homePage)
http.ListenAndServe(":9002", nil)
So, the question is - why my server start waiting, while my clients send all their requests to other server, before it start handling other client's request? What do i miss? CPU is ok, memory is ok, bandwidth is ok.