-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlimits.go
More file actions
73 lines (68 loc) · 2.2 KB
/
Copy pathlimits.go
File metadata and controls
73 lines (68 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package rtapi
import (
"context"
"fmt"
"math"
)
// GlobalLimits returns rTorrent's global download and upload rate limits, in
// bytes per second. Zero means unlimited.
func (r *Rtorrent) GlobalLimits() (down, up uint64, err error) {
return r.GlobalLimitsContext(context.Background())
}
// GlobalLimitsContext is GlobalLimits with a context.
func (r *Rtorrent) GlobalLimitsContext(ctx context.Context) (down, up uint64, err error) {
req, err := marshalMethodCall(xmlrpcMethodCall{
MethodName: "system.multicall",
Params: []xmlrpcParam{{Value: newArrayValue(
newMethodCall("throttle.global_down.max_rate", ""),
newMethodCall("throttle.global_up.max_rate", ""),
)}},
})
if err != nil {
return 0, 0, err
}
resp, err := r.executeMulticall(ctx, req, 2)
if err != nil {
return 0, 0, fmt.Errorf("rtapi: get global limits: %w", err)
}
values, err := resp.arrayParam()
if err != nil {
return 0, 0, err
}
limits := make([]uint64, 2)
for i, value := range values[:2] {
result, err := value.firstArrayValue()
if err == nil {
limits[i], err = result.uint64Value()
}
if err != nil {
return 0, 0, fmt.Errorf("rtapi: parse global limit: %w", err)
}
}
return limits[0], limits[1], nil
}
// SetGlobalLimits sets rTorrent's global download and upload rate limits, in
// bytes per second. Zero removes a limit.
func (r *Rtorrent) SetGlobalLimits(down, up uint64) error {
return r.SetGlobalLimitsContext(context.Background(), down, up)
}
// SetGlobalLimitsContext is SetGlobalLimits with a context.
func (r *Rtorrent) SetGlobalLimitsContext(ctx context.Context, down, up uint64) error {
if down > math.MaxInt64 || up > math.MaxInt64 {
return fmt.Errorf("rtapi: rate limit is too large")
}
req, err := marshalMethodCall(xmlrpcMethodCall{
MethodName: "system.multicall",
Params: []xmlrpcParam{{Value: newArrayValue(
newMethodCallValues("throttle.global_down.max_rate.set", newStringValue(""), newIntValue(int64(down))),
newMethodCallValues("throttle.global_up.max_rate.set", newStringValue(""), newIntValue(int64(up))),
)}},
})
if err != nil {
return err
}
if _, err := r.executeMulticall(ctx, req, 2); err != nil {
return fmt.Errorf("rtapi: set global limits: %w", err)
}
return nil
}