forked from TrueCloudLab/rclone
rc: return current settings if core/bwlimit called without parameters
This commit is contained in:
parent
0ae844d1f8
commit
276f8cccf6
2 changed files with 102 additions and 17 deletions
|
@ -133,25 +133,31 @@ func init() {
|
||||||
rc.Add(rc.Call{
|
rc.Add(rc.Call{
|
||||||
Path: "core/bwlimit",
|
Path: "core/bwlimit",
|
||||||
Fn: func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
Fn: func(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||||
ibwlimit, ok := in["rate"]
|
if in["rate"] != nil {
|
||||||
if !ok {
|
bwlimit, err := in.GetString("rate")
|
||||||
return out, errors.Errorf("parameter rate not found")
|
if err != nil {
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
|
var bws fs.BwTimetable
|
||||||
|
err = bws.Set(bwlimit)
|
||||||
|
if err != nil {
|
||||||
|
return out, errors.Wrap(err, "bad bwlimit")
|
||||||
|
}
|
||||||
|
if len(bws) != 1 {
|
||||||
|
return out, errors.New("need exactly 1 bandwidth setting")
|
||||||
|
}
|
||||||
|
bw := bws[0]
|
||||||
|
SetBwLimit(bw.Bandwidth)
|
||||||
}
|
}
|
||||||
bwlimit, ok := ibwlimit.(string)
|
bytesPerSecond := int64(-1)
|
||||||
if !ok {
|
if tokenBucket != nil {
|
||||||
return out, errors.Errorf("value must be string rate=%v", ibwlimit)
|
bytesPerSecond = int64(tokenBucket.Limit())
|
||||||
}
|
}
|
||||||
var bws fs.BwTimetable
|
out = rc.Params{
|
||||||
err = bws.Set(bwlimit)
|
"rate": fs.SizeSuffix(bytesPerSecond).String(),
|
||||||
if err != nil {
|
"bytesPerSecond": bytesPerSecond,
|
||||||
return out, errors.Wrap(err, "bad bwlimit")
|
|
||||||
}
|
}
|
||||||
if len(bws) != 1 {
|
return out, nil
|
||||||
return out, errors.New("need exactly 1 bandwidth setting")
|
|
||||||
}
|
|
||||||
bw := bws[0]
|
|
||||||
SetBwLimit(bw.Bandwidth)
|
|
||||||
return rc.Params{"rate": bw.Bandwidth.String()}, nil
|
|
||||||
},
|
},
|
||||||
Title: "Set the bandwidth limit.",
|
Title: "Set the bandwidth limit.",
|
||||||
Help: `
|
Help: `
|
||||||
|
@ -159,11 +165,31 @@ This sets the bandwidth limit to that passed in.
|
||||||
|
|
||||||
Eg
|
Eg
|
||||||
|
|
||||||
rclone rc core/bwlimit rate=1M
|
|
||||||
rclone rc core/bwlimit rate=off
|
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
|
The format of the parameter is exactly the same as passed to --bwlimit
|
||||||
except only one bandwidth may be specified.
|
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 a new issue