Skip to content

Commit

Permalink
guard against concurrent reset (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 authored Mar 21, 2023
1 parent 28616cc commit ff4ddc1
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions reader/reusable_read_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"fmt"
"io"
"strings"
"sync"
)

// ReusableReadCloser is a reusable reader with no-op close
type ReusableReadCloser struct {
*sync.RWMutex
io.Reader
readBuf *bytes.Buffer
backBuf *bytes.Buffer
Expand Down Expand Up @@ -67,6 +69,7 @@ func NewReusableReadCloser(raw interface{}) (*ReusableReadCloser, error) {

}
reusableReadCloser := &ReusableReadCloser{
&sync.RWMutex{},
io.TeeReader(&readBuf, &backBuf),
&readBuf,
&backBuf,
Expand All @@ -86,6 +89,9 @@ func (r ReusableReadCloser) Read(p []byte) (int, error) {
}

func (r ReusableReadCloser) reset() {
r.Lock()
defer r.Unlock()

_, _ = io.Copy(r.readBuf, r.backBuf)
}

Expand Down

0 comments on commit ff4ddc1

Please sign in to comment.