Skip to content

Commit

Permalink
Split cli toxi subcommands to seperate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
miry committed Sep 16, 2021
1 parent 77671d1 commit 80756d8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linters:

linters-settings:
funlen:
lines: 90
lines: 80
statements: 30

lll:
Expand Down
165 changes: 88 additions & 77 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,85 +154,96 @@ func cliCommands() []*cli.Command {

func cliToxiSubCommands() []*cli.Command {
return []*cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add a new toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "type of toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
&cli.BoolFlag{
Name: "upstream",
Aliases: []string{"u"},
Usage: "add toxic to upstream",
DefaultText: "false",
},
&cli.BoolFlag{
Name: "downstream",
Aliases: []string{"d"},
Usage: "add toxic to downstream",
DefaultText: "true",
},
cliToxiAddSubCommand(),
cliToxiUpdateSubCommand(),
cliToxiRemoveSubCommand(),
}
}

func cliToxiAddSubCommand() *cli.Command {
return &cli.Command{
Name: "add",
Aliases: []string{"a"},
Usage: "add a new toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "type of toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
&cli.BoolFlag{
Name: "upstream",
Aliases: []string{"u"},
Usage: "add toxic to upstream",
DefaultText: "false",
},
&cli.BoolFlag{
Name: "downstream",
Aliases: []string{"d"},
Usage: "add toxic to downstream",
DefaultText: "true",
},
Action: withToxi(addToxic),
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "update an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
Action: withToxi(addToxic),
}
}
func cliToxiUpdateSubCommand() *cli.Command {
return &cli.Command{
Name: "update",
Aliases: []string{"u"},
Usage: "update an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
&cli.StringFlag{
Name: "toxicity",
Aliases: []string{"tox"},
Usage: "toxicity of toxic",
},
&cli.StringSliceFlag{
Name: "attribute",
Aliases: []string{"a"},
Usage: "toxic attribute in key=value format",
},
Action: withToxi(updateToxic),
},
{
Name: "remove",
Aliases: []string{"r", "delete", "d"},
Usage: "remove an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
Action: withToxi(updateToxic),
}
}

func cliToxiRemoveSubCommand() *cli.Command {
return &cli.Command{
Name: "remove",
Aliases: []string{"r", "delete", "d"},
Usage: "remove an enabled toxic",
ArgsUsage: "<proxyName>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "toxicName",
Aliases: []string{"n"},
Usage: "name of the toxic",
},
Action: withToxi(removeToxic),
},
Action: withToxi(removeToxic),
}
}

Expand Down Expand Up @@ -430,21 +441,21 @@ func parseToxicity(c *cli.Context, defaultToxicity float32) (float32, error) {
return toxicity, nil
}

type addToxicOptions struct {
type ToxicOptions struct {
proxyName, toxicName, toxicType string
toxicity float32
attributes toxiproxy.Attributes
upstream, downstream bool
}

func (o *addToxicOptions) stream() string {
func (o *ToxicOptions) stream() string {
if o.upstream {
return "upstream"
}
return "downstream"
}

func parseAddToxicParams(c *cli.Context) (*addToxicOptions, error) {
func parseAddToxicParams(c *cli.Context) (*ToxicOptions, error) {
upstream := c.Bool("upstream")
downstream := c.Bool("downstream")
if upstream && downstream {
Expand All @@ -470,7 +481,7 @@ func parseAddToxicParams(c *cli.Context) (*addToxicOptions, error) {

attributes := parseAttributes(c, "attribute")

return &addToxicOptions{
return &ToxicOptions{
proxyName: proxyName,
toxicName: toxicName,
toxicType: toxicType,
Expand Down
51 changes: 29 additions & 22 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ func (p *Proxy) listen() (net.Listener, error) {
return p.listener, nil
}

func (p *Proxy) close() {
// Unblock ln.Accept()
err := p.listener.Close()
if err != nil {
logrus.WithFields(logrus.Fields{
"proxy": p.Name,
"listen": p.Listen,
"err": err,
}).Warn("Attempted to close an already closed proxy server")
}
}

// This channel is to kill the blocking Accept() call below by closing the
// net.Listener.
func (p *Proxy) freeBlocker(acceptTomb *tomb.Tomb) {
<-p.tomb.Dying()

// Notify ln.Accept() that the shutdown was safe
acceptTomb.Killf("Shutting down from stop()")

p.close()

// Wait for the accept loop to finish processing
acceptTomb.Wait()
p.tomb.Done()
}

// server runs the Proxy server, accepting new clients and creating Links to
// connect them to upstreams.
func (proxy *Proxy) server() {
Expand All @@ -116,30 +143,10 @@ func (proxy *Proxy) server() {
return
}

acceptTomb := tomb.Tomb{}
acceptTomb := &tomb.Tomb{}
defer acceptTomb.Done()

// This channel is to kill the blocking Accept() call below by closing the
// net.Listener.
go func() {
<-proxy.tomb.Dying()

// Notify ln.Accept() that the shutdown was safe
acceptTomb.Killf("Shutting down from stop()")
// Unblock ln.Accept()
err := ln.Close()
if err != nil {
logrus.WithFields(logrus.Fields{
"proxy": proxy.Name,
"listen": proxy.Listen,
"err": err,
}).Warn("Attempted to close an already closed proxy server")
}

// Wait for the accept loop to finish processing
acceptTomb.Wait()
proxy.tomb.Done()
}()
go proxy.freeBlocker(acceptTomb)

for {
client, err := ln.Accept()
Expand Down

0 comments on commit 80756d8

Please sign in to comment.