pkg/ioutils: add NewBufReaderWithDrainbufAndBuffer
Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
This commit is contained in:
parent
028b28e406
commit
65f57a95bb
1 changed files with 21 additions and 9 deletions
|
@ -24,16 +24,29 @@ func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser {
|
||||||
|
|
||||||
type bufReader struct {
|
type bufReader struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
err error
|
err error
|
||||||
wait sync.Cond
|
wait sync.Cond
|
||||||
|
drainBuf []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBufReader(r io.Reader) *bufReader {
|
func NewBufReader(r io.Reader) *bufReader {
|
||||||
reader := &bufReader{
|
reader := &bufReader{
|
||||||
buf: &bytes.Buffer{},
|
buf: &bytes.Buffer{},
|
||||||
reader: r,
|
drainBuf: make([]byte, 1024),
|
||||||
|
reader: r,
|
||||||
|
}
|
||||||
|
reader.wait.L = &reader.Mutex
|
||||||
|
go reader.drain()
|
||||||
|
return reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBufReaderWithDrainbufAndBuffer(r io.Reader, drainBuffer []byte, buffer *bytes.Buffer) *bufReader {
|
||||||
|
reader := &bufReader{
|
||||||
|
buf: buffer,
|
||||||
|
drainBuf: drainBuffer,
|
||||||
|
reader: r,
|
||||||
}
|
}
|
||||||
reader.wait.L = &reader.Mutex
|
reader.wait.L = &reader.Mutex
|
||||||
go reader.drain()
|
go reader.drain()
|
||||||
|
@ -41,14 +54,13 @@ func NewBufReader(r io.Reader) *bufReader {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *bufReader) drain() {
|
func (r *bufReader) drain() {
|
||||||
buf := make([]byte, 1024)
|
|
||||||
for {
|
for {
|
||||||
n, err := r.reader.Read(buf)
|
n, err := r.reader.Read(r.drainBuf)
|
||||||
r.Lock()
|
r.Lock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.err = err
|
r.err = err
|
||||||
} else {
|
} else {
|
||||||
r.buf.Write(buf[0:n])
|
r.buf.Write(r.drainBuf[0:n])
|
||||||
}
|
}
|
||||||
r.wait.Signal()
|
r.wait.Signal()
|
||||||
r.Unlock()
|
r.Unlock()
|
||||||
|
|
Loading…
Reference in a new issue