Skip to content

Commit

Permalink
Use buffer pool to marshal request body.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Dec 19, 2024
1 parent 5150d51 commit 424d985
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
package signaling

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -140,13 +139,14 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ
}
defer pool.Put(c)

data, err := json.Marshal(request)
data, err := b.buffers.MarshalAsJSON(request)
if err != nil {
log.Printf("Could not marshal request %+v: %s", request, err)
return err
}

req, err := http.NewRequestWithContext(ctx, "POST", requestUrl.String(), bytes.NewReader(data))
defer b.buffers.Put(data)
req, err := http.NewRequestWithContext(ctx, "POST", requestUrl.String(), data)
if err != nil {
log.Printf("Could not create request to %s: %s", requestUrl, err)
return err
Expand All @@ -160,11 +160,11 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ
}

// Add checksum so the backend can validate the request.
AddBackendChecksum(req, data, secret)
AddBackendChecksum(req, data.Bytes(), secret)

resp, err := c.Do(req)
if err != nil {
log.Printf("Could not send request %s to %s: %s", string(data), req.URL, err)
log.Printf("Could not send request %s to %s: %s", data.String(), req.URL, err)
return err
}
defer resp.Body.Close()
Expand Down

0 comments on commit 424d985

Please sign in to comment.