-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
117 lines (97 loc) · 2.45 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
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/pyinx/zk-proxy/zk"
"golang.org/x/net/context"
)
var (
buildstamp string
githash string
goversion string
)
var (
backendAddrs = flag.String("backend_addr", "", "zk server address: 1.1.1.1:2181,2.2.2.2:2181")
httpAddr = flag.String("http_addr", "0.0.0.0:8000", "http address")
proxyAddr = flag.String("proxy_addr", "0.0.0.0:2182", "proxy address")
cpuNum = flag.Int("cpu_num", 1, "max cpu num")
ipAcl = flag.Bool("ip_acl", false, "enable ip acl for zk path")
limitNum = flag.Int("limit_num", -1, "limit num for request rate")
version = flag.Bool("version", false, "show proxy version")
)
func main() {
flag.Parse()
if *version {
showVersion()
return
}
if len(*backendAddrs) == 0 {
fmt.Println("help to get usage")
return
}
setCpuNum(*cpuNum)
ln, err := net.Listen("tcp", *proxyAddr)
if err != nil {
panic(err)
}
ctx, cancle := context.WithCancel(context.Background())
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
go stopProc(cancle, c)
go zk.StartHttp(*httpAddr)
if *ipAcl {
zk.InitAcl(zk.GetZkServers(*backendAddrs))
}
if *limitNum > 0 {
zk.SetLimit(*limitNum)
}
// go cpuProfile()
// go heapProfile()
serv := zk.Serve
serv(ctx, ln, zk.NewAuth(zk.GetZkServers(*backendAddrs)), zk.NewZK())
}
func setCpuNum(cpuNum int) {
if cpuNum > 4 {
runtime.GOMAXPROCS(4)
} else if cpuNum < 1 {
runtime.GOMAXPROCS(1)
} else {
runtime.GOMAXPROCS(cpuNum)
}
}
func stopProc(cancle context.CancelFunc, c chan os.Signal) {
s := <-c
fmt.Printf("receive signal %s, exiting...\n", s)
cancle()
time.Sleep(10 * time.Millisecond)
os.Exit(0)
}
func showVersion() {
fmt.Println("buildstamp: " + buildstamp)
fmt.Println("githash: " + githash)
fmt.Println("goversion: " + goversion)
}
func cpuProfile() {
f, _ := os.OpenFile("cpu.prof", os.O_RDWR|os.O_CREATE, 0644)
defer f.Close()
fmt.Println("CPU Profile started")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
time.Sleep(120 * time.Second)
fmt.Println("CPU Profile stopped")
}
func heapProfile() {
f, _ := os.OpenFile("heap.prof", os.O_RDWR|os.O_CREATE, 0644)
defer f.Close()
fmt.Println("Heap Profile started")
time.Sleep(120 * time.Second)
pprof.WriteHeapProfile(f)
fmt.Println("Heap Profile generated")
}