/
ssdbproxy.go
127 lines (115 loc) · 2.48 KB
/
ssdbproxy.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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"runtime"
_ "runtime/debug"
"runtime/pprof"
_ "sync"
"time"
_ "github.com/matishsiao/gossdb/ssdb"
_ "github.com/shirou/gopsutil/process"
)
var (
version string = "0.0.18"
configPath string = "configs.json"
CONFIGS Configs
modTime time.Time
GlobalClient ServerClient
memprofile string = "mempprof.log"
memFile *os.File
full bool
calcCPU bool
useCPU int
)
func main() {
log.Println("Version:", version)
flag.StringVar(&configPath, "c", "configs.json", "config file path")
flag.BoolVar(&full, "f", false, "using full cpu")
//flag.StringVar(&memprofile,"mm", "", "write memory profile to this file")
flag.Parse()
config, err := loadConfigs(configPath)
if err != nil {
log.Println("Load config file error:", err)
os.Exit(1)
}
CONFIGS = config
SetUlimit(1002000)
useCPU = runtime.NumCPU()
if !full {
useCPU -= 1
if useCPU <= 0 {
useCPU = 1
}
}
runtime.GOMAXPROCS(useCPU)
go getCPU()
GlobalClient.Init()
go Listen(CONFIGS.Host, CONFIGS.Port)
go WebServer()
go checkConfig()
for {
PrintGCSummary()
time.Sleep(60 * time.Second)
}
}
func getCPU() {
/*stats, _ := process.NewProcess(int32(os.Getpid()))
for {
cpu, _ := stats.Percent(1 * time.Second)
if int(cpu) > useCPU*80 {
log.Printf("CPU using too high:%v\n", cpu)
go WatchCPU()
}
time.Sleep(1 * time.Second)
}*/
}
func checkConfig() {
for {
configWatcher()
time.Sleep(250 * time.Millisecond)
}
}
func writeMemProfile() {
pprof.WriteHeapProfile(memFile)
}
func configWatcher() {
file, err := os.Open(configPath) // For read access.
if err != nil {
log.Println("configWatcher error:", err)
}
info, err := file.Stat()
if err != nil {
log.Println("configWatcher error:", err)
}
if modTime.Unix() == -62135596800 {
log.Println("configWatcher init mod time")
modTime = info.ModTime()
}
if info.ModTime() != modTime {
log.Printf("Config file changed. Reolad config file.\n")
modTime = info.ModTime()
CONFIGS, err = loadConfigs(configPath)
if err != nil {
log.Printf("configWatcher error:%v\n", err)
}
}
defer file.Close()
}
func loadConfigs(fileName string) (Configs, error) {
file, e := ioutil.ReadFile(fileName)
if e != nil {
log.Printf("Load config file error: %v\n", e)
os.Exit(1)
}
var config Configs
err := json.Unmarshal(file, &config)
if err != nil {
log.Printf("Config load error:%v \n", err)
return config, err
}
return config, nil
}