Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

fix net.http usage of request.doReq() #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package request

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
Expand All @@ -12,10 +13,20 @@ import (
func doReq(req *http.Request) ([]byte, error) {
client := &http.Client{}
resp, err := client.Do(req)
if resp != nil {
// workaround for possible net.http memory leak, ref:
// http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#close_http_resp_body
defer resp.Body.Close()
}
if err != nil {
if resp != nil {
// workaround for possible issue when we want to reuse the
// underlying connection, the resp.Body should be read in every
// possible path
io.Copy(ioutil.Discard, resp.Body)
}
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down