Created
July 7, 2015 08:28
-
-
Save gperrudin/d118f331a2618739a783 to your computer and use it in GitHub Desktop.
golang http.get : connection not closed
This file contains hidden or 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
// makes a request to a service with the get parameters passed in parameter | |
// returns a Response struct containing the response | |
func getVastPlayer(v url.Values) (Response, float64, error) { | |
resp := Response{} | |
u := VAST_URL + v.Encode() | |
t1 := time.Now() | |
myresp, err := http.Get(u) | |
defer myresp.Body.Close() | |
t2 := time.Now() | |
if err != nil { | |
return resp, -1, errors.New("request error (" + err.Error() + ")") | |
} | |
if vastResp.StatusCode != 200 { | |
return resp, -1, errors.New("request error (status code : " + strconv.Itoa(myresp.StatusCode) + ")") | |
} | |
if err := json.NewDecoder(myresp.Body).Decode(&resp); err != nil { | |
return resp, -1, errors.New("response decoding error (" + err.Error() + ")") | |
} | |
responseTime := float64(t2.Sub(t1).Nanoseconds() / 1000000) | |
return resp, responseTime, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment