2018-02-01 13:13:24 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2018-04-06 18:13:27 +00:00
|
|
|
"context"
|
2021-11-04 10:12:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-02-01 13:13:24 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2018-02-01 13:13:24 +00:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
2020-07-04 16:20:54 +00: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-04 16:48:00 +00:00
|
|
|
TokenBucketSlotTransportRx
|
|
|
|
TokenBucketSlotTransportTx
|
2020-07-04 16:20:54 +00:00
|
|
|
TokenBucketSlots
|
2018-02-01 13:13:24 +00:00
|
|
|
)
|
|
|
|
|
2020-07-04 16:20:54 +00:00
|
|
|
type buckets [TokenBucketSlots]*rate.Limiter
|
|
|
|
|
|
|
|
// tokenBucket holds info about the rate limiters in use
|
|
|
|
type tokenBucket struct {
|
2023-06-22 16:59:24 +00:00
|
|
|
mu sync.RWMutex // protects the token bucket variables
|
|
|
|
curr buckets
|
|
|
|
prev buckets
|
|
|
|
toggledOff bool
|
|
|
|
currLimit fs.BwTimeSlot
|
2020-07-04 16:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if limit is disabled
|
|
|
|
//
|
|
|
|
// Call with lock held
|
2024-04-17 14:40:07 +00:00
|
|
|
func (bs *buckets) _isOff() bool { //nolint:unused // Don't include unused when running golangci-lint in case its on windows where this is not called
|
2020-07-04 16:20:54 +00:00
|
|
|
return bs[0] == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable the limits
|
|
|
|
//
|
|
|
|
// Call with lock held
|
|
|
|
func (bs *buckets) _setOff() {
|
|
|
|
for i := range bs {
|
|
|
|
bs[i] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 16:31:57 +00: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-21 17:54:21 +00:00
|
|
|
// Use defaultMaxBurstSize up to 2GBit/s (256MiB/s) then scale
|
2021-08-10 16:31:57 +00: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-01 22:16:19 +00:00
|
|
|
|
2020-12-07 16:19:20 +00: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-10 16:31:57 +00:00
|
|
|
tbs[TokenBucketSlotTransportTx] = newEmptyTokenBucket(bandwidth.Tx)
|
2020-12-07 16:19:20 +00:00
|
|
|
bandwidthAccounting = bandwidth.Tx
|
|
|
|
}
|
|
|
|
if bandwidth.Rx > 0 {
|
2021-08-10 16:31:57 +00:00
|
|
|
tbs[TokenBucketSlotTransportRx] = newEmptyTokenBucket(bandwidth.Rx)
|
2020-12-07 16:19:20 +00:00
|
|
|
if bandwidth.Rx > bandwidthAccounting {
|
|
|
|
bandwidthAccounting = bandwidth.Rx
|
|
|
|
}
|
|
|
|
}
|
2021-02-12 21:15:23 +00:00
|
|
|
// Limit core bandwidth to max of Rx and Tx if both are limited
|
|
|
|
if bandwidth.Tx > 0 && bandwidth.Rx > 0 {
|
2021-08-10 16:31:57 +00:00
|
|
|
tbs[TokenBucketSlotAccounting] = newEmptyTokenBucket(bandwidthAccounting)
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
2020-12-07 16:19:20 +00:00
|
|
|
return tbs
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartTokenBucket starts the token bucket if necessary
|
2020-07-04 16:20:54 +00:00
|
|
|
func (tb *tokenBucket) StartTokenBucket(ctx context.Context) {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
2020-11-05 11:33:32 +00:00
|
|
|
ci := fs.GetConfig(ctx)
|
2020-07-04 16:20:54 +00:00
|
|
|
tb.currLimit = ci.BwLimit.LimitAt(time.Now())
|
2020-12-07 16:19:20 +00:00
|
|
|
if tb.currLimit.Bandwidth.IsSet() {
|
2020-07-04 16:20:54 +00:00
|
|
|
tb.curr = newTokenBucket(tb.currLimit.Bandwidth)
|
2021-08-21 17:54:21 +00:00
|
|
|
fs.Infof(nil, "Starting bandwidth limiter at %v Byte/s", &tb.currLimit.Bandwidth)
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
2023-06-22 16:59:24 +00:00
|
|
|
|
|
|
|
// Start the SIGUSR2 signal handler to toggle bandwidth.
|
|
|
|
// This function does nothing in windows systems.
|
|
|
|
tb.startSignalHandler()
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartTokenTicker creates a ticker to update the bandwidth limiter every minute.
|
2020-07-04 16:20:54 +00:00
|
|
|
func (tb *tokenBucket) StartTokenTicker(ctx context.Context) {
|
2020-11-05 11:33:32 +00:00
|
|
|
ci := fs.GetConfig(ctx)
|
2018-02-01 13:13:24 +00: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 11:33:32 +00:00
|
|
|
if len(ci.BwLimit) <= 1 {
|
2018-02-01 13:13:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Minute)
|
|
|
|
go func() {
|
|
|
|
for range ticker.C {
|
2020-11-05 11:33:32 +00:00
|
|
|
limitNow := ci.BwLimit.LimitAt(time.Now())
|
2023-06-22 16:59:24 +00:00
|
|
|
tb.mu.Lock()
|
2018-02-01 13:13:24 +00:00
|
|
|
|
2020-07-04 16:20:54 +00:00
|
|
|
if tb.currLimit.Bandwidth != limitNow.Bandwidth {
|
2018-02-01 13:13:24 +00:00
|
|
|
// If bwlimit is toggled off, the change should only
|
|
|
|
// become active on the next toggle, which causes
|
2020-07-04 16:20:54 +00:00
|
|
|
// an exchange of tb.curr <-> tb.prev
|
|
|
|
var targetBucket *buckets
|
|
|
|
if tb.toggledOff {
|
|
|
|
targetBucket = &tb.prev
|
2018-02-01 13:13:24 +00:00
|
|
|
} else {
|
2020-07-04 16:20:54 +00:00
|
|
|
targetBucket = &tb.curr
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set new bandwidth. If unlimited, set tokenbucket to nil.
|
2020-12-07 16:19:20 +00:00
|
|
|
if limitNow.Bandwidth.IsSet() {
|
2018-02-01 13:13:24 +00:00
|
|
|
*targetBucket = newTokenBucket(limitNow.Bandwidth)
|
2020-07-04 16:20:54 +00:00
|
|
|
if tb.toggledOff {
|
2018-02-01 13:13:24 +00:00
|
|
|
fs.Logf(nil, "Scheduled bandwidth change. "+
|
2021-04-07 10:23:42 +00:00
|
|
|
"Limit will be set to %v Byte/s when toggled on again.", &limitNow.Bandwidth)
|
2018-02-01 13:13:24 +00:00
|
|
|
} else {
|
2021-04-07 10:23:42 +00:00
|
|
|
fs.Logf(nil, "Scheduled bandwidth change. Limit set to %v Byte/s", &limitNow.Bandwidth)
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-04 16:20:54 +00:00
|
|
|
targetBucket._setOff()
|
2018-02-01 13:13:24 +00:00
|
|
|
fs.Logf(nil, "Scheduled bandwidth change. Bandwidth limits disabled")
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:20:54 +00:00
|
|
|
tb.currLimit = limitNow
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
2023-06-22 16:59:24 +00:00
|
|
|
|
|
|
|
tb.mu.Unlock()
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:16:19 +00:00
|
|
|
// LimitBandwidth sleeps for the correct amount of time for the passage
|
|
|
|
// of n bytes according to the current bandwidth limit
|
2020-07-04 16:20:54 +00:00
|
|
|
func (tb *tokenBucket) LimitBandwidth(i TokenBucketSlot, n int) {
|
|
|
|
tb.mu.RLock()
|
2018-02-01 13:13:24 +00:00
|
|
|
|
|
|
|
// Limit the transfer speed if required
|
2021-02-01 22:16:19 +00: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 13:13:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:16:19 +00:00
|
|
|
tb.mu.RUnlock()
|
2018-02-01 13:13:24 +00:00
|
|
|
}
|
2018-03-16 21:45:09 +00:00
|
|
|
|
2018-04-24 08:43:07 +00:00
|
|
|
// SetBwLimit sets the current bandwidth limit
|
2020-12-07 16:19:20 +00:00
|
|
|
func (tb *tokenBucket) SetBwLimit(bandwidth fs.BwPair) {
|
2020-07-04 16:20:54 +00:00
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
2020-12-07 16:19:20 +00:00
|
|
|
if bandwidth.IsSet() {
|
2020-07-04 16:20:54 +00:00
|
|
|
tb.curr = newTokenBucket(bandwidth)
|
2018-04-24 08:43:07 +00:00
|
|
|
fs.Logf(nil, "Bandwidth limit set to %v", bandwidth)
|
|
|
|
} else {
|
2020-07-04 16:20:54 +00:00
|
|
|
tb.curr._setOff()
|
2018-04-24 08:43:07 +00:00
|
|
|
fs.Logf(nil, "Bandwidth limit reset to unlimited")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:20:54 +00: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 10:12:57 +00:00
|
|
|
return out, fmt.Errorf("bad bwlimit: %w", err)
|
2020-07-04 16:20:54 +00: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-07 16:19:20 +00: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-04 16:20:54 +00:00
|
|
|
}
|
|
|
|
tb.mu.RUnlock()
|
|
|
|
out = rc.Params{
|
2020-12-07 16:19:20 +00:00
|
|
|
"rate": bp.String(),
|
|
|
|
"bytesPerSecond": bytesPerSecond,
|
|
|
|
"bytesPerSecondTx": int64(bp.Tx),
|
|
|
|
"bytesPerSecondRx": int64(bp.Rx),
|
2020-07-04 16:20:54 +00:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-03-16 21:45:09 +00:00
|
|
|
// Remote control for the token bucket
|
|
|
|
func init() {
|
|
|
|
rc.Add(rc.Call{
|
2024-05-31 14:11:45 +00:00
|
|
|
Path: "core/bwlimit",
|
|
|
|
Fn: TokenBucket.rcBwlimit,
|
2018-03-16 21:45:09 +00:00
|
|
|
Title: "Set the bandwidth limit.",
|
|
|
|
Help: `
|
2020-12-07 16:19:20 +00: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-16 21:45:09 +00:00
|
|
|
|
|
|
|
Eg
|
|
|
|
|
2018-04-23 19:44:44 +00:00
|
|
|
rclone rc core/bwlimit rate=off
|
2019-06-24 12:18:52 +00:00
|
|
|
{
|
|
|
|
"bytesPerSecond": -1,
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecondTx": -1,
|
|
|
|
"bytesPerSecondRx": -1,
|
2019-06-24 12:18:52 +00:00
|
|
|
"rate": "off"
|
|
|
|
}
|
|
|
|
rclone rc core/bwlimit rate=1M
|
|
|
|
{
|
|
|
|
"bytesPerSecond": 1048576,
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecondTx": 1048576,
|
|
|
|
"bytesPerSecondRx": 1048576,
|
|
|
|
"rate": "1M"
|
|
|
|
}
|
|
|
|
rclone rc core/bwlimit rate=1M:100k
|
|
|
|
{
|
|
|
|
"bytesPerSecond": 1048576,
|
|
|
|
"bytesPerSecondTx": 1048576,
|
|
|
|
"bytesPerSecondRx": 131072,
|
2019-06-24 12:18:52 +00:00
|
|
|
"rate": "1M"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-19 11:02:44 +00:00
|
|
|
If the rate parameter is not supplied then the bandwidth is queried
|
2019-06-24 12:18:52 +00:00
|
|
|
|
|
|
|
rclone rc core/bwlimit
|
|
|
|
{
|
|
|
|
"bytesPerSecond": 1048576,
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecondTx": 1048576,
|
|
|
|
"bytesPerSecondRx": 1048576,
|
2019-06-24 12:18:52 +00:00
|
|
|
"rate": "1M"
|
|
|
|
}
|
2018-03-16 21:45:09 +00:00
|
|
|
|
|
|
|
The format of the parameter is exactly the same as passed to --bwlimit
|
|
|
|
except only one bandwidth may be specified.
|
2019-06-24 12:18:52 +00:00
|
|
|
|
2021-11-04 11:50:43 +00:00
|
|
|
In either case "rate" is returned as a human-readable string, and
|
2019-06-24 12:18:52 +00:00
|
|
|
"bytesPerSecond" is returned as a number.
|
2018-03-16 21:45:09 +00:00
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|