From 424d985b379f57c294f21f16a80c2680ed2f7f4d Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Thu, 21 Nov 2024 20:04:49 +0100 Subject: [PATCH] Use buffer pool to marshal request body. --- backend_client.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backend_client.go b/backend_client.go index 070b102e..d741dfea 100644 --- a/backend_client.go +++ b/backend_client.go @@ -22,7 +22,6 @@ package signaling import ( - "bytes" "context" "encoding/json" "errors" @@ -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 @@ -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()