-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (109 loc) · 2.56 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"sort"
"time"
"github.com/jmuia/tcp-proxy/health"
"github.com/jmuia/tcp-proxy/loadbalancer"
logger "github.com/jmuia/tcp-proxy/logging"
"github.com/jmuia/tcp-proxy/proxy"
)
func main() {
cfg := cli()
tcpProxy, err := proxy.NewTCPProxy(*cfg)
if err != nil {
logger.Error(err)
os.Exit(1)
}
handleExitSignal(tcpProxy)
handleStatsSignal(tcpProxy)
err = tcpProxy.Run()
if err != nil {
logger.Error(err)
os.Exit(1)
}
}
func cli() *proxy.Config {
cfg := proxy.Config{}
flag.Usage = func() {
fmt.Println("Usage: ./tcp-proxy [OPTIONS] <BACKEND>...")
flag.PrintDefaults()
fmt.Println()
fmt.Println("Metrics: send SIGINFO (ctrl-t) or SIGUSR1")
fmt.Println()
fmt.Println("Example:")
fmt.Println(" ./tcp-proxy \\")
fmt.Println("\t-laddr localhost:4000 \\")
fmt.Println("\t-timeout 3s \\")
fmt.Println("\t-lb random \\")
fmt.Println("\tlocalhost:8001 \\")
fmt.Println("\tlocalhost:8002")
}
flag.StringVar(&cfg.Laddr, "laddr", ":4000", "address to listen on")
flag.DurationVar(&cfg.Timeout, "timeout", 3*time.Second, "backend dial timeout")
flag.Var(newLbTypeVar(&cfg.Lb.Type, loadbalancer.P2C_TYPE), "lb", "load balancer algorithm (RANDOM|P2C)")
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
cfg.Backends = flag.Args()
cfg.GracePeriod = 5 * time.Second
cfg.Health = health.HealthCheckConfig{
Timeout: 1 * time.Second,
Interval: 5 * time.Second,
UnhealthyThreshold: 3,
HealthyThreshold: 3,
}
return &cfg
}
type lbTypeValue loadbalancer.Type
func newLbTypeVar(p *loadbalancer.Type, value loadbalancer.Type) *lbTypeValue {
*p = value
return (*lbTypeValue)(p)
}
func (v *lbTypeValue) String() string {
return (*loadbalancer.Type)(v).String()
}
func (v *lbTypeValue) Set(s string) error {
t, err := loadbalancer.ParseType(s)
if err != nil {
return err
}
*v = lbTypeValue(t)
return nil
}
func sorted(m map[string]interface{}) []string {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func handleExitSignal(tcpProxy *proxy.TCPProxy) {
exitc := make(chan os.Signal)
signal.Notify(exitc, exitSignals...)
go func() {
for range exitc {
fmt.Println()
tcpProxy.Shutdown()
}
}()
}
func handleStatsSignal(tcpProxy *proxy.TCPProxy) {
statsc := make(chan os.Signal)
signal.Notify(statsc, statsSignals...)
go func() {
for range statsc {
fmt.Println()
stats := tcpProxy.Stats()
for _, k := range sorted(stats) {
fmt.Printf("%s: %v\n", k, stats[k])
}
}
}()
}