Skip to content

Commit 027a84b

Browse files
authored
filebeat/filestream: Use gzip.Reader.Reset instead of Close+NewReader (#48516)
Replace the pattern of closing the gzip reader and creating a new one with a call to gzip.Reader.Reset() in gzipSeekerReader.Seek(). `Reset()` is the idiomatic way to reuse a gzip reader when switching to a new underlying reader. It: - Reuses internal buffers instead of allocating new ones, reducing memory pressure and GC overhead - Is semantically equivalent to creating a new reader (discards current state and reinitializes) - Eliminates the unnecessary `Close()` call whose error was being ignored anyway
1 parent 7eccae0 commit 027a84b

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

filebeat/input/filestream/file.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,10 @@ func (r *gzipSeekerReader) Seek(offset int64, whence int) (int64, error) {
157157
"gzipSeekerReader: could not seek to 0: %w", err)
158158
}
159159

160-
// it'll create a new reader, so this error can be safely ignored
161-
_ = r.gzr.Close()
162-
163-
r.gzr, err = gzip.NewReader(r.f)
160+
err = r.gzr.Reset(r.f)
164161
if err != nil {
165162
return n, fmt.Errorf(
166-
"gzipSeekerReader: could not create new gzip reader: %w", err)
163+
"gzipSeekerReader: could not reset gzip reader: %w", err)
167164
}
168165
r.offset = 0
169166

0 commit comments

Comments
 (0)