2013-01-03 23:50:00 +01:00
|
|
|
// Accounting and limiting reader
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
package fs
|
2013-01-03 23:50:00 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-15 16:46:06 +02:00
|
|
|
"sort"
|
2013-01-03 23:50:00 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2015-02-19 20:26:00 +01:00
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
"github.com/VividCortex/ewma"
|
2015-02-19 20:26:00 +01:00
|
|
|
"github.com/tsenart/tb"
|
2013-01-03 23:50:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
2016-10-22 09:21:28 +02:00
|
|
|
Stats = NewStats()
|
2016-10-30 20:20:16 +01:00
|
|
|
tokenBucketMu sync.Mutex // protects the token bucket variables
|
2016-10-22 09:21:28 +02:00
|
|
|
tokenBucket *tb.Bucket
|
|
|
|
prevTokenBucket = tokenBucket
|
2017-01-03 03:52:41 +01:00
|
|
|
currLimitMu sync.Mutex // protects changes to the timeslot
|
|
|
|
currLimit BwTimeSlot
|
2013-01-03 23:50:00 +01:00
|
|
|
)
|
|
|
|
|
2015-02-19 20:26:00 +01:00
|
|
|
// Start the token bucket if necessary
|
|
|
|
func startTokenBucket() {
|
2017-01-03 03:52:41 +01:00
|
|
|
currLimitMu.Lock()
|
|
|
|
currLimit := bwLimit.LimitAt(time.Now())
|
|
|
|
currLimitMu.Unlock()
|
|
|
|
|
|
|
|
if currLimit.bandwidth > 0 {
|
|
|
|
tokenBucket = tb.NewBucket(int64(currLimit.bandwidth), 100*time.Millisecond)
|
2017-02-09 18:08:51 +01:00
|
|
|
Infof(nil, "Starting bandwidth limiter at %vBytes/s", &currLimit.bandwidth)
|
2016-10-22 09:21:28 +02:00
|
|
|
|
2016-10-30 20:20:16 +01:00
|
|
|
// Start the SIGUSR2 signal handler to toggle bandwidth.
|
|
|
|
// This function does nothing in windows systems.
|
|
|
|
startSignalHandler()
|
|
|
|
}
|
2015-02-19 20:26:00 +01:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:52:41 +01:00
|
|
|
// startTokenTicker creates a ticker to update the bandwidth limiter every minute.
|
|
|
|
func startTokenTicker() {
|
2017-01-05 04:03:49 +01:00
|
|
|
// If the timetable has a single entry or was not specified, we don't need
|
|
|
|
// a ticker to update the bandwidth.
|
|
|
|
if len(bwLimit) <= 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-03 03:52:41 +01:00
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
|
|
|
limitNow := bwLimit.LimitAt(time.Now())
|
|
|
|
currLimitMu.Lock()
|
|
|
|
|
|
|
|
if currLimit.bandwidth != limitNow.bandwidth {
|
|
|
|
tokenBucketMu.Lock()
|
|
|
|
if tokenBucket != nil {
|
|
|
|
err := tokenBucket.Close()
|
|
|
|
if err != nil {
|
2017-02-09 18:08:51 +01:00
|
|
|
Debugf(nil, "Error closing token bucket: %v", err)
|
2017-01-03 03:52:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new bandwidth. If unlimited, set tokenbucket to nil.
|
|
|
|
if limitNow.bandwidth > 0 {
|
|
|
|
tokenBucket = tb.NewBucket(int64(limitNow.bandwidth), 100*time.Millisecond)
|
2017-02-09 12:01:20 +01:00
|
|
|
Logf(nil, "Scheduled bandwidth change. Limit set to %vBytes/s", &limitNow.bandwidth)
|
2017-01-03 03:52:41 +01:00
|
|
|
} else {
|
|
|
|
tokenBucket = nil
|
2017-02-09 12:01:20 +01:00
|
|
|
Logf(nil, "Scheduled bandwidth change. Bandwidth limits disabled")
|
2017-01-03 03:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
currLimit = limitNow
|
|
|
|
tokenBucketMu.Unlock()
|
|
|
|
}
|
|
|
|
currLimitMu.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
// stringSet holds a set of strings
|
|
|
|
type stringSet map[string]struct{}
|
2013-01-03 23:50:00 +01:00
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
// inProgress holds a synchronizes map of in progress transfers
|
|
|
|
type inProgress struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
m map[string]*Account
|
|
|
|
}
|
|
|
|
|
|
|
|
// newInProgress makes a new inProgress object
|
|
|
|
func newInProgress() *inProgress {
|
|
|
|
return &inProgress{
|
|
|
|
m: make(map[string]*Account, Config.Transfers),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set marks the name as in progress
|
|
|
|
func (ip *inProgress) set(name string, acc *Account) {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
ip.m[name] = acc
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear marks the name as no longer in progress
|
|
|
|
func (ip *inProgress) clear(name string) {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
delete(ip.m, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get gets the account for name, of nil if not found
|
|
|
|
func (ip *inProgress) get(name string) *Account {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
return ip.m[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strings returns all the strings in the stringSet
|
|
|
|
func (ss stringSet) Strings() []string {
|
2013-01-03 23:50:00 +01:00
|
|
|
strings := make([]string, 0, len(ss))
|
2015-09-22 19:47:16 +02:00
|
|
|
for name := range ss {
|
2015-09-15 16:46:06 +02:00
|
|
|
var out string
|
|
|
|
if acc := Stats.inProgress.get(name); acc != nil {
|
|
|
|
out = acc.String()
|
|
|
|
} else {
|
|
|
|
out = name
|
|
|
|
}
|
|
|
|
strings = append(strings, " * "+out)
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
2015-09-15 16:46:06 +02:00
|
|
|
sorted := sort.StringSlice(strings)
|
|
|
|
sorted.Sort()
|
|
|
|
return sorted
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
// String returns all the file names in the stringSet joined by newline
|
|
|
|
func (ss stringSet) String() string {
|
|
|
|
return strings.Join(ss.Strings(), "\n")
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// StatsInfo limits and accounts all transfers
|
2013-06-27 21:13:07 +02:00
|
|
|
type StatsInfo struct {
|
2013-01-03 23:50:00 +01:00
|
|
|
lock sync.RWMutex
|
|
|
|
bytes int64
|
|
|
|
errors int64
|
|
|
|
checks int64
|
2015-09-15 16:46:06 +02:00
|
|
|
checking stringSet
|
2013-01-03 23:50:00 +01:00
|
|
|
transfers int64
|
2015-09-15 16:46:06 +02:00
|
|
|
transferring stringSet
|
2013-01-03 23:50:00 +01:00
|
|
|
start time.Time
|
2015-09-15 16:46:06 +02:00
|
|
|
inProgress *inProgress
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// NewStats cretates an initialised StatsInfo
|
|
|
|
func NewStats() *StatsInfo {
|
|
|
|
return &StatsInfo{
|
2015-09-15 16:46:06 +02:00
|
|
|
checking: make(stringSet, Config.Checkers),
|
|
|
|
transferring: make(stringSet, Config.Transfers),
|
2013-01-03 23:50:00 +01:00
|
|
|
start: time.Now(),
|
2015-09-15 16:46:06 +02:00
|
|
|
inProgress: newInProgress(),
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// String convert the StatsInfo to a string for printing
|
|
|
|
func (s *StatsInfo) String() string {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
2013-06-27 21:13:07 +02:00
|
|
|
dt := time.Now().Sub(s.start)
|
2015-09-22 19:47:16 +02:00
|
|
|
dtSeconds := dt.Seconds()
|
2013-01-03 23:50:00 +01:00
|
|
|
speed := 0.0
|
|
|
|
if dt > 0 {
|
2016-06-18 17:40:22 +02:00
|
|
|
speed = float64(s.bytes) / dtSeconds
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
2015-09-22 19:47:16 +02:00
|
|
|
dtRounded := dt - (dt % (time.Second / 10))
|
2013-01-03 23:50:00 +01:00
|
|
|
buf := &bytes.Buffer{}
|
2016-11-22 05:04:05 +01:00
|
|
|
|
|
|
|
if Config.DataRateUnit == "bits" {
|
|
|
|
speed = speed * 8
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:50:00 +01:00
|
|
|
fmt.Fprintf(buf, `
|
2016-07-11 14:04:30 +02:00
|
|
|
Transferred: %10s (%s)
|
2013-01-03 23:50:00 +01:00
|
|
|
Errors: %10d
|
|
|
|
Checks: %10d
|
|
|
|
Transferred: %10d
|
2015-09-15 16:46:06 +02:00
|
|
|
Elapsed time: %10v
|
2013-01-03 23:50:00 +01:00
|
|
|
`,
|
2016-11-22 05:04:05 +01:00
|
|
|
SizeSuffix(s.bytes).Unit("Bytes"), SizeSuffix(speed).Unit(strings.Title(Config.DataRateUnit)+"/s"),
|
2013-06-27 21:13:07 +02:00
|
|
|
s.errors,
|
|
|
|
s.checks,
|
|
|
|
s.transfers,
|
2015-09-22 19:47:16 +02:00
|
|
|
dtRounded)
|
2013-01-03 23:50:00 +01:00
|
|
|
if len(s.checking) > 0 {
|
2015-09-15 16:46:06 +02:00
|
|
|
fmt.Fprintf(buf, "Checking:\n%s\n", s.checking)
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
if len(s.transferring) > 0 {
|
2015-09-15 16:46:06 +02:00
|
|
|
fmt.Fprintf(buf, "Transferring:\n%s\n", s.transferring)
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Log outputs the StatsInfo to the log
|
|
|
|
func (s *StatsInfo) Log() {
|
2017-02-09 18:08:51 +01:00
|
|
|
Infof(nil, "%v\n", s)
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes updates the stats for bytes bytes
|
2013-06-27 21:13:07 +02:00
|
|
|
func (s *StatsInfo) Bytes(bytes int64) {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.bytes += bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errors updates the stats for errors
|
2013-06-27 21:13:07 +02:00
|
|
|
func (s *StatsInfo) Errors(errors int64) {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
s.errors += errors
|
|
|
|
}
|
|
|
|
|
2013-07-02 09:33:17 +02:00
|
|
|
// GetErrors reads the number of errors
|
|
|
|
func (s *StatsInfo) GetErrors() int64 {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.errors
|
|
|
|
}
|
|
|
|
|
2015-06-06 09:38:45 +02:00
|
|
|
// ResetCounters sets the counters (bytes, checks, errors, transfers) to 0
|
|
|
|
func (s *StatsInfo) ResetCounters() {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
s.bytes = 0
|
|
|
|
s.errors = 0
|
|
|
|
s.checks = 0
|
|
|
|
s.transfers = 0
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:07:00 +02:00
|
|
|
// ResetErrors sets the errors count to 0
|
|
|
|
func (s *StatsInfo) ResetErrors() {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
s.errors = 0
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Errored returns whether there have been any errors
|
|
|
|
func (s *StatsInfo) Errored() bool {
|
2013-07-02 09:33:17 +02:00
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
2013-06-27 21:13:07 +02:00
|
|
|
return s.errors != 0
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:50:00 +01:00
|
|
|
// Error adds a single error into the stats
|
2013-06-27 21:13:07 +02:00
|
|
|
func (s *StatsInfo) Error() {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
2015-09-22 19:47:16 +02:00
|
|
|
s.errors++
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checking adds a check into the stats
|
2016-07-02 17:58:50 +02:00
|
|
|
func (s *StatsInfo) Checking(remote string) {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
2016-07-02 17:58:50 +02:00
|
|
|
s.checking[remote] = struct{}{}
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoneChecking removes a check from the stats
|
2016-07-02 17:58:50 +02:00
|
|
|
func (s *StatsInfo) DoneChecking(remote string) {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
2016-07-02 17:58:50 +02:00
|
|
|
delete(s.checking, remote)
|
2015-09-22 19:47:16 +02:00
|
|
|
s.checks++
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2015-06-03 16:08:27 +02:00
|
|
|
// GetTransfers reads the number of transfers
|
|
|
|
func (s *StatsInfo) GetTransfers() int64 {
|
|
|
|
s.lock.RLock()
|
|
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.transfers
|
|
|
|
}
|
|
|
|
|
2013-01-03 23:50:00 +01:00
|
|
|
// Transferring adds a transfer into the stats
|
2016-07-02 17:58:50 +02:00
|
|
|
func (s *StatsInfo) Transferring(remote string) {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
2016-07-02 17:58:50 +02:00
|
|
|
s.transferring[remote] = struct{}{}
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DoneTransferring removes a transfer from the stats
|
2016-09-12 19:15:58 +02:00
|
|
|
//
|
|
|
|
// if ok is true then it increments the transfers count
|
|
|
|
func (s *StatsInfo) DoneTransferring(remote string, ok bool) {
|
2013-01-03 23:50:00 +01:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
2016-07-02 17:58:50 +02:00
|
|
|
delete(s.transferring, remote)
|
2016-09-12 19:15:58 +02:00
|
|
|
if ok {
|
|
|
|
s.transfers++
|
|
|
|
}
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account limits and accounts for one transfer
|
|
|
|
type Account struct {
|
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
|
|
|
|
in io.ReadCloser
|
2017-02-17 00:57:58 +01:00
|
|
|
origIn io.ReadCloser
|
2015-09-15 16:46:06 +02: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 23:56:16 +02:00
|
|
|
closed bool // set if the file is closed
|
2015-09-15 16:46:06 +02:00
|
|
|
exit chan struct{} // channel that will be closed when transfer is finished
|
2017-02-17 00:57:58 +01:00
|
|
|
withBuf bool // is using a buffered in
|
2016-08-22 22:19:38 +02:00
|
|
|
|
|
|
|
wholeFileDisabled bool // disables the whole file when doing parts
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 21:18:14 +01: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 16:46:06 +02:00
|
|
|
acc := &Account{
|
|
|
|
in: 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{}),
|
|
|
|
avg: ewma.NewMovingAverage(),
|
|
|
|
lpTime: time.Now(),
|
|
|
|
}
|
|
|
|
go acc.averageLoop()
|
|
|
|
Stats.inProgress.set(acc.name, acc)
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
2016-11-30 21:18:14 +01:00
|
|
|
// NewAccount makes a Account reader for an object
|
|
|
|
func NewAccount(in io.ReadCloser, obj Object) *Account {
|
|
|
|
return NewAccountSizeName(in, obj.Size(), obj.Remote())
|
|
|
|
}
|
|
|
|
|
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
|
2017-02-17 10:15:24 +01:00
|
|
|
if acc.size >= int64(Config.BufferSize) {
|
2017-02-14 23:28:18 +01:00
|
|
|
buffers = int(int64(Config.BufferSize) / asyncBufferSize)
|
2017-02-14 20:31:33 +01:00
|
|
|
} else {
|
2017-02-17 10:15:24 +01:00
|
|
|
buffers = int(acc.size / asyncBufferSize)
|
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 {
|
2017-02-17 10:15:24 +01:00
|
|
|
in, err := newAsyncReader(acc.in, buffers)
|
2016-12-14 22:15:12 +01:00
|
|
|
if err != nil {
|
2017-02-17 10:15:24 +01:00
|
|
|
Errorf(acc.name, "Failed to make buffer: %v", err)
|
2016-12-14 22:15:12 +01:00
|
|
|
} else {
|
2017-02-17 10:15:24 +01:00
|
|
|
acc.in = in
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// GetReader returns the underlying io.ReadCloser
|
|
|
|
func (acc *Account) GetReader() io.ReadCloser {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
2017-02-17 00:57:58 +01:00
|
|
|
return acc.origIn
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopBuffering stops the async buffer doing any more buffering
|
|
|
|
func (acc *Account) StopBuffering() {
|
|
|
|
if asyncIn, ok := acc.in.(*asyncReader); ok {
|
|
|
|
asyncIn.Abandon()
|
|
|
|
}
|
2016-12-14 22:15:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateReader updates the underlying io.ReadCloser
|
|
|
|
func (acc *Account) UpdateReader(in io.ReadCloser) {
|
|
|
|
acc.mu.Lock()
|
2017-02-17 00:57:58 +01:00
|
|
|
acc.StopBuffering()
|
2017-02-17 10:15:24 +01:00
|
|
|
acc.in = in
|
2017-02-17 00:57:58 +01:00
|
|
|
acc.origIn = in
|
2017-02-17 10:15:24 +01:00
|
|
|
acc.WithBuffer()
|
2016-12-14 22:15:12 +01:00
|
|
|
acc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:19:38 +02:00
|
|
|
// disableWholeFileAccounting turns off the whole file accounting
|
|
|
|
func (acc *Account) disableWholeFileAccounting() {
|
|
|
|
acc.mu.Lock()
|
|
|
|
acc.wholeFileDisabled = true
|
|
|
|
acc.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountPart disables the whole file counter and returns an
|
|
|
|
// io.Reader to wrap a segment of the transfer.
|
|
|
|
func (acc *Account) accountPart(in io.Reader) io.Reader {
|
|
|
|
return newAccountStream(acc, in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (acc *Account) averageLoop() {
|
2015-09-15 16:46:06 +02:00
|
|
|
tick := time.NewTicker(time.Second)
|
|
|
|
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
|
|
|
|
acc.avg.Add(avg)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:19:38 +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) {
|
2015-09-15 16:46:06 +02:00
|
|
|
// Set start time.
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.statmu.Lock()
|
|
|
|
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()
|
2015-09-15 16:46:06 +02:00
|
|
|
|
2016-08-22 22:19:38 +02:00
|
|
|
n, err = in.Read(p)
|
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
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
Stats.Bytes(int64(n))
|
2015-09-15 16:46:06 +02:00
|
|
|
|
2016-10-30 20:20:16 +01:00
|
|
|
// Get the token bucket in use
|
|
|
|
tokenBucketMu.Lock()
|
|
|
|
tb := tokenBucket
|
2016-10-22 09:21:28 +02:00
|
|
|
|
2015-02-19 20:26:00 +01:00
|
|
|
// Limit the transfer speed if required
|
2016-10-30 20:20:16 +01:00
|
|
|
if tb != nil {
|
|
|
|
tb.Wait(int64(n))
|
2015-02-19 20:26:00 +01:00
|
|
|
}
|
2017-01-03 03:52:41 +01:00
|
|
|
tokenBucketMu.Unlock()
|
2013-01-03 23:50:00 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
if acc.wholeFileDisabled {
|
|
|
|
// Don't account
|
|
|
|
return acc.in.Read(p)
|
|
|
|
}
|
|
|
|
return acc.read(acc.in, p)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// 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.
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) Progress() (bytes, size int64) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Speed returns the speed of the current file transfer
|
|
|
|
// in bytes per second, as well a an exponentially weighted moving average
|
|
|
|
// If no read has completed yet, 0 is returned for both values.
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) Speed() (bps, current float64) {
|
|
|
|
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
|
|
|
|
current = acc.avg.Value()
|
2015-09-15 16:46:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ETA returns the ETA of the current operation,
|
|
|
|
// rounded to full seconds.
|
|
|
|
// If the ETA cannot be determined 'ok' returns false.
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) ETA() (eta time.Duration, ok bool) {
|
|
|
|
if acc == nil || acc.size <= 0 {
|
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()
|
|
|
|
if acc.bytes == 0 {
|
2015-09-15 16:46:06 +02:00
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
left := acc.size - acc.bytes
|
2015-09-15 16:46:06 +02:00
|
|
|
if left <= 0 {
|
|
|
|
return 0, true
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
avg := acc.avg.Value()
|
2015-09-15 16:46:06 +02:00
|
|
|
if avg <= 0 {
|
|
|
|
return 0, false
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
seconds := float64(left) / acc.avg.Value()
|
2015-09-15 16:46:06 +02:00
|
|
|
|
|
|
|
return time.Duration(time.Second * time.Duration(int(seconds))), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// String produces stats for this file
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) String() string {
|
|
|
|
a, b := acc.Progress()
|
2017-06-13 12:22:16 +02:00
|
|
|
_, cur := acc.Speed()
|
2016-08-22 22:19:38 +02:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
name := []rune(acc.name)
|
2015-09-17 15:53:45 +02:00
|
|
|
if len(name) > 45 {
|
|
|
|
where := len(name) - 42
|
2015-09-17 19:42:39 +02:00
|
|
|
name = append([]rune{'.', '.', '.'}, name[where:]...)
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
2016-11-22 05:04:05 +01:00
|
|
|
|
|
|
|
if Config.DataRateUnit == "bits" {
|
2017-06-13 12:22:16 +02:00
|
|
|
cur = cur * 8
|
2016-11-22 05:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done := ""
|
|
|
|
if b > 0 {
|
|
|
|
done = fmt.Sprintf("%2d%% done, ", int(100*float64(a)/float64(b)))
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
2016-11-22 05:04:05 +01:00
|
|
|
return fmt.Sprintf("%45s: %s%s, ETA: %s",
|
|
|
|
string(name),
|
|
|
|
done,
|
|
|
|
SizeSuffix(cur).Unit(strings.Title(Config.DataRateUnit)+"/s"),
|
|
|
|
etas,
|
|
|
|
)
|
2015-09-15 16:46:06 +02:00
|
|
|
}
|
|
|
|
|
2013-01-03 23:50:00 +01:00
|
|
|
// Close the object
|
2016-08-22 22:19:38 +02:00
|
|
|
func (acc *Account) Close() error {
|
|
|
|
acc.mu.Lock()
|
|
|
|
defer acc.mu.Unlock()
|
|
|
|
if acc.closed {
|
2015-10-05 23:56:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-22 22:19:38 +02:00
|
|
|
acc.closed = true
|
|
|
|
close(acc.exit)
|
|
|
|
Stats.inProgress.clear(acc.name)
|
|
|
|
return acc.in.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountStream accounts a single io.Reader into a parent *Account
|
|
|
|
type accountStream struct {
|
|
|
|
acc *Account
|
|
|
|
in io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAccountStream makes a new accountStream for an in
|
|
|
|
func newAccountStream(acc *Account, in io.Reader) *accountStream {
|
|
|
|
return &accountStream{
|
|
|
|
acc: acc,
|
|
|
|
in: in,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountByPart turns off whole file accounting
|
|
|
|
//
|
|
|
|
// Returns the current account or nil if not found
|
|
|
|
func AccountByPart(obj Object) *Account {
|
|
|
|
acc := Stats.inProgress.get(obj.Remote())
|
|
|
|
if acc == nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
Debugf(obj, "Didn't find object to account part transfer")
|
2016-09-05 19:10:01 +02:00
|
|
|
return nil
|
2016-08-22 22:19:38 +02:00
|
|
|
}
|
|
|
|
acc.disableWholeFileAccounting()
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountPart accounts for part of a transfer
|
|
|
|
//
|
|
|
|
// It disables the whole file counter and returns an io.Reader to wrap
|
|
|
|
// a segment of the transfer.
|
|
|
|
func AccountPart(obj Object, in io.Reader) io.Reader {
|
|
|
|
acc := AccountByPart(obj)
|
|
|
|
if acc == nil {
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
return acc.accountPart(in)
|
2013-01-03 23:50:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check it satisfies the interface
|
2016-08-22 22:19:38 +02:00
|
|
|
var (
|
|
|
|
_ io.ReadCloser = &Account{}
|
|
|
|
_ io.Reader = &accountStream{}
|
|
|
|
)
|