mirror of
https://github.com/rclone/rclone.git
synced 2024-11-29 11:55:01 +01:00
rc: return current settings if core/bwlimit called without parameters
This commit is contained in:
parent
0ae844d1f8
commit
276f8cccf6
@ -133,13 +133,10 @@ func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "core/bwlimit",
|
||||
Fn: func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
ibwlimit, ok := in["rate"]
|
||||
if !ok {
|
||||
return out, errors.Errorf("parameter rate not found")
|
||||
}
|
||||
bwlimit, ok := ibwlimit.(string)
|
||||
if !ok {
|
||||
return out, errors.Errorf("value must be string rate=%v", ibwlimit)
|
||||
if in["rate"] != nil {
|
||||
bwlimit, err := in.GetString("rate")
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
var bws fs.BwTimetable
|
||||
err = bws.Set(bwlimit)
|
||||
@ -151,7 +148,16 @@ func init() {
|
||||
}
|
||||
bw := bws[0]
|
||||
SetBwLimit(bw.Bandwidth)
|
||||
return rc.Params{"rate": bw.Bandwidth.String()}, nil
|
||||
}
|
||||
bytesPerSecond := int64(-1)
|
||||
if tokenBucket != nil {
|
||||
bytesPerSecond = int64(tokenBucket.Limit())
|
||||
}
|
||||
out = rc.Params{
|
||||
"rate": fs.SizeSuffix(bytesPerSecond).String(),
|
||||
"bytesPerSecond": bytesPerSecond,
|
||||
}
|
||||
return out, nil
|
||||
},
|
||||
Title: "Set the bandwidth limit.",
|
||||
Help: `
|
||||
@ -159,11 +165,31 @@ This sets the bandwidth limit to that passed in.
|
||||
|
||||
Eg
|
||||
|
||||
rclone rc core/bwlimit rate=1M
|
||||
rclone rc core/bwlimit rate=off
|
||||
{
|
||||
"bytesPerSecond": -1,
|
||||
"rate": "off"
|
||||
}
|
||||
rclone rc core/bwlimit rate=1M
|
||||
{
|
||||
"bytesPerSecond": 1048576,
|
||||
"rate": "1M"
|
||||
}
|
||||
|
||||
|
||||
If the rate parameter is not suppied then the bandwidth is queried
|
||||
|
||||
rclone rc core/bwlimit
|
||||
{
|
||||
"bytesPerSecond": 1048576,
|
||||
"rate": "1M"
|
||||
}
|
||||
|
||||
The format of the parameter is exactly the same as passed to --bwlimit
|
||||
except only one bandwidth may be specified.
|
||||
|
||||
In either case "rate" is returned as a human readable string, and
|
||||
"bytesPerSecond" is returned as a number.
|
||||
`,
|
||||
})
|
||||
}
|
||||
|
59
fs/accounting/token_bucket_test.go
Normal file
59
fs/accounting/token_bucket_test.go
Normal file
@ -0,0 +1,59 @@
|
||||
package accounting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/ncw/rclone/fs/rc"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
func TestRcBwLimit(t *testing.T) {
|
||||
call := rc.Calls.Get("core/bwlimit")
|
||||
assert.NotNil(t, call)
|
||||
|
||||
// Set
|
||||
in := rc.Params{
|
||||
"rate": "1M",
|
||||
}
|
||||
out, err := call.Fn(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rc.Params{
|
||||
"bytesPerSecond": int64(1048576),
|
||||
"rate": "1M",
|
||||
}, out)
|
||||
assert.Equal(t, rate.Limit(1048576), tokenBucket.Limit())
|
||||
|
||||
// Query
|
||||
in = rc.Params{}
|
||||
out, err = call.Fn(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rc.Params{
|
||||
"bytesPerSecond": int64(1048576),
|
||||
"rate": "1M",
|
||||
}, out)
|
||||
|
||||
// Reset
|
||||
in = rc.Params{
|
||||
"rate": "off",
|
||||
}
|
||||
out, err = call.Fn(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rc.Params{
|
||||
"bytesPerSecond": int64(-1),
|
||||
"rate": "off",
|
||||
}, out)
|
||||
assert.Nil(t, tokenBucket)
|
||||
|
||||
// Query
|
||||
in = rc.Params{}
|
||||
out, err = call.Fn(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rc.Params{
|
||||
"bytesPerSecond": int64(-1),
|
||||
"rate": "off",
|
||||
}, out)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user