2015-09-09 16:47:24 +00:00
|
|
|
package ioutils
|
|
|
|
|
2015-09-29 17:58:08 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// maxCap is the highest capacity to use in byte slices that buffer data.
|
2015-09-17 00:51:11 +00:00
|
|
|
const maxCap = 1e6
|
2015-09-09 16:47:24 +00:00
|
|
|
|
2016-03-31 16:50:50 +00:00
|
|
|
// minCap is the lowest capacity to use in byte slices that buffer data
|
|
|
|
const minCap = 64
|
|
|
|
|
2015-09-29 17:58:08 +00:00
|
|
|
// blockThreshold is the minimum number of bytes in the buffer which will cause
|
|
|
|
// a write to BytesPipe to block when allocating a new slice.
|
|
|
|
const blockThreshold = 1e6
|
|
|
|
|
2016-03-31 16:50:50 +00:00
|
|
|
var (
|
|
|
|
// ErrClosed is returned when Write is called on a closed BytesPipe.
|
|
|
|
ErrClosed = errors.New("write to closed BytesPipe")
|
|
|
|
|
|
|
|
bufPools = make(map[int]*sync.Pool)
|
|
|
|
)
|
2015-09-29 17:58:08 +00:00
|
|
|
|
|
|
|
// BytesPipe is io.ReadWriteCloser which works similarly to pipe(queue).
|
|
|
|
// All written data may be read at most once. Also, BytesPipe allocates
|
|
|
|
// and releases new byte slices to adjust to current needs, so the buffer
|
|
|
|
// won't be overgrown after peak loads.
|
2015-09-09 16:47:24 +00:00
|
|
|
type BytesPipe struct {
|
2015-09-29 17:58:08 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
wait *sync.Cond
|
2016-03-31 16:50:50 +00:00
|
|
|
buf []*fixedBuffer
|
|
|
|
bufLen int
|
|
|
|
closeErr error // error to return from next Read. set to nil if not closed.
|
2015-09-09 16:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBytesPipe creates new BytesPipe, initialized by specified slice.
|
|
|
|
// If buf is nil, then it will be initialized with slice which cap is 64.
|
|
|
|
// buf will be adjusted in a way that len(buf) == 0, cap(buf) == cap(buf).
|
2016-03-31 16:50:50 +00:00
|
|
|
func NewBytesPipe() *BytesPipe {
|
|
|
|
bp := &BytesPipe{}
|
|
|
|
bp.buf = append(bp.buf, getBuffer(minCap))
|
2015-09-29 17:58:08 +00:00
|
|
|
bp.wait = sync.NewCond(&bp.mu)
|
|
|
|
return bp
|
2015-09-09 16:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes p to BytesPipe.
|
2015-09-17 00:51:11 +00:00
|
|
|
// It can allocate new []byte slices in a process of writing.
|
2015-09-29 17:58:08 +00:00
|
|
|
func (bp *BytesPipe) Write(p []byte) (int, error) {
|
|
|
|
bp.mu.Lock()
|
2016-03-31 16:50:50 +00:00
|
|
|
|
2015-09-29 17:58:08 +00:00
|
|
|
written := 0
|
2015-09-17 00:51:11 +00:00
|
|
|
for {
|
2015-09-29 17:58:08 +00:00
|
|
|
if bp.closeErr != nil {
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.mu.Unlock()
|
2015-09-29 17:58:08 +00:00
|
|
|
return written, ErrClosed
|
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
|
|
|
|
if len(bp.buf) == 0 {
|
|
|
|
bp.buf = append(bp.buf, getBuffer(64))
|
|
|
|
}
|
|
|
|
// get the last buffer
|
2015-09-17 00:51:11 +00:00
|
|
|
b := bp.buf[len(bp.buf)-1]
|
|
|
|
|
2016-03-31 16:50:50 +00:00
|
|
|
n, err := b.Write(p)
|
2015-09-29 17:58:08 +00:00
|
|
|
written += n
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.bufLen += n
|
|
|
|
|
|
|
|
// errBufferFull is an error we expect to get if the buffer is full
|
|
|
|
if err != nil && err != errBufferFull {
|
|
|
|
bp.wait.Broadcast()
|
|
|
|
bp.mu.Unlock()
|
|
|
|
return written, err
|
|
|
|
}
|
2015-09-29 17:58:08 +00:00
|
|
|
|
2015-09-17 00:51:11 +00:00
|
|
|
// if there was enough room to write all then break
|
|
|
|
if len(p) == n {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// more data: write to the next slice
|
|
|
|
p = p[n:]
|
2015-09-29 17:58:08 +00:00
|
|
|
|
2016-03-31 16:50:50 +00:00
|
|
|
// make sure the buffer doesn't grow too big from this write
|
2015-09-29 17:58:08 +00:00
|
|
|
for bp.bufLen >= blockThreshold {
|
|
|
|
bp.wait.Wait()
|
|
|
|
}
|
|
|
|
|
2016-03-31 16:50:50 +00:00
|
|
|
// add new byte slice to the buffers slice and continue writing
|
|
|
|
nextCap := b.Cap() * 2
|
2015-09-29 17:58:08 +00:00
|
|
|
if nextCap > maxCap {
|
2015-09-17 00:51:11 +00:00
|
|
|
nextCap = maxCap
|
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.buf = append(bp.buf, getBuffer(nextCap))
|
2015-09-17 00:51:11 +00:00
|
|
|
}
|
2015-09-29 17:58:08 +00:00
|
|
|
bp.wait.Broadcast()
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.mu.Unlock()
|
2015-09-29 17:58:08 +00:00
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWithError causes further reads from a BytesPipe to return immediately.
|
|
|
|
func (bp *BytesPipe) CloseWithError(err error) error {
|
|
|
|
bp.mu.Lock()
|
|
|
|
if err != nil {
|
|
|
|
bp.closeErr = err
|
|
|
|
} else {
|
|
|
|
bp.closeErr = io.EOF
|
|
|
|
}
|
|
|
|
bp.wait.Broadcast()
|
|
|
|
bp.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close causes further reads from a BytesPipe to return immediately.
|
|
|
|
func (bp *BytesPipe) Close() error {
|
|
|
|
return bp.CloseWithError(nil)
|
2015-09-09 16:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads bytes from BytesPipe.
|
|
|
|
// Data could be read only once.
|
|
|
|
func (bp *BytesPipe) Read(p []byte) (n int, err error) {
|
2015-09-29 17:58:08 +00:00
|
|
|
bp.mu.Lock()
|
2016-03-31 16:50:50 +00:00
|
|
|
if bp.bufLen == 0 {
|
2015-09-29 17:58:08 +00:00
|
|
|
if bp.closeErr != nil {
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.mu.Unlock()
|
2015-09-29 17:58:08 +00:00
|
|
|
return 0, bp.closeErr
|
|
|
|
}
|
|
|
|
bp.wait.Wait()
|
2016-03-31 16:50:50 +00:00
|
|
|
if bp.bufLen == 0 && bp.closeErr != nil {
|
|
|
|
bp.mu.Unlock()
|
2015-09-29 17:58:08 +00:00
|
|
|
return 0, bp.closeErr
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
|
|
|
|
for bp.bufLen > 0 {
|
|
|
|
b := bp.buf[0]
|
|
|
|
read, _ := b.Read(p) // ignore error since fixedBuffer doesn't really return an error
|
2015-09-17 00:51:11 +00:00
|
|
|
n += read
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.bufLen -= read
|
|
|
|
|
|
|
|
if b.Len() == 0 {
|
|
|
|
// it's empty so return it to the pool and move to the next one
|
|
|
|
returnBuffer(b)
|
|
|
|
bp.buf[0] = nil
|
|
|
|
bp.buf = bp.buf[1:]
|
2015-09-17 00:51:11 +00:00
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
|
2015-09-17 00:51:11 +00:00
|
|
|
if len(p) == read {
|
|
|
|
break
|
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
|
2015-09-17 00:51:11 +00:00
|
|
|
p = p[read:]
|
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
|
2015-09-29 17:58:08 +00:00
|
|
|
bp.wait.Broadcast()
|
2016-03-31 16:50:50 +00:00
|
|
|
bp.mu.Unlock()
|
2015-09-09 16:47:24 +00:00
|
|
|
return
|
|
|
|
}
|
2016-03-31 16:50:50 +00:00
|
|
|
|
|
|
|
func returnBuffer(b *fixedBuffer) {
|
|
|
|
b.Reset()
|
|
|
|
pool := bufPools[b.Cap()]
|
|
|
|
if pool != nil {
|
|
|
|
pool.Put(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBuffer(size int) *fixedBuffer {
|
|
|
|
pool, ok := bufPools[size]
|
|
|
|
if !ok {
|
|
|
|
pool = &sync.Pool{New: func() interface{} { return &fixedBuffer{buf: make([]byte, 0, size)} }}
|
|
|
|
bufPools[size] = pool
|
|
|
|
}
|
|
|
|
return pool.Get().(*fixedBuffer)
|
|
|
|
}
|