diff --git a/umami_utils.go b/umami_utils.go index 56eb762..9b9a202 100644 --- a/umami_utils.go +++ b/umami_utils.go @@ -47,6 +47,9 @@ func sendRequest(ctx context.Context, url string, body interface{}, headers http status := resp.StatusCode if status < 200 || status >= 300 { + defer func() { + _ = resp.Body.Close() + }() return nil, fmt.Errorf("request failed with status %d", status) } diff --git a/umami_worker.go b/umami_worker.go index d7e3c4b..1a63045 100644 --- a/umami_worker.go +++ b/umami_worker.go @@ -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) resp, err := sendRequest(ctx, h.umamiHost+"/api/send", event.body, event.headers) - defer func() { - _ = resp.Body.Close() - }() if err != nil { h.error("failed to send tracking: " + err.Error()) return } + defer func() { + _ = resp.Body.Close() + }() }