forked from ethereum-optimism/optimism
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ensure proper closing of gzip and file writers/readers to preve…
…nt resource leaks (ethereum-optimism#11475) * add gzipCloser struct * add Close method * fix OpenDecompressed func * fix CompressByFileType func * knit * Apply suggestions from code review close both even if one fails Co-authored-by: Adrian Sutton <[email protected]> * fix WriteCloser Close method * fix name for more general * fix writercloser name for more geneeral * add construction function for WrappedCloser * using construction func * seperate wrapped closer struct to wrapped_closer.go --------- Co-authored-by: Adrian Sutton <[email protected]>
- Loading branch information
1 parent
f243ad0
commit 698633c
Showing
2 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ioutil | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
) | ||
|
||
// WrappedReadCloser is a struct that closes both the gzip.Reader and the underlying io.Closer. | ||
type WrappedReadCloser struct { | ||
io.ReadCloser | ||
closer io.Closer | ||
} | ||
|
||
// WrappedWriteCloser is a struct that closes both the gzip.Writer and the underlying io.Closer. | ||
type WrappedWriteCloser struct { | ||
io.WriteCloser | ||
closer io.Closer | ||
} | ||
|
||
// Close closes both the gzip.Reader and the underlying reader. | ||
func (g *WrappedReadCloser) Close() error { | ||
return errors.Join(g.ReadCloser.Close(), g.closer.Close()) | ||
} | ||
|
||
// Close closes both the gzip.Writer and the underlying writer. | ||
func (g *WrappedWriteCloser) Close() error { | ||
return errors.Join(g.WriteCloser.Close(), g.closer.Close()) | ||
} | ||
|
||
// NewWrappedReadCloser is a constructor function that initializes a WrappedReadCloser structure. | ||
func NewWrappedReadCloser(r io.ReadCloser, c io.Closer) *WrappedReadCloser { | ||
return &WrappedReadCloser{ | ||
ReadCloser: r, | ||
closer: c, | ||
} | ||
} | ||
|
||
// NewWrappedWriteCloser is a constructor function that initializes a WrappedWriteCloser structure. | ||
func NewWrappedWriteCloser(r io.WriteCloser, c io.Closer) *WrappedWriteCloser { | ||
return &WrappedWriteCloser{ | ||
WriteCloser: r, | ||
closer: c, | ||
} | ||
} |