-
Notifications
You must be signed in to change notification settings - Fork 0
/
hahaproxy.go
186 lines (152 loc) · 3.34 KB
/
hahaproxy.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"fmt"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/pprof"
"strconv"
"syscall"
"time"
"github.com/astaxie/beego/logs"
"github.com/howeyc/fsnotify"
)
var log *logs.BeeLogger
var conf *ConfigType
var g_timeout int = 4
var heartbeat int
var conffile = "proxy.conf"
// set logfile, loglevel
func SetLog() error {
logjsonconf := `{
"filename":"log.log",
"daily":true,
"maxDays":15,
"rotate":true,
"perm":600
}`
// "maxsize":1e10,
var logconfmap map[string]interface{}
if err := json.Unmarshal([]byte(logjsonconf), &logconfmap); err != nil {
return err
}
log = logs.NewLogger(10000)
logfilename := conf.Get("global", "log")
if len(logfilename) != 0 {
logconfmap["filename"] = logfilename
}
fmt.Printf("log file is [%s]\n", logfilename)
jsonconf, err := json.Marshal(logconfmap)
if err != nil {
fmt.Println("HHH", err)
return err
}
log.SetLogger("file", string(jsonconf))
loglevelstr := conf.Get("global", "loglevel")
if len(loglevelstr) == 0 {
loglevelstr = "8" // Debug
}
if loglevelstr > "3" {
log.EnableFuncCallDepth(true)
}
loglevel, err := strconv.Atoi(loglevelstr)
if err != nil {
return fmt.Errorf("logLevel is invalid:", loglevelstr)
}
log.SetLevel(loglevel)
log.Emergency("hahaproxy start!")
return nil
}
func monitorConf(file string, wa *fsnotify.Watcher) {
for {
err := wa.Watch(file)
if err != nil {
time.Sleep(2 * time.Second)
continue
}
log.Error("Set Watch [%s] ok!", file)
break
}
}
func main() {
chhh := make(chan os.Signal)
signal.Notify(chhh, syscall.SIGINT)
f, _ := os.Create("profile.log")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
// get global set
var err error
conf, err = NewConfig(conffile)
if err != nil {
fmt.Println(err)
return
}
// teststr := conf.Get("global", "log")
// fmt.Printf("teststr [%s], len(teststr) [%d]\n", teststr, len(teststr))
fmt.Println("Load Config Over!")
// set log
if err := SetLog(); err != nil {
fmt.Println(err)
return
}
fmt.Println("Log is Okay!")
// all server store in map
sermap := make(map[string]*Worker, 32)
// register inotify
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Error("fsnotify.NewWatcher Error! [%s]", err)
return
}
go monitorConf(conffile, watcher)
// if some work cannot bind, app exit!!
bindok := make(chan byte)
MAINLOGIC:
// main logic
allsess := conf.AllKey()
for _, sess := range allsess {
if sess == "global" {
continue
}
oneServer, ok := sermap[sess]
if !ok {
log.Info("start new Server [%s]", sess)
oneServer := NewWorker()
oneServer.Name = sess
oneServer.Triger = make(chan string, 1)
sermap[sess] = oneServer
go oneServer.Start(bindok) // start
} else {
log.Info("reload Server [%s]", sess)
oneServer.Triger <- "reload"
}
}
// main loop
select {
case <-bindok:
return
case <-time.After(2 * time.Second):
}
for {
select {
case ev := <-watcher.Event:
if ev.IsModify() || ev.IsCreate() {
fmt.Println("Modify Event")
conf, err = NewConfig(conffile)
if err != nil {
log.Error("Reload Error! [%s]", err)
continue
}
goto MAINLOGIC
} else if ev.IsDelete() {
fmt.Println("Delete Event")
go monitorConf(conffile, watcher)
}
case <-time.After(10 * time.Second):
// log.Info("Main coroutine loop")
case <-chhh:
return
}
}
}