From 7f70557c776c35e4e01a5533d2c05cecc711e113 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 19 Oct 2024 01:37:57 +0800 Subject: [PATCH] fix: fix reporter memory leak problem #103 --- reporter/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/reporter/main.go b/reporter/main.go index 8db9955..061b76f 100644 --- a/reporter/main.go +++ b/reporter/main.go @@ -59,7 +59,10 @@ func main() { interval := *Interval - ticker := time.Tick(time.Duration(interval) * time.Second) + ticker := time.NewTicker(time.Duration(interval) * time.Second) + defer ticker.Stop() + + httpClient := &http.Client{} log.Println("Start reporting...") log.Println("Mode:", *Mode) @@ -78,10 +81,10 @@ func main() { if *Mode == "udp" { sendUDPPack(*parsedURL, payload) } else { - sendHTTPRequest(*parsedURL, payload) + sendHTTPRequest(*parsedURL, payload, httpClient) } - <-ticker + <-ticker.C } } @@ -125,7 +128,7 @@ func sendUDPPack(url url.URL, payload ReportData) { /** * Send HTTP Request to report server data */ -func sendHTTPRequest(_url url.URL, payload ReportData) { +func sendHTTPRequest(_url url.URL, payload ReportData, client *http.Client) { jsonData, err := jsoniter.Marshal(payload) if err != nil { log.Println("Error encoding JSON:", err) @@ -148,7 +151,6 @@ func sendHTTPRequest(_url url.URL, payload ReportData) { req.Header.Set("Content-Type", "application/json") req.Header.Set("x-tianji-report-version", version) - client := &http.Client{} resp, err := client.Do(req) if err != nil { log.Println("Send request error:", err)