fix: fix reporter memory leak problem #103

This commit is contained in:
moonrailgun 2024-10-19 01:37:57 +08:00
parent 2a503ca250
commit 7f70557c77

View File

@ -59,7 +59,10 @@ func main() {
interval := *Interval 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("Start reporting...")
log.Println("Mode:", *Mode) log.Println("Mode:", *Mode)
@ -78,10 +81,10 @@ func main() {
if *Mode == "udp" { if *Mode == "udp" {
sendUDPPack(*parsedURL, payload) sendUDPPack(*parsedURL, payload)
} else { } 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 * 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) jsonData, err := jsoniter.Marshal(payload)
if err != nil { if err != nil {
log.Println("Error encoding JSON:", err) 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("Content-Type", "application/json")
req.Header.Set("x-tianji-report-version", version) req.Header.Set("x-tianji-report-version", version)
client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Println("Send request error:", err) log.Println("Send request error:", err)