From d842e6c22c4f8950bc32ae7bc4a8f6603891195d Mon Sep 17 00:00:00 2001 From: Sean Marciniak Date: Mon, 16 Sep 2024 13:37:30 +0930 Subject: [PATCH] Improving memory performance when it comes to snappy Moving snappy to lazy read from the original payload instead decompressing it in memory --- .chloggen/msg_fix-snappy-lazy-read.yaml | 25 +++++++++++++++++++++++++ config/confighttp/compression.go | 13 +++---------- config/confighttp/compression_test.go | 2 +- 3 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 .chloggen/msg_fix-snappy-lazy-read.yaml diff --git a/.chloggen/msg_fix-snappy-lazy-read.yaml b/.chloggen/msg_fix-snappy-lazy-read.yaml new file mode 100644 index 00000000000..00c037f1528 --- /dev/null +++ b/.chloggen/msg_fix-snappy-lazy-read.yaml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: confighttp + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Snappy compression to lazy read for memory efficiency + +# One or more tracking issues or pull requests related to the change +issues: [11177] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] diff --git a/config/confighttp/compression.go b/config/confighttp/compression.go index 4498fefe864..3c1a494e0c2 100644 --- a/config/confighttp/compression.go +++ b/config/confighttp/compression.go @@ -58,17 +58,10 @@ var availableDecoders = map[string]func(body io.ReadCloser) (io.ReadCloser, erro } return zr, nil }, + //nolint:unparam // ignoring lint since method needs to fit typedef "snappy": func(body io.ReadCloser) (io.ReadCloser, error) { - sr := snappy.NewReader(body) - sb := new(bytes.Buffer) - _, err := io.Copy(sb, sr) - if err != nil { - return nil, err - } - if err = body.Close(); err != nil { - return nil, err - } - return io.NopCloser(sb), nil + // Lazy Reading content to improve memory efficiency + return io.NopCloser(snappy.NewReader(body)), nil }, } diff --git a/config/confighttp/compression_test.go b/config/confighttp/compression_test.go index a4fcb013f4f..fe087827418 100644 --- a/config/confighttp/compression_test.go +++ b/config/confighttp/compression_test.go @@ -230,7 +230,7 @@ func TestHTTPContentDecompressionHandler(t *testing.T) { encoding: "snappy", reqBody: bytes.NewBuffer(testBody), respCode: http.StatusBadRequest, - respBody: "snappy: corrupt input\n", + respBody: "snappy: corrupt input", }, { name: "UnsupportedCompression",