2018-02-01 21:13:24 +08:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2018-04-07 02:13:27 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-02-01 21:13:24 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2018-02-01 21:13:24 +08:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
2020-07-05 00:20:54 +08:00
|
|
|
// TokenBucket holds the global token bucket limiter
|
|
|
|
var TokenBucket tokenBucket
|
|
|
|
|
|
|
|
// TokenBucketSlot is the type to select which token bucket to use
|
|
|
|
type TokenBucketSlot int
|
|
|
|
|
|
|
|
// Slots for the token bucket
|
|
|
|
const (
|
|
|
|
TokenBucketSlotAccounting TokenBucketSlot = iota
|
2020-07-05 00:48:00 +08:00
|
|
|
TokenBucketSlotTransportRx
|
|
|
|
TokenBucketSlotTransportTx
|
2020-07-05 00:20:54 +08:00
|
|
|
TokenBucketSlots
|
2018-02-01 21:13:24 +08:00
|
|
|
)
|
|
|
|
|
2020-07-05 00:20:54 +08:00
|
|
|
type buckets [TokenBucketSlots]*rate.Limiter
|
|
|
|
|
|
|
|
// tokenBucket holds info about the rate limiters in use
|
|
|
|
type tokenBucket struct {
|
|
|
|
mu sync.RWMutex // protects the token bucket variables
|
|
|
|
curr buckets
|
|
|
|
prev buckets
|
|
|
|
toggledOff bool
|
|
|
|
currLimitMu sync.Mutex // protects changes to the timeslot
|
|
|
|
currLimit fs.BwTimeSlot
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if limit is disabled
|
|
|
|
//
|
|
|
|
// Call with lock held
|
|
|
|
func (bs *buckets) _isOff() bool {
|
|
|
|
return bs[0] == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable the limits
|
|
|
|
//
|
|
|
|
// Call with lock held
|
|
|
|
func (bs *buckets) _setOff() {
|
|
|
|
for i := range bs {
|
|
|
|
bs[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 00:31:57 +08:00
|
|
|
const defaultMaxBurstSize = 4 * 1024 * 1024 // must be bigger than the biggest request
|
|
|
|
|
|
|
|
// make a new empty token bucket with the bandwidth given
|
|
|
|
func newEmptyTokenBucket(bandwidth fs.SizeSuffix) *rate.Limiter {
|
|
|
|
// Relate maxBurstSize to bandwidth limit
|
|
|
|
// 4M gives 2.5 Gb/s on Windows
|
2021-08-22 01:54:21 +08:00
|
|
|
// Use defaultMaxBurstSize up to 2GBit/s (256MiB/s) then scale
|
2021-08-11 00:31:57 +08:00
|
|
|
maxBurstSize := (bandwidth * defaultMaxBurstSize) / (256 * 1024 * 1024)
|
|
|
|
if maxBurstSize < defaultMaxBurstSize {
|
|
|
|
maxBurstSize = defaultMaxBurstSize
|
|
|
|
}
|
|
|
|
// fs.Debugf(nil, "bandwidth=%v maxBurstSize=%v", bandwidth, maxBurstSize)
|
|
|
|
tb := rate.NewLimiter(rate.Limit(bandwidth), int(maxBurstSize))
|
|
|
|
if tb != nil {
|
|
|
|
// empty the bucket
|
|
|
|
err := tb.WaitN(context.Background(), int(maxBurstSize))
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to empty token bucket: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tb
|
|
|
|
}
|
2021-02-02 06:16:19 +08:00
|
|
|
|
2020-12-08 00:19:20 +08:00
|
|
|
// make a new empty token bucket with the bandwidth(s) given
|
|
|
|
func newTokenBucket(bandwidth fs.BwPair) (tbs buckets) {
|
|
|
|
bandwidthAccounting := fs.SizeSuffix(-1)
|
|
|
|
if bandwidth.Tx > 0 {
|
2021-08-11 00:31:57 +08:00
|
|
|
tbs[TokenBucketSlotTransportTx] = newEmptyTokenBucket(bandwidth.Tx)
|
2020-12-08 00:19:20 +08:00
|
|
|
bandwidthAccounting = bandwidth.Tx
|
|
|
|
}
|
|
|
|
if bandwidth.Rx > 0 {
|
2021-08-11 00:31:57 +08:00
|
|
|
tbs[TokenBucketSlotTransportRx] = newEmptyTokenBucket(bandwidth.Rx)
|
2020-12-08 00:19:20 +08:00
|
|
|
if bandwidth.Rx > bandwidthAccounting {
|
|
|
|
bandwidthAccounting = bandwidth.Rx
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 05:15:23 +08:00
|
|
|
// Limit core bandwidth to max of Rx and Tx if both are limited
|
|
|
|
if bandwidth.Tx > 0 && bandwidth.Rx > 0 {
|
2021-08-11 00:31:57 +08:00
|
|
|
tbs[TokenBucketSlotAccounting] = newEmptyTokenBucket(bandwidthAccounting)
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
2020-12-08 00:19:20 +08:00
|
|
|
return tbs
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartTokenBucket starts the token bucket if necessary
|
2020-07-05 00:20:54 +08:00
|
|
|
func (tb *tokenBucket) StartTokenBucket(ctx context.Context) {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
2020-11-05 19:33:32 +08:00
|
|
|
ci := fs.GetConfig(ctx)
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.currLimit = ci.BwLimit.LimitAt(time.Now())
|
2020-12-08 00:19:20 +08:00
|
|
|
if tb.currLimit.Bandwidth.IsSet() {
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.curr = newTokenBucket(tb.currLimit.Bandwidth)
|
2021-08-22 01:54:21 +08:00
|
|
|
fs.Infof(nil, "Starting bandwidth limiter at %v Byte/s", &tb.currLimit.Bandwidth)
|
2018-02-01 21:13:24 +08:00
|
|
|
|
|
|
|
// Start the SIGUSR2 signal handler to toggle bandwidth.
|
|
|
|
// This function does nothing in windows systems.
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.startSignalHandler()
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartTokenTicker creates a ticker to update the bandwidth limiter every minute.
|
2020-07-05 00:20:54 +08:00
|
|
|
func (tb *tokenBucket) StartTokenTicker(ctx context.Context) {
|
2020-11-05 19:33:32 +08:00
|
|
|
ci := fs.GetConfig(ctx)
|
2018-02-01 21:13:24 +08:00
|
|
|
// If the timetable has a single entry or was not specified, we don't need
|
|
|
|
// a ticker to update the bandwidth.
|
2020-11-05 19:33:32 +08:00
|
|
|
if len(ci.BwLimit) <= 1 {
|
2018-02-01 21:13:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
2020-11-05 19:33:32 +08:00
|
|
|
limitNow := ci.BwLimit.LimitAt(time.Now())
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.currLimitMu.Lock()
|
2018-02-01 21:13:24 +08:00
|
|
|
|
2020-07-05 00:20:54 +08:00
|
|
|
if tb.currLimit.Bandwidth != limitNow.Bandwidth {
|
|
|
|
tb.mu.Lock()
|
2018-02-01 21:13:24 +08:00
|
|
|
|
|
|
|
// If bwlimit is toggled off, the change should only
|
|
|
|
// become active on the next toggle, which causes
|
2020-07-05 00:20:54 +08:00
|
|
|
// an exchange of tb.curr <-> tb.prev
|
|
|
|
var targetBucket *buckets
|
|
|
|
if tb.toggledOff {
|
|
|
|
targetBucket = &tb.prev
|
2018-02-01 21:13:24 +08:00
|
|
|
} else {
|
2020-07-05 00:20:54 +08:00
|
|
|
targetBucket = &tb.curr
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set new bandwidth. If unlimited, set tokenbucket to nil.
|
2020-12-08 00:19:20 +08:00
|
|
|
if limitNow.Bandwidth.IsSet() {
|
2018-02-01 21:13:24 +08:00
|
|
|
*targetBucket = newTokenBucket(limitNow.Bandwidth)
|
2020-07-05 00:20:54 +08:00
|
|
|
if tb.toggledOff {
|
2018-02-01 21:13:24 +08:00
|
|
|
fs.Logf(nil, "Scheduled bandwidth change. "+
|
2021-04-07 18:23:42 +08:00
|
|
|
"Limit will be set to %v Byte/s when toggled on again.", &limitNow.Bandwidth)
|
2018-02-01 21:13:24 +08:00
|
|
|
} else {
|
2021-04-07 18:23:42 +08:00
|
|
|
fs.Logf(nil, "Scheduled bandwidth change. Limit set to %v Byte/s", &limitNow.Bandwidth)
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-05 00:20:54 +08:00
|
|
|
targetBucket._setOff()
|
2018-02-01 21:13:24 +08:00
|
|
|
fs.Logf(nil, "Scheduled bandwidth change. Bandwidth limits disabled")
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.currLimit = limitNow
|
|
|
|
tb.mu.Unlock()
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.currLimitMu.Unlock()
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-02-02 06:16:19 +08:00
|
|
|
// LimitBandwidth sleeps for the correct amount of time for the passage
|
|
|
|
// of n bytes according to the current bandwidth limit
|
2020-07-05 00:20:54 +08:00
|
|
|
func (tb *tokenBucket) LimitBandwidth(i TokenBucketSlot, n int) {
|
|
|
|
tb.mu.RLock()
|
2018-02-01 21:13:24 +08:00
|
|
|
|
|
|
|
// Limit the transfer speed if required
|
2021-02-02 06:16:19 +08:00
|
|
|
if tb.curr[i] != nil {
|
|
|
|
err := tb.curr[i].WaitN(context.Background(), n)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Token bucket error: %v", err)
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 06:16:19 +08:00
|
|
|
tb.mu.RUnlock()
|
2018-02-01 21:13:24 +08:00
|
|
|
}
|
2018-03-17 05:45:09 +08:00
|
|
|
|
2018-04-24 16:43:07 +08:00
|
|
|
// SetBwLimit sets the current bandwidth limit
|
2020-12-08 00:19:20 +08:00
|
|
|
func (tb *tokenBucket) SetBwLimit(bandwidth fs.BwPair) {
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
2020-12-08 00:19:20 +08:00
|
|
|
if bandwidth.IsSet() {
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.curr = newTokenBucket(bandwidth)
|
2018-04-24 16:43:07 +08:00
|
|
|
fs.Logf(nil, "Bandwidth limit set to %v", bandwidth)
|
|
|
|
} else {
|
2020-07-05 00:20:54 +08:00
|
|
|
tb.curr._setOff()
|
2018-04-24 16:43:07 +08:00
|
|
|
fs.Logf(nil, "Bandwidth limit reset to unlimited")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:20:54 +08:00
|
|
|
// read and set the bandwidth limits
|
|
|
|
func (tb *tokenBucket) rcBwlimit(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
|
|
|
if in["rate"] != nil {
|
|
|
|
bwlimit, err := in.GetString("rate")
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
var bws fs.BwTimetable
|
|
|
|
err = bws.Set(bwlimit)
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
return out, fmt.Errorf("bad bwlimit: %w", err)
|
2020-07-05 00:20:54 +08:00
|
|
|
}
|
|
|
|
if len(bws) != 1 {
|
|
|
|
return out, errors.New("need exactly 1 bandwidth setting")
|
|
|
|
}
|
|
|
|
bw := bws[0]
|
|
|
|
tb.SetBwLimit(bw.Bandwidth)
|
|
|
|
}
|
|
|
|
tb.mu.RLock()
|
|
|
|
bytesPerSecond := int64(-1)
|
2020-12-08 00:19:20 +08:00
|
|
|
if tb.curr[TokenBucketSlotAccounting] != nil {
|
|
|
|
bytesPerSecond = int64(tb.curr[TokenBucketSlotAccounting].Limit())
|
|
|
|
}
|
|
|
|
var bp = fs.BwPair{Tx: -1, Rx: -1}
|
|
|
|
if tb.curr[TokenBucketSlotTransportTx] != nil {
|
|
|
|
bp.Tx = fs.SizeSuffix(tb.curr[TokenBucketSlotTransportTx].Limit())
|
|
|
|
}
|
|
|
|
if tb.curr[TokenBucketSlotTransportRx] != nil {
|
|
|
|
bp.Rx = fs.SizeSuffix(tb.curr[TokenBucketSlotTransportRx].Limit())
|
2020-07-05 00:20:54 +08:00
|
|
|
}
|
|
|
|
tb.mu.RUnlock()
|
|
|
|
out = rc.Params{
|
2020-12-08 00:19:20 +08:00
|
|
|
"rate": bp.String(),
|
|
|
|
"bytesPerSecond": bytesPerSecond,
|
|
|
|
"bytesPerSecondTx": int64(bp.Tx),
|
|
|
|
"bytesPerSecondRx": int64(bp.Rx),
|
2020-07-05 00:20:54 +08:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-03-17 05:45:09 +08:00
|
|
|
// Remote control for the token bucket
|
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
|
|
|
Path: "core/bwlimit",
|
2019-06-17 16:34:30 +08:00
|
|
|
Fn: func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
2020-07-05 00:20:54 +08:00
|
|
|
return TokenBucket.rcBwlimit(ctx, in)
|
2018-03-17 05:45:09 +08:00
|
|
|
},
|
|
|
|
Title: "Set the bandwidth limit.",
|
|
|
|
Help: `
|
2020-12-08 00:19:20 +08:00
|
|
|
This sets the bandwidth limit to the string passed in. This should be
|
|
|
|
a single bandwidth limit entry or a pair of upload:download bandwidth.
|
2018-03-17 05:45:09 +08:00
|
|
|
|
|
|
|
Eg
|
|
|
|
|
2018-04-24 03:44:44 +08:00
|
|
|
rclone rc core/bwlimit rate=off
|
2019-06-24 20:18:52 +08:00
|
|
|
{
|
|
|
|
"bytesPerSecond": -1,
|
2020-12-08 00:19:20 +08:00
|
|
|
"bytesPerSecondTx": -1,
|
|
|
|
"bytesPerSecondRx": -1,
|
2019-06-24 20:18:52 +08:00
|
|
|
"rate": "off"
|
|
|
|
}
|
|
|
|
rclone rc core/bwlimit rate=1M
|
|
|
|
{
|
|
|
|
"bytesPerSecond": 1048576,
|
2020-12-08 00:19:20 +08:00
|
|
|
"bytesPerSecondTx": 1048576,
|
|
|
|
"bytesPerSecondRx": 1048576,
|
|
|
|
"rate": "1M"
|
|
|
|
}
|
|
|
|
rclone rc core/bwlimit rate=1M:100k
|
|
|
|
{
|
|
|
|
"bytesPerSecond": 1048576,
|
|
|
|
"bytesPerSecondTx": 1048576,
|
|
|
|
"bytesPerSecondRx": 131072,
|
2019-06-24 20:18:52 +08:00
|
|
|
"rate": "1M"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-19 19:02:44 +08:00
|
|
|
If the rate parameter is not supplied then the bandwidth is queried
|
2019-06-24 20:18:52 +08:00
|
|
|
|
|
|
|
rclone rc core/bwlimit
|
|
|
|
{
|
|
|
|
"bytesPerSecond": 1048576,
|
2020-12-08 00:19:20 +08:00
|
|
|
"bytesPerSecondTx": 1048576,
|
|
|
|
"bytesPerSecondRx": 1048576,
|
2019-06-24 20:18:52 +08:00
|
|
|
"rate": "1M"
|
|
|
|
}
|
2018-03-17 05:45:09 +08:00
|
|
|
|
|
|
|
The format of the parameter is exactly the same as passed to --bwlimit
|
|
|
|
except only one bandwidth may be specified.
|
2019-06-24 20:18:52 +08:00
|
|
|
|
2021-11-04 19:50:43 +08:00
|
|
|
In either case "rate" is returned as a human-readable string, and
|
2019-06-24 20:18:52 +08:00
|
|
|
"bytesPerSecond" is returned as a number.
|
2018-03-17 05:45:09 +08:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|