Skip to content

Commit

Permalink
Use io.Copy instead of io.ReadAll (#3241)
Browse files Browse the repository at this point in the history
Copying to a bytes.Buffer results in fewer allocations then ReadAll.
  • Loading branch information
michel-laterman authored Jan 31, 2024
1 parent 73bb556 commit 502af76
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
21 changes: 9 additions & 12 deletions internal/pkg/api/handleAck.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/miolini/datacounter"
"github.com/rs/zerolog"

"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
Expand Down Expand Up @@ -149,20 +149,17 @@ func (ack *AckT) validateRequest(zlog zerolog.Logger, w http.ResponseWriter, r *
if ack.cfg.Limits.AckLimit.MaxBody > 0 {
body = http.MaxBytesReader(w, body, ack.cfg.Limits.AckLimit.MaxBody)
}

raw, err := io.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("handleAcks read body: %w", err)
}

cntAcks.bodyIn.Add(uint64(len(raw)))
readCounter := datacounter.NewReaderCounter(body)

var req AckRequest
if err := json.Unmarshal(raw, &req); err != nil {
return nil, fmt.Errorf("handleAcks unmarshal: %w", err)
dec := json.NewDecoder(readCounter)
if err := dec.Decode(&req); err != nil {
return nil, fmt.Errorf("unable to decode ack request: %w", err)
}
zlog.Trace().RawJSON("raw", raw).Msg("Ack request")
return &req, err

cntAcks.bodyIn.Add(readCounter.Count())
zlog.Trace().Msg("Ack request")
return &req, nil
}

func eventToActionResult(agentID, aType string, ev AckRequest_Events_Item) (acr model.ActionResult) {
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/api/handlePGPRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package api

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -143,7 +144,9 @@ func (pt *PGPRetrieverT) getPGPFromUpstream(ctx context.Context) ([]byte, error)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %d", ErrUpstreamStatus, resp.StatusCode)
}
return io.ReadAll(resp.Body)
var b bytes.Buffer
_, err = io.Copy(&b, resp.Body)
return b.Bytes(), err
}

// writeKeyToDir will write the specified key to the keys directory
Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/bulk/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package bulk

import (
"bytes"
"encoding/json"
"io"

Expand Down Expand Up @@ -41,11 +42,12 @@ func parseError(res *esapi.Response, log *zerolog.Logger) error {

if err := decoder.Decode(&e); err != nil {
log.Error().Err(err).Msg("Cannot decode Elasticsearch error body")
bodyBytes, readErr := io.ReadAll(res.Body)
var b bytes.Buffer
_, readErr := io.Copy(&b, res.Body)
if readErr != nil {
log.Debug().Err(readErr).Msg("Error reading error response body from Elasticsearch")
} else {
log.Debug().Err(err).Bytes("body", bodyBytes).Msg("Error content")
log.Debug().Err(err).Bytes("body", b.Bytes()).Msg("Error content")
}

return err
Expand Down

0 comments on commit 502af76

Please sign in to comment.