Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Oct 1, 2023
1 parent 6ff151a commit 65e65e5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions body_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ func (bw *bodyWrapper) DisableRewinds() {
bw.mu.Lock()
defer bw.mu.Unlock()

// Free memory if reading from original HTTP response
// or reader has nothing left to read in memory.
// Free memory if reading from original HTTP response, or reading from memory
// and memory reader has nothing left to read.
// Otherwise, i.e. when we're reading from memory, and there is more to read,
// memReadNext() will free memory later when it hits EOF.
if !bw.isFullyRead || bw.memReader.Len() == 0 {
bw.memReader = bytes.NewReader(nil)
bw.memBytes = nil
Expand All @@ -204,6 +206,8 @@ func (bw *bodyWrapper) memReadNext(p []byte) (int, error) {
n, err := bw.memReader.Read(p)

if err == io.EOF {
// Free memory after we hit EOF when reading from memory,
// if rewinds were disabled while we were reading from it.
if bw.isRewindDisabled {
bw.memReader = bytes.NewReader(nil)
bw.memBytes = nil
Expand Down

0 comments on commit 65e65e5

Please sign in to comment.