2018-01-12 17:30:54 +01:00
|
|
|
// Package accounting providers an accounting and limiting reader
|
|
|
|
package accounting
|
2013-01-03 23:50:00 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-01-14 17:12:39 +01:00
|
|
|
"unicode/utf8"
|
2015-02-19 20:26:00 +01:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-07-26 09:51:51 +02:00
|
|
|
|
2018-04-21 23:03:27 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/asyncreader"
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2013-01-03 23:50:00 +01:00
|
|
|
)
|
|
|
|
|
2018-04-21 23:03:27 +02:00
|
|
|
// ErrorMaxTransferLimitReached is returned from Read when the max
|
|
|
|
// transfer limit is reached.
|
|
|
|
var ErrorMaxTransferLimitReached = fserrors.FatalError(errors.New("Max transfer limit reached as set by --max-transfer"))
|
|
|
|
|
2013-01-03 23:50:00 +01:00
|
|
|
// Account limits and accounts for one transfer
|
|
|
|
type Account struct {
|
2019-07-16 13:56:20 +02:00
|
|
|
stats *StatsInfo
|
2015-06-09 18:29:25 +02:00
|
|
|
// The mutex is to make sure Read() and Close() aren't called
|
|
|
|
// concurrently. Unfortunately the persistent connection loop
|
|
|
|
// in http transport calls Read() after Do() returns on
|
|
|
|
// CancelRequest so this race can happen when it apparently
|
|
|
|
// shouldn't.
|
2015-09-15 16:46:06 +02:00
|
|
|
mu sync.Mutex
|
2018-02-01 16:41:58 +01:00
|
|
|
in io.Reader
|
2017-02-17 00:57:58 +01:00
|
|
|
origIn io.ReadCloser
|
2018-02-01 16:41:58 +01:00
|
|
|
close io.Closer
|
2015-09-15 16:46:06 +02:00
|
|
|
size int64
|
|
|
|
name string
|
2018-06-11 12:28:12 +02:00
|
|
|
statmu sync.Mutex // Separate mutex for stat values.
|
|
|
|
bytes int64 // Total number of bytes read
|
|
|
|
max int64 // if >=0 the max number of bytes to transfer
|
|
|
|
start time.Time // Start time of first read
|
|
|
|
lpTime time.Time // Time of last average measurement
|
|
|
|
lpBytes int // Number of bytes read since last measurement
|
|
|
|
avg float64 // Moving average of last few measurements in bytes/s
|
|
|
|
closed bool // set if the file is closed
|
|
|
|
exit chan struct{} // channel that will be closed when transfer is finished
|
|
|
|
withBuf bool // is using a buffered in
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2018-06-11 12:28:12 +02:00
|
|
|
const averagePeriod = 16 // period to do exponentially weighted averages over
|
|
|
|
|
2019-07-16 13:56:20 +02:00
|
|
|
// newAccountSizeName makes a Account reader for an io.ReadCloser of
|
2016-11-30 21:18:14 +01:00
|
|
|
// the given size and name
|
2019-07-16 13:56:20 +02:00
|
|
|
func newAccountSizeName(stats *StatsInfo, in io.ReadCloser, size int64, name string) *Account {
|
2015-09-15 16:46:06 +02:00
|
|
|
acc := &Account{
|
2019-07-16 13:56:20 +02:00
|
|
|
stats: stats,
|
2015-09-15 16:46:06 +02:00
|
|
|
in: in,
|
2018-02-01 16:41:58 +01:00
|
|
|
close: in,
|
2017-02-17 00:57:58 +01:00
|
|
|
origIn: in,
|
2016-11-30 21:18:14 +01:00
|
|
|
size: size,
|
|
|
|
name: name,
|
2015-09-15 16:46:06 +02:00
|
|
|
exit: make(chan struct{}),
|
2018-06-11 12:28:12 +02:00
|
|
|
avg: 0,
|
2015-09-15 16:46:06 +02:00
|
|
|
lpTime: time.Now(),
|
2018-04-21 23:03:27 +02:00
|
|
|
max: int64(fs.Config.MaxTransfer),
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
|
|
|
go acc.averageLoop()
|
2019-07-16 13:56:20 +02:00
|
|
|
stats.inProgress.set(acc.name, acc)
|
2015-09-15 16:46:06 +02:00
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
2017-02-17 10:15:24 +01:00
|
|
|
// WithBuffer - If the file is above a certain size it adds an Async reader
|
|
|
|
func (acc *Account) WithBuffer() *Account {
|
|
|
|
acc.withBuf = true
|
2017-02-14 20:31:33 +01:00
|
|
|
var buffers int
|
2018-01-12 17:30:54 +01:00
|
|
|
if acc.size >= int64(fs.Config.BufferSize) || acc.size == -1 {
|
|
|
|
buffers = int(int64(fs.Config.BufferSize) / asyncreader.BufferSize)
|
2017-02-14 20:31:33 +01:00
|
|
|
} else {
|
2018-01-12 17:30:54 +01:00
|
|
|
buffers = int(acc.size / asyncreader.BufferSize)
|
2017-02-14 20:31:33 +01:00
|
|
|
}
|
2016-12-14 22:15:12 +01:00
|
|
|
// On big files add a buffer
|
2017-02-14 20:31:33 +01:00
|
|
|
if buffers > 0 {
|
2018-02-01 16:41:58 +01:00
|
|
|
rc, err := asyncreader.New(acc.origIn, buffers)
|
2016-12-14 22:15:12 +01:00
|
|
|
if err != nil {
|
2018-01-12 17:30:54 +01:00
|
|
|
fs.Errorf(acc.name, "Failed to make buffer: %v", err)
|
2016-12-14 22:15:12 +01:00
|
|
|
} else {
|
2018-02-01 16:41:58 +01:00
|
|
|
acc.in = rc
|
|
|
|
acc.close = rc
|
2016-12-14 22:15:12 +01:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 00:57:58 +01:00
|
|
|
return acc
|
2016-12-14 22:15:12 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// GetReader returns the underlying io.ReadCloser under any Buffer
|
2016-12-14 22:15:12 +01:00
|
|
|
func (acc *Account) GetReader() io.ReadCloser {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
2017-02-17 00:57:58 +01:00
|
|
|
return acc.origIn
|
|
|
|
}
|
|
|
|
|
2018-08-11 10:18:19 +02:00
|
|
|
// GetAsyncReader returns the current AsyncReader or nil if Account is unbuffered
|
|
|
|
func (acc *Account) GetAsyncReader() *asyncreader.AsyncReader {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
|
|
|
if asyncIn, ok := acc.in.(*asyncreader.AsyncReader); ok {
|
|
|
|
return asyncIn
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:57:58 +01:00
|
|
|
// StopBuffering stops the async buffer doing any more buffering
|
|
|
|
func (acc *Account) StopBuffering() {
|
2018-01-12 17:30:54 +01:00
|
|
|
if asyncIn, ok := acc.in.(*asyncreader.AsyncReader); ok {
|
2017-02-17 00:57:58 +01:00
|
|
|
asyncIn.Abandon()
|
|
|
|
}
|
2016-12-14 22:15:12 +01:00
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// UpdateReader updates the underlying io.ReadCloser stopping the
|
2019-04-30 14:06:24 +02:00
|
|
|
// async buffer (if any) and re-adding it
|
2016-12-14 22:15:12 +01:00
|
|
|
func (acc *Account) UpdateReader(in io.ReadCloser) {
|
|
|
|
acc.mu.Lock()
|
2019-09-12 12:12:19 +02:00
|
|
|
if acc.withBuf {
|
|
|
|
acc.StopBuffering()
|
|
|
|
}
|
2017-02-17 10:15:24 +01:00
|
|
|
acc.in = in
|
2018-02-01 16:41:58 +01:00
|
|
|
acc.close = in
|
2017-02-17 00:57:58 +01:00
|
|
|
acc.origIn = in
|
2019-09-12 12:12:19 +02:00
|
|
|
if acc.withBuf {
|
|
|
|
acc.WithBuffer()
|
|
|
|
}
|
2016-12-14 22:15:12 +01:00
|
|
|
acc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// averageLoop calculates averages for the stats in the background
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) averageLoop() {
|
2015-09-15 16:46:06 +02:00
|
|
|
tick := time.NewTicker(time.Second)
|
2018-08-28 23:55:51 +02:00
|
|
|
var period float64
|
2015-09-15 16:46:06 +02:00
|
|
|
defer tick.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case now := <-tick.C:
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
2015-09-15 16:46:06 +02:00
|
|
|
// Add average of last second.
|
2016-08-22 22:19:38 +02:00
|
|
|
elapsed := now.Sub(acc.lpTime).Seconds()
|
|
|
|
avg := float64(acc.lpBytes) / elapsed
|
2018-08-28 23:55:51 +02:00
|
|
|
// Soft start the moving average
|
|
|
|
if period < averagePeriod {
|
|
|
|
period++
|
|
|
|
}
|
|
|
|
acc.avg = (avg + (period-1)*acc.avg) / period
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.lpBytes = 0
|
|
|
|
acc.lpTime = now
|
2015-09-15 16:46:06 +02:00
|
|
|
// Unlock stats
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Unlock()
|
|
|
|
case <-acc.exit:
|
2015-09-15 16:46:06 +02:00
|
|
|
return
|
|
|
|
}
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:04:12 +02:00
|
|
|
// Check the read is valid
|
|
|
|
func (acc *Account) checkRead() (err error) {
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
2019-07-16 13:56:20 +02:00
|
|
|
if acc.max >= 0 && acc.stats.GetBytes() >= acc.max {
|
2018-04-21 23:03:27 +02:00
|
|
|
acc.statmu.Unlock()
|
2019-04-24 18:04:12 +02:00
|
|
|
return ErrorMaxTransferLimitReached
|
2018-04-21 23:03:27 +02:00
|
|
|
}
|
|
|
|
// Set start time.
|
2016-08-22 22:19:38 +02:00
|
|
|
if acc.start.IsZero() {
|
|
|
|
acc.start = time.Now()
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Unlock()
|
2019-04-24 18:04:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-15 16:46:06 +02:00
|
|
|
|
2019-08-28 18:35:58 +02:00
|
|
|
// ServerSideCopyStart should be called at the start of a server side copy
|
|
|
|
//
|
|
|
|
// This pretends a transfer has started
|
|
|
|
func (acc *Account) ServerSideCopyStart() {
|
|
|
|
acc.statmu.Lock()
|
|
|
|
// Set start time.
|
|
|
|
if acc.start.IsZero() {
|
|
|
|
acc.start = time.Now()
|
|
|
|
}
|
|
|
|
acc.statmu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerSideCopyEnd accounts for a read of n bytes in a sever side copy
|
|
|
|
func (acc *Account) ServerSideCopyEnd(n int64) {
|
|
|
|
// Update Stats
|
|
|
|
acc.statmu.Lock()
|
|
|
|
acc.bytes += n
|
|
|
|
acc.statmu.Unlock()
|
|
|
|
|
|
|
|
acc.stats.Bytes(n)
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:04:12 +02:00
|
|
|
// Account the read and limit bandwidth
|
|
|
|
func (acc *Account) accountRead(n int) {
|
2015-09-15 16:46:06 +02:00
|
|
|
// Update Stats
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
|
|
|
acc.lpBytes += n
|
|
|
|
acc.bytes += int64(n)
|
|
|
|
acc.statmu.Unlock()
|
2015-09-15 16:46:06 +02:00
|
|
|
|
2019-07-16 13:56:20 +02:00
|
|
|
acc.stats.Bytes(int64(n))
|
2015-09-15 16:46:06 +02:00
|
|
|
|
2018-02-01 14:13:24 +01:00
|
|
|
limitBandwidth(n)
|
2019-04-24 18:04:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read bytes from the io.Reader passed in and account them
|
|
|
|
func (acc *Account) read(in io.Reader, p []byte) (n int, err error) {
|
|
|
|
err = acc.checkRead()
|
|
|
|
if err == nil {
|
|
|
|
n, err = in.Read(p)
|
|
|
|
acc.accountRead(n)
|
|
|
|
}
|
|
|
|
return n, err
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 22:19:38 +02:00
|
|
|
// Read bytes from the object - see io.Reader
|
|
|
|
func (acc *Account) Read(p []byte) (n int, err error) {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
|
|
|
return acc.read(acc.in, p)
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:04:12 +02:00
|
|
|
// AccountRead account having read n bytes
|
|
|
|
func (acc *Account) AccountRead(n int) (err error) {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
|
|
|
err = acc.checkRead()
|
|
|
|
if err == nil {
|
|
|
|
acc.accountRead(n)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// Close the object
|
|
|
|
func (acc *Account) Close() error {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
|
|
|
if acc.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
acc.closed = true
|
|
|
|
close(acc.exit)
|
2019-07-16 13:56:20 +02:00
|
|
|
acc.stats.inProgress.clear(acc.name)
|
2019-04-24 18:04:12 +02:00
|
|
|
if acc.close == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-01 16:41:58 +01:00
|
|
|
return acc.close.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// progress returns bytes read as well as the size.
|
2015-09-15 16:46:06 +02:00
|
|
|
// Size can be <= 0 if the size is unknown.
|
2018-02-01 16:41:58 +01:00
|
|
|
func (acc *Account) progress() (bytes, size int64) {
|
2016-08-22 22:19:38 +02:00
|
|
|
if acc == nil {
|
2015-09-15 16:46:06 +02:00
|
|
|
return 0, 0
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
2017-06-13 12:22:16 +02:00
|
|
|
bytes, size = acc.bytes, acc.size
|
|
|
|
acc.statmu.Unlock()
|
|
|
|
return bytes, size
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// speed returns the speed of the current file transfer
|
2015-09-15 16:46:06 +02:00
|
|
|
// in bytes per second, as well a an exponentially weighted moving average
|
|
|
|
// If no read has completed yet, 0 is returned for both values.
|
2018-02-01 16:41:58 +01:00
|
|
|
func (acc *Account) speed() (bps, current float64) {
|
2016-08-22 22:19:38 +02:00
|
|
|
if acc == nil {
|
2015-09-15 16:46:06 +02:00
|
|
|
return 0, 0
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
|
|
|
defer acc.statmu.Unlock()
|
|
|
|
if acc.bytes == 0 {
|
2015-09-15 16:46:06 +02:00
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
// Calculate speed from first read.
|
2016-08-22 22:19:38 +02:00
|
|
|
total := float64(time.Now().Sub(acc.start)) / float64(time.Second)
|
|
|
|
bps = float64(acc.bytes) / total
|
2018-06-11 12:28:12 +02:00
|
|
|
current = acc.avg
|
2015-09-15 16:46:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// eta returns the ETA of the current operation,
|
2015-09-15 16:46:06 +02:00
|
|
|
// rounded to full seconds.
|
|
|
|
// If the ETA cannot be determined 'ok' returns false.
|
2018-08-28 12:17:05 +02:00
|
|
|
func (acc *Account) eta() (etaDuration time.Duration, ok bool) {
|
|
|
|
if acc == nil {
|
2015-09-15 16:46:06 +02:00
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
|
|
|
defer acc.statmu.Unlock()
|
2018-08-28 12:17:05 +02:00
|
|
|
return eta(acc.bytes, acc.size, acc.avg)
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 17:12:39 +01:00
|
|
|
// shortenName shortens in to size runes long
|
|
|
|
// If size <= 0 then in is left untouched
|
|
|
|
func shortenName(in string, size int) string {
|
|
|
|
if size <= 0 {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
if utf8.RuneCountInString(in) <= size {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
name := []rune(in)
|
|
|
|
size-- // don't count elipsis rune
|
|
|
|
suffixLength := size / 2
|
|
|
|
prefixLength := size - suffixLength
|
|
|
|
suffixStart := len(name) - suffixLength
|
|
|
|
name = append(append(name[:prefixLength], '…'), name[suffixStart:]...)
|
|
|
|
return string(name)
|
|
|
|
}
|
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
// String produces stats for this file
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) String() string {
|
2018-02-01 16:41:58 +01:00
|
|
|
a, b := acc.progress()
|
|
|
|
_, cur := acc.speed()
|
|
|
|
eta, etaok := acc.eta()
|
2015-09-15 16:46:06 +02:00
|
|
|
etas := "-"
|
|
|
|
if etaok {
|
|
|
|
if eta > 0 {
|
|
|
|
etas = fmt.Sprintf("%v", eta)
|
|
|
|
} else {
|
|
|
|
etas = "0s"
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 13:08:24 +02:00
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
if fs.Config.DataRateUnit == "bits" {
|
2017-06-13 12:22:16 +02:00
|
|
|
cur = cur * 8
|
2016-11-22 05:04:05 +01:00
|
|
|
}
|
|
|
|
|
2017-12-08 09:02:57 +01:00
|
|
|
percentageDone := 0
|
2016-11-22 05:04:05 +01:00
|
|
|
if b > 0 {
|
2017-12-08 09:02:57 +01:00
|
|
|
percentageDone = int(100 * float64(a) / float64(b))
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
2017-12-08 09:02:57 +01:00
|
|
|
|
2019-01-14 17:12:39 +01:00
|
|
|
return fmt.Sprintf("%*s:%3d%% /%s, %s/s, %s",
|
|
|
|
fs.Config.StatsFileNameLength,
|
|
|
|
shortenName(acc.name, fs.Config.StatsFileNameLength),
|
|
|
|
percentageDone,
|
|
|
|
fs.SizeSuffix(b),
|
2018-01-12 17:30:54 +01:00
|
|
|
fs.SizeSuffix(cur),
|
2016-11-22 05:04:05 +01:00
|
|
|
etas,
|
|
|
|
)
|
2018-08-07 21:56:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteStats produces stats for this file
|
2019-07-26 09:51:51 +02:00
|
|
|
func (acc *Account) RemoteStats() (out rc.Params) {
|
|
|
|
out = make(rc.Params)
|
2018-08-07 21:56:40 +02:00
|
|
|
a, b := acc.progress()
|
|
|
|
out["bytes"] = a
|
|
|
|
out["size"] = b
|
|
|
|
spd, cur := acc.speed()
|
|
|
|
out["speed"] = spd
|
|
|
|
out["speedAvg"] = cur
|
|
|
|
|
|
|
|
eta, etaok := acc.eta()
|
|
|
|
out["eta"] = nil
|
|
|
|
if etaok {
|
|
|
|
if eta > 0 {
|
|
|
|
out["eta"] = eta.Seconds()
|
|
|
|
} else {
|
|
|
|
out["eta"] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out["name"] = acc.name
|
|
|
|
|
|
|
|
percentageDone := 0
|
|
|
|
if b > 0 {
|
|
|
|
percentageDone = int(100 * float64(a) / float64(b))
|
|
|
|
}
|
|
|
|
out["percentage"] = percentageDone
|
|
|
|
|
|
|
|
return out
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// OldStream returns the top io.Reader
|
|
|
|
func (acc *Account) OldStream() io.Reader {
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
2018-02-01 16:41:58 +01:00
|
|
|
return acc.in
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStream updates the top io.Reader
|
|
|
|
func (acc *Account) SetStream(in io.Reader) {
|
|
|
|
acc.mu.Lock()
|
|
|
|
acc.in = in
|
|
|
|
acc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapStream wraps an io Reader so it will be accounted in the same
|
|
|
|
// way as account
|
|
|
|
func (acc *Account) WrapStream(in io.Reader) io.Reader {
|
|
|
|
return &accountStream{
|
|
|
|
acc: acc,
|
|
|
|
in: in,
|
2015-10-05 23:56:16 +02:00
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// accountStream accounts a single io.Reader into a parent *Account
|
|
|
|
type accountStream struct {
|
|
|
|
acc *Account
|
|
|
|
in io.Reader
|
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// OldStream return the underlying stream
|
|
|
|
func (a *accountStream) OldStream() io.Reader {
|
|
|
|
return a.in
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStream set the underlying stream
|
|
|
|
func (a *accountStream) SetStream(in io.Reader) {
|
|
|
|
a.in = in
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapStream wrap in in an accounter
|
|
|
|
func (a *accountStream) WrapStream(in io.Reader) io.Reader {
|
|
|
|
return a.acc.WrapStream(in)
|
2016-08-22 22:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the object - see io.Reader
|
|
|
|
func (a *accountStream) Read(p []byte) (n int, err error) {
|
|
|
|
return a.acc.read(a.in, p)
|
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// Accounter accounts a stream allowing the accounting to be removed and re-added
|
|
|
|
type Accounter interface {
|
|
|
|
io.Reader
|
|
|
|
OldStream() io.Reader
|
|
|
|
SetStream(io.Reader)
|
|
|
|
WrapStream(io.Reader) io.Reader
|
2016-08-22 22:19:38 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 16:41:58 +01:00
|
|
|
// WrapFn wraps an io.Reader (for accounting purposes usually)
|
|
|
|
type WrapFn func(io.Reader) io.Reader
|
|
|
|
|
|
|
|
// UnWrap unwraps a reader returning unwrapped and wrap, a function to
|
|
|
|
// wrap it back up again. If `in` is an Accounter then this function
|
|
|
|
// will take the accounting unwrapped and wrap will put it back on
|
|
|
|
// again the new Reader passed in.
|
2016-08-22 22:19:38 +02:00
|
|
|
//
|
2018-02-01 16:41:58 +01:00
|
|
|
// This allows functions which wrap io.Readers to move the accounting
|
|
|
|
// to the end of the wrapped chain of readers. This is very important
|
|
|
|
// if buffering is being introduced and if the Reader might be wrapped
|
|
|
|
// again.
|
|
|
|
func UnWrap(in io.Reader) (unwrapped io.Reader, wrap WrapFn) {
|
|
|
|
acc, ok := in.(Accounter)
|
|
|
|
if !ok {
|
|
|
|
return in, func(r io.Reader) io.Reader { return r }
|
2016-08-22 22:19:38 +02:00
|
|
|
}
|
2018-02-01 16:41:58 +01:00
|
|
|
return acc.OldStream(), acc.WrapStream
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|