2018-01-12 17:30:54 +01:00
|
|
|
// Package asyncreader provides an asynchronous reader which reads
|
|
|
|
// independently of write
|
|
|
|
package asyncreader
|
2015-10-06 16:35:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-02-14 23:28:18 +01:00
|
|
|
"sync"
|
2016-06-12 16:06:02 +02:00
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/lib/readers"
|
2016-06-12 16:06:02 +02:00
|
|
|
"github.com/pkg/errors"
|
2015-10-06 16:35:22 +02:00
|
|
|
)
|
|
|
|
|
2017-02-17 00:55:47 +01:00
|
|
|
const (
|
2018-01-12 17:30:54 +01:00
|
|
|
// BufferSize is the default size of the async buffer
|
|
|
|
BufferSize = 1024 * 1024
|
2017-02-17 00:55:47 +01:00
|
|
|
softStartInitial = 4 * 1024
|
|
|
|
)
|
2017-02-14 23:28:18 +01:00
|
|
|
|
|
|
|
var asyncBufferPool = sync.Pool{
|
|
|
|
New: func() interface{} { return newBuffer() },
|
|
|
|
}
|
|
|
|
|
2017-03-06 20:22:17 +01:00
|
|
|
var errorStreamAbandoned = errors.New("stream abandoned")
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// AsyncReader will do async read-ahead from the input reader
|
2015-10-06 16:35:22 +02:00
|
|
|
// and make the data available as an io.Reader.
|
|
|
|
// This should be fully transparent, except that once an error
|
|
|
|
// has been returned from the Reader, it will not recover.
|
2018-01-12 17:30:54 +01:00
|
|
|
type AsyncReader struct {
|
2015-10-06 16:35:22 +02:00
|
|
|
in io.ReadCloser // Input reader
|
|
|
|
ready chan *buffer // Buffers ready to be handed to the reader
|
2017-02-14 23:28:18 +01:00
|
|
|
token chan struct{} // Tokens which allow a buffer to be taken
|
2015-10-06 16:35:22 +02:00
|
|
|
exit chan struct{} // Closes when finished
|
|
|
|
buffers int // Number of buffers
|
|
|
|
err error // If an error has occurred it is here
|
|
|
|
cur *buffer // Current buffer being served
|
|
|
|
exited chan struct{} // Channel is closed been the async reader shuts down
|
2017-02-17 00:55:47 +01:00
|
|
|
size int // size of buffer to use
|
2017-02-17 09:37:53 +01:00
|
|
|
closed bool // whether we have closed the underlying stream
|
2017-03-06 20:22:17 +01:00
|
|
|
mu sync.Mutex // lock for Read/WriteTo/Abandon/Close
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// New returns a reader that will asynchronously read from
|
|
|
|
// the supplied Reader into a number of buffers each of size BufferSize
|
2015-10-06 16:35:22 +02:00
|
|
|
// It will start reading from the input at once, maybe even before this
|
|
|
|
// function has returned.
|
|
|
|
// The input can be read from the returned reader.
|
|
|
|
// When done use Close to release the buffers and close the supplied input.
|
2018-01-12 17:30:54 +01:00
|
|
|
func New(rd io.ReadCloser, buffers int) (*AsyncReader, error) {
|
2015-10-06 16:35:22 +02:00
|
|
|
if buffers <= 0 {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.New("number of buffers too small")
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
|
|
|
if rd == nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.New("nil reader supplied")
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
a := &AsyncReader{}
|
2017-02-14 23:28:18 +01:00
|
|
|
a.init(rd, buffers)
|
2015-10-06 16:35:22 +02:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
func (a *AsyncReader) init(rd io.ReadCloser, buffers int) {
|
2015-10-06 16:35:22 +02:00
|
|
|
a.in = rd
|
|
|
|
a.ready = make(chan *buffer, buffers)
|
2017-02-14 23:28:18 +01:00
|
|
|
a.token = make(chan struct{}, buffers)
|
2015-10-06 16:35:22 +02:00
|
|
|
a.exit = make(chan struct{}, 0)
|
|
|
|
a.exited = make(chan struct{}, 0)
|
|
|
|
a.buffers = buffers
|
|
|
|
a.cur = nil
|
2017-02-17 00:55:47 +01:00
|
|
|
a.size = softStartInitial
|
2015-10-06 16:35:22 +02:00
|
|
|
|
2017-02-14 23:28:18 +01:00
|
|
|
// Create tokens
|
2015-10-06 16:35:22 +02:00
|
|
|
for i := 0; i < buffers; i++ {
|
2017-02-14 23:28:18 +01:00
|
|
|
a.token <- struct{}{}
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start async reader
|
|
|
|
go func() {
|
|
|
|
// Ensure that when we exit this is signalled.
|
|
|
|
defer close(a.exited)
|
2017-02-14 23:28:18 +01:00
|
|
|
defer close(a.ready)
|
2015-10-06 16:35:22 +02:00
|
|
|
for {
|
|
|
|
select {
|
2017-02-14 23:28:18 +01:00
|
|
|
case <-a.token:
|
|
|
|
b := a.getBuffer()
|
2018-01-12 17:30:54 +01:00
|
|
|
if a.size < BufferSize {
|
2017-02-17 00:55:47 +01:00
|
|
|
b.buf = b.buf[:a.size]
|
|
|
|
a.size <<= 1
|
|
|
|
}
|
2015-10-06 16:35:22 +02:00
|
|
|
err := b.read(a.in)
|
|
|
|
a.ready <- b
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-a.exit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2017-02-14 23:28:18 +01:00
|
|
|
// return the buffer to the pool (clearing it)
|
2018-01-12 17:30:54 +01:00
|
|
|
func (a *AsyncReader) putBuffer(b *buffer) {
|
2017-02-14 23:28:18 +01:00
|
|
|
b.clear()
|
|
|
|
asyncBufferPool.Put(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a buffer from the pool
|
2018-01-12 17:30:54 +01:00
|
|
|
func (a *AsyncReader) getBuffer() *buffer {
|
2017-02-17 00:55:47 +01:00
|
|
|
b := asyncBufferPool.Get().(*buffer)
|
|
|
|
return b
|
2017-02-14 23:28:18 +01:00
|
|
|
}
|
|
|
|
|
2015-10-06 16:35:22 +02:00
|
|
|
// Read will return the next available data.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (a *AsyncReader) fill() (err error) {
|
2015-10-06 16:35:22 +02:00
|
|
|
if a.cur.isEmpty() {
|
|
|
|
if a.cur != nil {
|
2017-02-14 23:28:18 +01:00
|
|
|
a.putBuffer(a.cur)
|
|
|
|
a.token <- struct{}{}
|
2015-10-06 16:35:22 +02:00
|
|
|
a.cur = nil
|
|
|
|
}
|
|
|
|
b, ok := <-a.ready
|
|
|
|
if !ok {
|
2017-03-06 20:22:17 +01:00
|
|
|
// Return an error to show fill failed
|
|
|
|
if a.err == nil {
|
|
|
|
return errorStreamAbandoned
|
|
|
|
}
|
2015-10-06 16:35:22 +02:00
|
|
|
return a.err
|
|
|
|
}
|
|
|
|
a.cur = b
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read will return the next available data.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (a *AsyncReader) Read(p []byte) (n int, err error) {
|
2017-03-06 20:22:17 +01:00
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
2015-10-06 16:35:22 +02:00
|
|
|
// Swap buffer and maybe return error
|
|
|
|
err = a.fill()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy what we can
|
|
|
|
n = copy(p, a.cur.buffer())
|
|
|
|
a.cur.increment(n)
|
|
|
|
|
|
|
|
// If at end of buffer, return any error, if present
|
|
|
|
if a.cur.isEmpty() {
|
|
|
|
a.err = a.cur.err
|
|
|
|
return n, a.err
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo writes data to w until there's no more data to write or when an error occurs.
|
|
|
|
// The return value n is the number of bytes written.
|
|
|
|
// Any error encountered during the write is also returned.
|
2018-01-12 17:30:54 +01:00
|
|
|
func (a *AsyncReader) WriteTo(w io.Writer) (n int64, err error) {
|
2017-03-06 20:22:17 +01:00
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
|
2015-10-06 16:35:22 +02:00
|
|
|
n = 0
|
|
|
|
for {
|
|
|
|
err = a.fill()
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
n2, err := w.Write(a.cur.buffer())
|
|
|
|
a.cur.increment(n2)
|
|
|
|
n += int64(n2)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if a.cur.err != nil {
|
|
|
|
a.err = a.cur.err
|
|
|
|
return n, a.cur.err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:37:53 +01:00
|
|
|
// Abandon will ensure that the underlying async reader is shut down.
|
2018-01-12 17:30:54 +01:00
|
|
|
// It will NOT close the input supplied on New.
|
|
|
|
func (a *AsyncReader) Abandon() {
|
2015-10-06 16:35:22 +02:00
|
|
|
select {
|
2017-02-14 23:28:18 +01:00
|
|
|
case <-a.exit:
|
2017-02-17 09:37:53 +01:00
|
|
|
// Do nothing if reader routine already exited
|
2017-02-14 23:28:18 +01:00
|
|
|
return
|
2015-10-06 16:35:22 +02:00
|
|
|
default:
|
|
|
|
}
|
2017-02-14 23:28:18 +01:00
|
|
|
// Close and wait for go routine
|
|
|
|
close(a.exit)
|
|
|
|
<-a.exited
|
2017-03-06 20:22:17 +01:00
|
|
|
// take the lock to wait for Read/WriteTo to complete
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2017-02-14 23:28:18 +01:00
|
|
|
// Return any outstanding buffers to the Pool
|
|
|
|
if a.cur != nil {
|
|
|
|
a.putBuffer(a.cur)
|
2017-02-17 09:37:53 +01:00
|
|
|
a.cur = nil
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
2017-02-14 23:28:18 +01:00
|
|
|
for b := range a.ready {
|
|
|
|
a.putBuffer(b)
|
|
|
|
}
|
2017-02-15 23:39:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close will ensure that the underlying async reader is shut down.
|
2018-01-12 17:30:54 +01:00
|
|
|
// It will also close the input supplied on New.
|
|
|
|
func (a *AsyncReader) Close() (err error) {
|
2017-02-17 09:37:53 +01:00
|
|
|
a.Abandon()
|
|
|
|
if a.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a.closed = true
|
|
|
|
return a.in.Close()
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal buffer
|
|
|
|
// If an error is present, it must be returned
|
|
|
|
// once all buffer content has been served.
|
|
|
|
type buffer struct {
|
|
|
|
buf []byte
|
|
|
|
err error
|
|
|
|
offset int
|
|
|
|
}
|
|
|
|
|
2017-02-14 23:28:18 +01:00
|
|
|
func newBuffer() *buffer {
|
|
|
|
return &buffer{
|
2018-01-12 17:30:54 +01:00
|
|
|
buf: make([]byte, BufferSize),
|
2017-02-14 23:28:18 +01:00
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear returns the buffer to its full size and clears the members
|
|
|
|
func (b *buffer) clear() {
|
|
|
|
b.buf = b.buf[:cap(b.buf)]
|
|
|
|
b.err = nil
|
|
|
|
b.offset = 0
|
2015-10-06 16:35:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// isEmpty returns true is offset is at end of
|
|
|
|
// buffer, or
|
|
|
|
func (b *buffer) isEmpty() bool {
|
|
|
|
if b == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(b.buf)-b.offset <= 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// read into start of the buffer from the supplied reader,
|
|
|
|
// resets the offset and updates the size of the buffer.
|
|
|
|
// Any error encountered during the read is returned.
|
|
|
|
func (b *buffer) read(rd io.Reader) error {
|
|
|
|
var n int
|
2018-01-12 17:30:54 +01:00
|
|
|
n, b.err = readers.ReadFill(rd, b.buf)
|
2015-10-06 16:35:22 +02:00
|
|
|
b.buf = b.buf[0:n]
|
|
|
|
b.offset = 0
|
|
|
|
return b.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the buffer at current offset
|
|
|
|
func (b *buffer) buffer() []byte {
|
|
|
|
return b.buf[b.offset:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// increment the offset
|
|
|
|
func (b *buffer) increment(n int) {
|
|
|
|
b.offset += n
|
|
|
|
}
|