-
Notifications
You must be signed in to change notification settings - Fork 193
/
rate.go
84 lines (78 loc) · 2.27 KB
/
rate.go
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
74
75
76
77
78
79
80
81
82
83
84
//nolint:dupl
package cmd
import (
"context"
"fmt"
"github.com/alexei-led/pumba/pkg/chaos"
"github.com/alexei-led/pumba/pkg/chaos/netem"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
type rateContext struct {
context context.Context
}
// NewRateCLICommand initialize CLI rate command and bind it to the lossContext
func NewRateCLICommand(ctx context.Context) *cli.Command {
cmdContext := &rateContext{context: ctx}
return &cli.Command{
Name: "rate",
Flags: []cli.Flag{
cli.StringFlag{
Name: "rate, r",
Usage: "delay outgoing packets; in common units",
Value: "100kbit",
},
cli.IntFlag{
Name: "packetoverhead, p",
Usage: "per packet overhead; in bytes",
Value: 0,
},
cli.IntFlag{
Name: "cellsize, s",
Usage: "cell size of the simulated link layer scheme",
Value: 0,
},
cli.IntFlag{
Name: "celloverhead, c",
Usage: "per cell overhead; in bytes",
Value: 0,
},
},
Usage: "rate limit egress traffic",
ArgsUsage: fmt.Sprintf("containers (name, list of names, or RE2 regex if prefixed with %q", chaos.Re2Prefix),
Description: "rate limit egress traffic for specified containers",
Action: cmdContext.rate,
}
}
// NETEM RATE Command - network emulation rate
func (cmd *rateContext) rate(c *cli.Context) error {
// parse common chaos flags
globalParams, err := chaos.ParseGlobalParams(c)
if err != nil {
return errors.Wrap(err, "error parsing global parameters")
}
// parse netem flags
netemParams, err := parseNetemParams(c.Parent(), globalParams.Interval)
if err != nil {
return errors.Wrap(err, "error parsing netem parameters")
}
// get target egress rate
rate := c.String("rate")
// get packet overhead
packetOverhead := c.Int("packetoverhead")
// get cell size
cellSize := c.Int("cellsize")
// get cell overhead
cellOverhead := c.Int("celloverhead")
// init netem rate command
lossCommand, err := netem.NewRateCommand(chaos.DockerClient, globalParams, netemParams, rate, packetOverhead, cellSize, cellOverhead)
if err != nil {
return errors.Wrap(err, "error creating netem rate command")
}
// run netem command
err = chaos.RunChaosCommand(cmd.context, lossCommand, globalParams)
if err != nil {
return errors.Wrap(err, "error running netem rate command")
}
return nil
}