all repos — homestead @ 3ef38d001d88f01511bf12ebcdfdf77b2e174689

Code for my website

fix remaining goatcounter issues

Alan Pearce
commit

3ef38d001d88f01511bf12ebcdfdf77b2e174689

parent

ca485baa487d36881484776f88628a47ebe1849c

1 file changed, 17 insertions(+), 18 deletions(-)

jump to
M internal/stats/goatcounter/count.gointernal/stats/goatcounter/count.go
@@ -85,28 +85,27 @@ if err != nil {
return errors.WithMessage(err, "could not marshal JSON") } - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - req, err := http.NewRequestWithContext( - ctx, - http.MethodPost, - gc.urls.count, - bytes.NewBuffer(body), - ) - if err != nil { - return errors.WithMessage(err, "could not create HTTP request") - } - req.Header.Set("Accept", "application/json") - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Authorization", "Bearer "+gc.token) - - go func(req *http.Request) { - var body []byte + go func(body []byte) { + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + req, err := http.NewRequestWithContext( + ctx, + http.MethodPost, + gc.urls.count, + bytes.NewBuffer(body), + ) + if err != nil { + gc.log.Warn("could not create HTTP request", "error", err) + } + req.Header.Set("Accept", "application/json") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+gc.token) res, err := gc.client.Do(req) if err != nil { gc.log.Warn("could not perform HTTP request", "error", err) } + clear(body) if res.Body != nil { defer res.Body.Close()
@@ -118,7 +117,7 @@ }
if res.StatusCode != http.StatusAccepted { gc.log.Warn("failed to log pageview", "status", res.StatusCode, "body", body) } - }(req) + }(body) return nil }