fix: close Body correctly

This commit is contained in:
Oleh Astappiev 2024-12-08 20:55:06 +01:00
parent 02580cce20
commit 254effa8e5
No known key found for this signature in database
GPG key ID: 37D7AC22589E9632
2 changed files with 6 additions and 3 deletions

View file

@ -47,6 +47,9 @@ func sendRequest(ctx context.Context, url string, body interface{}, headers http
status := resp.StatusCode status := resp.StatusCode
if status < 200 || status >= 300 { if status < 200 || status >= 300 {
defer func() {
_ = resp.Body.Close()
}()
return nil, fmt.Errorf("request failed with status %d", status) return nil, fmt.Errorf("request failed with status %d", status)
} }

View file

@ -120,11 +120,11 @@ func (h *UmamiFeeder) reportEventToUmami(ctx context.Context, event *UmamiPayloa
h.debug("sending tracking request %s with body %v %v", event.body.Payload.Url, event.body, event.headers) h.debug("sending tracking request %s with body %v %v", event.body.Payload.Url, event.body, event.headers)
resp, err := sendRequest(ctx, h.umamiHost+"/api/send", event.body, event.headers) resp, err := sendRequest(ctx, h.umamiHost+"/api/send", event.body, event.headers)
defer func() {
_ = resp.Body.Close()
}()
if err != nil { if err != nil {
h.error("failed to send tracking: " + err.Error()) h.error("failed to send tracking: " + err.Error())
return return
} }
defer func() {
_ = resp.Body.Close()
}()
} }