2018-01-12 16:30:54 +00:00
|
|
|
// Package accounting providers an accounting and limiting reader
|
|
|
|
package accounting
|
2013-01-03 22:50:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2019-01-14 16:12:39 +00:00
|
|
|
"unicode/utf8"
|
2015-02-19 19:26:00 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fs/asyncreader"
|
2018-04-21 21:03:27 +00:00
|
|
|
"github.com/ncw/rclone/fs/fserrors"
|
|
|
|
"github.com/pkg/errors"
|
2013-01-03 22:50:00 +00:00
|
|
|
)
|
|
|
|
|
2018-04-21 21:03:27 +00: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 22:50:00 +00:00
|
|
|
// Account limits and accounts for one transfer
|
|
|
|
type Account struct {
|
2015-06-09 16:29:25 +00: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 14:46:06 +00:00
|
|
|
mu sync.Mutex
|
2018-02-01 15:41:58 +00:00
|
|
|
in io.Reader
|
2017-02-16 23:57:58 +00:00
|
|
|
origIn io.ReadCloser
|
2018-02-01 15:41:58 +00:00
|
|
|
close io.Closer
|
2015-09-15 14:46:06 +00:00
|
|
|
size int64
|
|
|
|
name string
|
2018-06-11 10:28:12 +00: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 22:50:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 10:28:12 +00:00
|
|
|
const averagePeriod = 16 // period to do exponentially weighted averages over
|
|
|
|
|
2016-11-30 20:18:14 +00:00
|
|
|
// NewAccountSizeName makes a Account reader for an io.ReadCloser of
|
|
|
|
// the given size and name
|
|
|
|
func NewAccountSizeName(in io.ReadCloser, size int64, name string) *Account {
|
2015-09-15 14:46:06 +00:00
|
|
|
acc := &Account{
|
|
|
|
in: in,
|
2018-02-01 15:41:58 +00:00
|
|
|
close: in,
|
2017-02-16 23:57:58 +00:00
|
|
|
origIn: in,
|
2016-11-30 20:18:14 +00:00
|
|
|
size: size,
|
|
|
|
name: name,
|
2015-09-15 14:46:06 +00:00
|
|
|
exit: make(chan struct{}),
|
2018-06-11 10:28:12 +00:00
|
|
|
avg: 0,
|
2015-09-15 14:46:06 +00:00
|
|
|
lpTime: time.Now(),
|
2018-04-21 21:03:27 +00:00
|
|
|
max: int64(fs.Config.MaxTransfer),
|
2015-09-15 14:46:06 +00:00
|
|
|
}
|
|
|
|
go acc.averageLoop()
|
|
|
|
Stats.inProgress.set(acc.name, acc)
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:18:14 +00:00
|
|
|
// NewAccount makes a Account reader for an object
|
2018-01-12 16:30:54 +00:00
|
|
|
func NewAccount(in io.ReadCloser, obj fs.Object) *Account {
|
2016-11-30 20:18:14 +00:00
|
|
|
return NewAccountSizeName(in, obj.Size(), obj.Remote())
|
|
|
|
}
|
|
|
|
|
2017-02-17 09:15:24 +00: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 19:31:33 +00:00
|
|
|
var buffers int
|
2018-01-12 16:30:54 +00:00
|
|
|
if acc.size >= int64(fs.Config.BufferSize) || acc.size == -1 {
|
|
|
|
buffers = int(int64(fs.Config.BufferSize) / asyncreader.BufferSize)
|
2017-02-14 19:31:33 +00:00
|
|
|
} else {
|
2018-01-12 16:30:54 +00:00
|
|
|
buffers = int(acc.size / asyncreader.BufferSize)
|
2017-02-14 19:31:33 +00:00
|
|
|
}
|
2016-12-14 21:15:12 +00:00
|
|
|
// On big files add a buffer
|
2017-02-14 19:31:33 +00:00
|
|
|
if buffers > 0 {
|
2018-02-01 15:41:58 +00:00
|
|
|
rc, err := asyncreader.New(acc.origIn, buffers)
|
2016-12-14 21:15:12 +00:00
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Errorf(acc.name, "Failed to make buffer: %v", err)
|
2016-12-14 21:15:12 +00:00
|
|
|
} else {
|
2018-02-01 15:41:58 +00:00
|
|
|
acc.in = rc
|
|
|
|
acc.close = rc
|
2016-12-14 21:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-16 23:57:58 +00:00
|
|
|
return acc
|
2016-12-14 21:15:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00:00
|
|
|
// GetReader returns the underlying io.ReadCloser under any Buffer
|
2016-12-14 21:15:12 +00:00
|
|
|
func (acc *Account) GetReader() io.ReadCloser {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
2017-02-16 23:57:58 +00:00
|
|
|
return acc.origIn
|
|
|
|
}
|
|
|
|
|
2018-08-11 08:18:19 +00: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-16 23:57:58 +00:00
|
|
|
// StopBuffering stops the async buffer doing any more buffering
|
|
|
|
func (acc *Account) StopBuffering() {
|
2018-01-12 16:30:54 +00:00
|
|
|
if asyncIn, ok := acc.in.(*asyncreader.AsyncReader); ok {
|
2017-02-16 23:57:58 +00:00
|
|
|
asyncIn.Abandon()
|
|
|
|
}
|
2016-12-14 21:15:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00:00
|
|
|
// UpdateReader updates the underlying io.ReadCloser stopping the
|
2019-04-30 12:06:24 +00:00
|
|
|
// async buffer (if any) and re-adding it
|
2016-12-14 21:15:12 +00:00
|
|
|
func (acc *Account) UpdateReader(in io.ReadCloser) {
|
|
|
|
acc.mu.Lock()
|
2017-02-16 23:57:58 +00:00
|
|
|
acc.StopBuffering()
|
2017-02-17 09:15:24 +00:00
|
|
|
acc.in = in
|
2018-02-01 15:41:58 +00:00
|
|
|
acc.close = in
|
2017-02-16 23:57:58 +00:00
|
|
|
acc.origIn = in
|
2017-02-17 09:15:24 +00:00
|
|
|
acc.WithBuffer()
|
2016-12-14 21:15:12 +00:00
|
|
|
acc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00:00
|
|
|
// averageLoop calculates averages for the stats in the background
|
2016-08-22 20:19:38 +00:00
|
|
|
func (acc *Account) averageLoop() {
|
2015-09-15 14:46:06 +00:00
|
|
|
tick := time.NewTicker(time.Second)
|
2018-08-28 21:55:51 +00:00
|
|
|
var period float64
|
2015-09-15 14:46:06 +00:00
|
|
|
defer tick.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case now := <-tick.C:
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
2015-09-15 14:46:06 +00:00
|
|
|
// Add average of last second.
|
2016-08-22 20:19:38 +00:00
|
|
|
elapsed := now.Sub(acc.lpTime).Seconds()
|
|
|
|
avg := float64(acc.lpBytes) / elapsed
|
2018-08-28 21:55:51 +00:00
|
|
|
// Soft start the moving average
|
|
|
|
if period < averagePeriod {
|
|
|
|
period++
|
|
|
|
}
|
|
|
|
acc.avg = (avg + (period-1)*acc.avg) / period
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.lpBytes = 0
|
|
|
|
acc.lpTime = now
|
2015-09-15 14:46:06 +00:00
|
|
|
// Unlock stats
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Unlock()
|
|
|
|
case <-acc.exit:
|
2015-09-15 14:46:06 +00:00
|
|
|
return
|
|
|
|
}
|
2013-01-03 22:50:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:04:12 +00:00
|
|
|
// Check the read is valid
|
|
|
|
func (acc *Account) checkRead() (err error) {
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
2018-04-21 21:03:27 +00:00
|
|
|
if acc.max >= 0 && Stats.GetBytes() >= acc.max {
|
|
|
|
acc.statmu.Unlock()
|
2019-04-24 16:04:12 +00:00
|
|
|
return ErrorMaxTransferLimitReached
|
2018-04-21 21:03:27 +00:00
|
|
|
}
|
|
|
|
// Set start time.
|
2016-08-22 20:19:38 +00:00
|
|
|
if acc.start.IsZero() {
|
|
|
|
acc.start = time.Now()
|
2015-09-15 14:46:06 +00:00
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Unlock()
|
2019-04-24 16:04:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-15 14:46:06 +00:00
|
|
|
|
2019-04-24 16:04:12 +00:00
|
|
|
// Account the read and limit bandwidth
|
|
|
|
func (acc *Account) accountRead(n int) {
|
2015-09-15 14:46:06 +00:00
|
|
|
// Update Stats
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
|
|
|
acc.lpBytes += n
|
|
|
|
acc.bytes += int64(n)
|
|
|
|
acc.statmu.Unlock()
|
2015-09-15 14:46:06 +00:00
|
|
|
|
2013-06-27 19:13:07 +00:00
|
|
|
Stats.Bytes(int64(n))
|
2015-09-15 14:46:06 +00:00
|
|
|
|
2018-02-01 13:13:24 +00:00
|
|
|
limitBandwidth(n)
|
2019-04-24 16:04:12 +00: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 22:50:00 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 20:19:38 +00: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 16:04:12 +00: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 15:41:58 +00: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)
|
|
|
|
Stats.inProgress.clear(acc.name)
|
2019-04-24 16:04:12 +00:00
|
|
|
if acc.close == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-01 15:41:58 +00:00
|
|
|
return acc.close.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// progress returns bytes read as well as the size.
|
2015-09-15 14:46:06 +00:00
|
|
|
// Size can be <= 0 if the size is unknown.
|
2018-02-01 15:41:58 +00:00
|
|
|
func (acc *Account) progress() (bytes, size int64) {
|
2016-08-22 20:19:38 +00:00
|
|
|
if acc == nil {
|
2015-09-15 14:46:06 +00:00
|
|
|
return 0, 0
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
2017-06-13 10:22:16 +00:00
|
|
|
bytes, size = acc.bytes, acc.size
|
|
|
|
acc.statmu.Unlock()
|
|
|
|
return bytes, size
|
2015-09-15 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00:00
|
|
|
// speed returns the speed of the current file transfer
|
2015-09-15 14:46:06 +00: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 15:41:58 +00:00
|
|
|
func (acc *Account) speed() (bps, current float64) {
|
2016-08-22 20:19:38 +00:00
|
|
|
if acc == nil {
|
2015-09-15 14:46:06 +00:00
|
|
|
return 0, 0
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
|
|
|
defer acc.statmu.Unlock()
|
|
|
|
if acc.bytes == 0 {
|
2015-09-15 14:46:06 +00:00
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
// Calculate speed from first read.
|
2016-08-22 20:19:38 +00:00
|
|
|
total := float64(time.Now().Sub(acc.start)) / float64(time.Second)
|
|
|
|
bps = float64(acc.bytes) / total
|
2018-06-11 10:28:12 +00:00
|
|
|
current = acc.avg
|
2015-09-15 14:46:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00:00
|
|
|
// eta returns the ETA of the current operation,
|
2015-09-15 14:46:06 +00:00
|
|
|
// rounded to full seconds.
|
|
|
|
// If the ETA cannot be determined 'ok' returns false.
|
2018-08-28 10:17:05 +00:00
|
|
|
func (acc *Account) eta() (etaDuration time.Duration, ok bool) {
|
|
|
|
if acc == nil {
|
2015-09-15 14:46:06 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
|
|
|
defer acc.statmu.Unlock()
|
2018-08-28 10:17:05 +00:00
|
|
|
return eta(acc.bytes, acc.size, acc.avg)
|
2015-09-15 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 16:12:39 +00: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 14:46:06 +00:00
|
|
|
// String produces stats for this file
|
2016-08-22 20:19:38 +00:00
|
|
|
func (acc *Account) String() string {
|
2018-02-01 15:41:58 +00:00
|
|
|
a, b := acc.progress()
|
|
|
|
_, cur := acc.speed()
|
|
|
|
eta, etaok := acc.eta()
|
2015-09-15 14:46:06 +00:00
|
|
|
etas := "-"
|
|
|
|
if etaok {
|
|
|
|
if eta > 0 {
|
|
|
|
etas = fmt.Sprintf("%v", eta)
|
|
|
|
} else {
|
|
|
|
etas = "0s"
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 11:08:24 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
if fs.Config.DataRateUnit == "bits" {
|
2017-06-13 10:22:16 +00:00
|
|
|
cur = cur * 8
|
2016-11-22 04:04:05 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:02:57 +00:00
|
|
|
percentageDone := 0
|
2016-11-22 04:04:05 +00:00
|
|
|
if b > 0 {
|
2017-12-08 08:02:57 +00:00
|
|
|
percentageDone = int(100 * float64(a) / float64(b))
|
2015-09-15 14:46:06 +00:00
|
|
|
}
|
2017-12-08 08:02:57 +00:00
|
|
|
|
2019-01-14 16:12:39 +00: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 16:30:54 +00:00
|
|
|
fs.SizeSuffix(cur),
|
2016-11-22 04:04:05 +00:00
|
|
|
etas,
|
|
|
|
)
|
2018-08-07 19:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteStats produces stats for this file
|
|
|
|
func (acc *Account) RemoteStats() (out map[string]interface{}) {
|
|
|
|
out = make(map[string]interface{})
|
|
|
|
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 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00:00
|
|
|
// OldStream returns the top io.Reader
|
|
|
|
func (acc *Account) OldStream() io.Reader {
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
2018-02-01 15:41:58 +00: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 21:56:16 +00:00
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// accountStream accounts a single io.Reader into a parent *Account
|
|
|
|
type accountStream struct {
|
|
|
|
acc *Account
|
|
|
|
in io.Reader
|
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00: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 20:19:38 +00: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 15:41:58 +00: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 20:19:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:41:58 +00: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 20:19:38 +00:00
|
|
|
//
|
2018-02-01 15:41:58 +00: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 20:19:38 +00:00
|
|
|
}
|
2018-02-01 15:41:58 +00:00
|
|
|
return acc.OldStream(), acc.WrapStream
|
2013-01-03 22:50:00 +00:00
|
|
|
}
|