forked from ehazlett/stellar
/
proxy.go
77 lines (67 loc) · 1.48 KB
/
proxy.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
package main
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/codegangsta/cli"
ptypes "github.com/gogo/protobuf/types"
)
var proxyCommand = cli.Command{
Name: "proxy",
Usage: "manage the cluster proxy",
Subcommands: []cli.Command{
proxyListBackendsCommand,
proxyReloadCommand,
},
}
var proxyReloadCommand = cli.Command{
Name: "reload",
Usage: "reload proxy service",
Action: func(c *cli.Context) error {
client, err := getClient(c)
if err != nil {
return err
}
defer client.Close()
if err := client.Proxy().Reload(); err != nil {
return err
}
return nil
},
}
var proxyListBackendsCommand = cli.Command{
Name: "list",
Usage: "list backends",
Action: func(c *cli.Context) error {
client, err := getClient(c)
if err != nil {
return err
}
defer client.Close()
backends, err := client.Proxy().Backends()
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
fmt.Fprintf(w, "HOST\tUPSTREAMS\n")
for _, b := range backends {
upstreams := []string{}
for _, up := range b.Upstreams {
latency, err := ptypes.DurationFromProto(up.Latency)
if err != nil {
return err
}
status := up.Status
if latency > time.Millisecond*0 {
status = fmt.Sprintf("%s: %s", up.Status, latency)
}
upstreams = append(upstreams, fmt.Sprintf("%s (%s)", up.Address, status))
}
fmt.Fprintf(w, "%s\t%s\n", b.Host, strings.Join(upstreams, ", "))
}
w.Flush()
return nil
},
}