rc: return current settings if core/bwlimit called without parameters

This commit is contained in:
Nick Craig-Wood 2019-06-24 13:18:52 +01:00
parent 0ae844d1f8
commit 276f8cccf6
2 changed files with 102 additions and 17 deletions

View File

@ -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.
`,
})
}

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