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"
|
2015-02-19 19:26:00 +00:00
|
|
|
|
2015-09-15 14:46:06 +00:00
|
|
|
"github.com/VividCortex/ewma"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fs/asyncreader"
|
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
|
|
|
|
statmu sync.Mutex // Separate mutex for stat values.
|
|
|
|
bytes int64 // Total number of bytes read
|
|
|
|
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 ewma.MovingAverage // Moving average of last few measurements
|
2015-10-05 21:56:16 +00:00
|
|
|
closed bool // set if the file is closed
|
2015-09-15 14:46:06 +00:00
|
|
|
exit chan struct{} // channel that will be closed when transfer is finished
|
2017-02-16 23:57:58 +00:00
|
|
|
withBuf bool // is using a buffered in
|
2013-01-03 22:50:00 +00:00
|
|
|
}
|
|
|
|
|
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{}),
|
|
|
|
avg: ewma.NewMovingAverage(),
|
|
|
|
lpTime: time.Now(),
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// asynb 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)
|
|
|
|
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
|
|
|
|
acc.avg.Add(avg)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 20:19:38 +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) {
|
2015-09-15 14:46:06 +00:00
|
|
|
// Set start time.
|
2016-08-22 20:19:38 +00:00
|
|
|
acc.statmu.Lock()
|
|
|
|
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()
|
2015-09-15 14:46:06 +00:00
|
|
|
|
2016-08-22 20:19:38 +00:00
|
|
|
n, err = in.Read(p)
|
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)
|
2013-01-03 22:50:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
current = acc.avg.Value()
|
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-02-01 15:41:58 +00:00
|
|
|
func (acc *Account) eta() (eta time.Duration, ok bool) {
|
2016-08-22 20:19:38 +00:00
|
|
|
if acc == nil || acc.size <= 0 {
|
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()
|
|
|
|
if acc.bytes == 0 {
|
2015-09-15 14:46:06 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
left := acc.size - acc.bytes
|
2015-09-15 14:46:06 +00:00
|
|
|
if left <= 0 {
|
|
|
|
return 0, true
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
avg := acc.avg.Value()
|
2015-09-15 14:46:06 +00:00
|
|
|
if avg <= 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
seconds := float64(left) / acc.avg.Value()
|
2015-09-15 14:46:06 +00:00
|
|
|
|
|
|
|
return time.Duration(time.Second * time.Duration(int(seconds))), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 20:19:38 +00:00
|
|
|
name := []rune(acc.name)
|
2018-01-12 16:30:54 +00:00
|
|
|
if fs.Config.StatsFileNameLength > 0 {
|
|
|
|
if len(name) > fs.Config.StatsFileNameLength {
|
|
|
|
where := len(name) - fs.Config.StatsFileNameLength
|
2018-01-10 20:32:36 +00:00
|
|
|
name = append([]rune{'.', '.', '.'}, name[where:]...)
|
|
|
|
}
|
2015-09-15 14:46:06 +00:00
|
|
|
}
|
2016-11-22 04:04:05 +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
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
done := fmt.Sprintf("%2d%% /%s", percentageDone, fs.SizeSuffix(b))
|
2017-12-08 08:02:57 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf("%45s: %s, %s/s, %s",
|
2016-11-22 04:04:05 +00:00
|
|
|
string(name),
|
|
|
|
done,
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.SizeSuffix(cur),
|
2016-11-22 04:04:05 +00:00
|
|
|
etas,
|
|
|
|
)
|
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
|
|
|
}
|