Skip to content

Commit

Permalink
operations: fix accounting for server side copies
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed Aug 28, 2019
1 parent 693112d commit 7560f92
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 22 additions & 0 deletions fs/accounting/accounting.go
Expand Up @@ -168,6 +168,28 @@ func (acc *Account) checkRead() (err error) {
return nil
}

// 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)
}

// Account the read and limit bandwidth
func (acc *Account) accountRead(n int) {
// Update Stats
Expand Down
5 changes: 4 additions & 1 deletion fs/operations/operations.go
Expand Up @@ -302,11 +302,14 @@ func Copy(ctx context.Context, f fs.Fs, dst fs.Object, remote string, src fs.Obj
if fs.Config.MaxTransfer >= 0 && accounting.Stats(ctx).GetBytes() >= int64(fs.Config.MaxTransfer) {
return nil, accounting.ErrorMaxTransferLimitReached
}
in := tr.Account(nil) // account the transfer
in.ServerSideCopyStart()
newDst, err = doCopy(ctx, src, remote)
if err == nil {
dst = newDst
accounting.Stats(ctx).Bytes(dst.Size()) // account the bytes for the server side transfer
in.ServerSideCopyEnd(dst.Size()) // account the bytes for the server side transfer
}
err = in.Close()
} else {
err = fs.ErrorCantCopy
}
Expand Down

0 comments on commit 7560f92

Please sign in to comment.