request: fix intermittent race when verbose and reading the body in different routines (#1507)

* fix racerooo

* glorious: nits

* close after reading

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2024-03-26 10:40:32 +11:00
committed by GitHub
parent 3aad665e90
commit 05dec88dc3

View File

@@ -171,8 +171,20 @@ func (r *Requester) doRequest(ctx context.Context, endpoint EndpointLimit, newRe
log.Debugf(log.RequestSys, "%s request header [%s]: %s", r.name, k, d)
}
log.Debugf(log.RequestSys, "%s request type: %s", r.name, p.Method)
if p.Body != nil {
log.Debugf(log.RequestSys, "%s request body: %v", r.name, p.Body)
if req.GetBody != nil {
bodyCopy, bodyErr := req.GetBody()
if bodyErr != nil {
return bodyErr
}
payload, bodyErr := io.ReadAll(bodyCopy)
err = bodyCopy.Close()
if err != nil {
log.Errorf(log.RequestSys, "%s failed to close request body %s", r.name, err)
}
if bodyErr != nil {
return bodyErr
}
log.Debugf(log.RequestSys, "%s request body: %s", r.name, payload)
}
}