cmd: add set/get limit (net throttling)

example:

	monero daemon set-limit --up 10
	Status:         OK
	Limit Up:       10 kB/s
	Limit Down:     1.0 GB/s

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
Ciro S. Costa 2021-09-18 11:53:02 +00:00
parent 0d94df56f6
commit 17445423a6
4 changed files with 195 additions and 0 deletions

View file

@ -0,0 +1,63 @@
package daemon
import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/cmd/monero/options"
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
)
type getLimitCommand struct {
JSON bool
}
func (c *getLimitCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-limit",
Short: "retrieve bandwidth throttling information",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
return cmd
}
func (c *getLimitCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOpts.Context()
defer cancel()
client, err := options.RootOpts.Client()
if err != nil {
return fmt.Errorf("client: %w", err)
}
resp, err := client.GetLimit(ctx)
if err != nil {
return fmt.Errorf("get limit: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
// nolint:forbidigo
func (c *getLimitCommand) pretty(v *daemon.GetLimitResult) {
table := display.NewTable()
table.AddRow("Status:", v.Status)
table.AddRow("Limit Up:", humanize.Bytes(v.LimitUp*1024)+"/s")
table.AddRow("Limit Down:", humanize.Bytes(v.LimitDown*1024)+"/s")
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&getLimitCommand{}).Cmd())
}

View file

@ -0,0 +1,75 @@
package daemon
import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/cmd/monero/options"
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
)
type setLimitCommand struct {
LimitUp uint64
LimitDown uint64
JSON bool
}
func (c *setLimitCommand) Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set-limit",
Short: "configure bandwidth throttling",
RunE: c.RunE,
}
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
cmd.Flags().Uint64Var(&c.LimitUp, "up",
0, "max upload bandwidth (in kB/s)")
cmd.Flags().Uint64Var(&c.LimitDown, "down",
0, "max download bandwidth (in kB/s)")
return cmd
}
func (c *setLimitCommand) RunE(_ *cobra.Command, _ []string) error {
ctx, cancel := options.RootOpts.Context()
defer cancel()
client, err := options.RootOpts.Client()
if err != nil {
return fmt.Errorf("client: %w", err)
}
params := daemon.SetLimitRequestParameters{
LimitUp: c.LimitUp,
LimitDown: c.LimitDown,
}
resp, err := client.SetLimit(ctx, params)
if err != nil {
return fmt.Errorf("set limit: %w", err)
}
if c.JSON {
return display.JSON(resp)
}
c.pretty(resp)
return nil
}
// nolint:forbidigo
func (c *setLimitCommand) pretty(v *daemon.SetLimitResult) {
table := display.NewTable()
table.AddRow("Status:", v.Status)
table.AddRow("Limit Up:", humanize.Bytes(v.LimitUp*1024)+"/s")
table.AddRow("Limit Down:", humanize.Bytes(v.LimitDown*1024)+"/s")
fmt.Println(table)
}
func init() {
RootCommand.AddCommand((&setLimitCommand{}).Cmd())
}

View file

@ -8,6 +8,7 @@ import (
const (
endpointGetHeight = "/get_height"
endpointGetLimit = "/get_limit"
endpointGetNetStats = "/get_net_stats"
endpointGetOuts = "/get_outs"
endpointGetPeerList = "/get_peer_list"
@ -16,6 +17,7 @@ const (
endpointGetTransactionPoolStats = "/get_transaction_pool_stats"
endpointGetTransactions = "/get_transactions"
endpointMiningStatus = "/mining_status"
endpointSetLimit = "/set_limit"
endpointStartMining = "/start_mining"
endpointStopMining = "/stop_mining"
)
@ -33,6 +35,30 @@ func (c *Client) StopMining(
return resp, nil
}
func (c *Client) GetLimit(ctx context.Context) (*GetLimitResult, error) {
resp := &GetLimitResult{}
err := c.RawRequest(ctx, endpointGetLimit, nil, resp)
if err != nil {
return nil, fmt.Errorf("raw request: %w", err)
}
return resp, nil
}
func (c *Client) SetLimit(
ctx context.Context, params SetLimitRequestParameters,
) (*SetLimitResult, error) {
resp := &SetLimitResult{}
err := c.RawRequest(ctx, endpointSetLimit, params, resp)
if err != nil {
return nil, fmt.Errorf("raw request: %w", err)
}
return resp, nil
}
func (c *Client) StartMining(
ctx context.Context, params StartMiningRequestParameters,
) (*StartMiningResult, error) {

View file

@ -852,6 +852,37 @@ type GetTransactionPoolResult struct {
Untrusted bool `json:"untrusted"`
}
type SetLimitRequestParameters struct {
// LimitUp is the upload limit in kB/s
//
LimitUp uint64 `json:"limit_up"`
// LimitDown is the download limit in kB/s
//
LimitDown uint64 `json:"limit_down"`
}
type SetLimitResult struct {
// LimitUp is the upload limit in kB/s
//
LimitUp uint64 `json:"limit_up"`
// LimitDOwn is the download limit in kB/s
//
LimitDown uint64 `json:"limit_down"`
RPCResultFooter `json:",inline"`
}
type GetLimitResult struct {
// LimitUp is the upload limit in kB/s
//
LimitUp uint64 `json:"limit_up"`
// LimitDown is the download limit in kB/s
//
LimitDown uint64 `json:"limit_down"`
RPCResultFooter `json:",inline"`
}
type StartMiningRequestParameters struct {
MinerAddress string `json:"miner_address"`
BackgroundMining bool `json:"background_mining"`