I am running a call to a remote server, as an agent, this call should run every 5 minutes, but in testing, I run it every second. and I see a memory leak, and struggling to resolve it. The Gist of the code is as follows:
// Set the initial payload to be sent
payloadBytes, err := json.Marshal(data)
if err != nil { log.Print("Failed Marshal", err) }
transport := &http.Transport{ DisableKeepAlives: true }
client := http.Client{Transport: transport}
// Repeat the post call to the remote server
repeatTicker := time.NewTicker(1 * time.Second)
for {
select {
case <- repeatTicker.C:
body := bytes.NewReader(payloadBytes)
log.Print("Sending:", string(payloadBytes))
postCycle(client, body)
}
}
transport.CloseIdleConnections()
}
func postCycle(client http.Client , body *bytes.Reader){
resp, err := client.Post("http://mywebsite.io/api", "application/json", body)
if err != nil { log.Print("Failed to Connect",err) }
responseData, err := ioutil.ReadAll(resp.Body)
if err != nil { log.Print("Response contains Errors", err) }
resp.Body.Close()
}
What I find it, the Transport is opened once and is reused, but the resp.Body.Close() does not seem to have the desired effect, and the memory count continues to rise. The return data is an echo of sent data, so this never changes. Am I missing something glaring here? or will the memory count top out and remain sable when the garbage collection catches up?