-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmysql-smart-proxy.go
149 lines (130 loc) · 3.81 KB
/
mysql-smart-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
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
// Copyright 2016 The MSP Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path"
"runtime"
"strings"
"syscall"
"github.com/compasses/mysql-smart-proxy/config"
"github.com/compasses/mysql-smart-proxy/core/golog"
"github.com/compasses/mysql-smart-proxy/core/hack"
"github.com/compasses/mysql-smart-proxy/server"
)
var configFile *string = flag.String("config", "msp.yaml", "MSP config file")
var logLevel *string = flag.String("log-level", "", "log level [debug|info|warn|error], default error")
var version *bool = flag.Bool("v", false, "the version of MSP")
const (
sqlLogName = "slow_query.log"
sysLogName = "proxy.log"
runLogName = "proxy_run.log"
MaxLogSize = 1024 * 1024 * 10
)
const banner string = `
MySQL Smart Proxy
`
func main() {
fmt.Print(banner)
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
fmt.Printf("Git commit:%s\n", hack.Version)
fmt.Printf("Build time:%s\n", hack.Compile)
if *version {
return
}
if len(*configFile) == 0 {
fmt.Println("must use a config file")
return
}
cfg, err := config.ParseConfigFile(*configFile)
if err != nil {
fmt.Printf("parse config file error:%v\n", err.Error())
return
}
//when the log file size greater than 1GB, MSP will generate a new file
if len(cfg.LogPath) != 0 {
sysFilePath := path.Join(cfg.LogPath, sysLogName)
sysFile, err := golog.NewRotatingFileHandler(sysFilePath, MaxLogSize, 1)
if err != nil {
fmt.Printf("new log file error:%v\n", err.Error())
return
}
golog.GlobalSysLogger = golog.New(sysFile, golog.Lfile|golog.Ltime|golog.Llevel)
sqlFilePath := path.Join(cfg.LogPath, sqlLogName)
sqlFile, err := golog.NewRotatingFileHandler(sqlFilePath, MaxLogSize, 1)
if err != nil {
fmt.Printf("new log file error:%v\n", err.Error())
return
}
golog.GlobalSqlLogger = golog.New(sqlFile, golog.Lfile|golog.Ltime|golog.Llevel)
runFilePath := path.Join(cfg.LogPath, runLogName)
runFile, err := golog.NewRotatingFileHandler(runFilePath, MaxLogSize, 1)
if err != nil {
fmt.Printf("new log file error:%v\n", err.Error())
return
}
golog.GlobalRunLogger = golog.New(runFile, golog.Lfile|golog.Ltime|golog.Llevel)
}
if *logLevel != "" {
setLogLevel(*logLevel)
} else {
setLogLevel(cfg.LogLevel)
}
var svr *server.Server
svr, err = server.NewServer(cfg)
if err != nil {
golog.Error("main", "main", err.Error(), 0)
golog.GlobalSysLogger.Close()
golog.GlobalSqlLogger.Close()
return
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sc
golog.Info("main", "main", "Got signal", 0, "signal", sig)
golog.GlobalSysLogger.Close()
golog.GlobalSqlLogger.Close()
svr.Close()
}()
//for debug serious issues
go func() {
err := http.ListenAndServe(":6033", nil)
golog.Error("Server", "Main", err.Error(), 0)
}()
svr.Run()
}
func setLogLevel(level string) {
switch strings.ToLower(level) {
case "debug":
golog.GlobalSysLogger.SetLevel(golog.LevelDebug)
case "info":
golog.GlobalSysLogger.SetLevel(golog.LevelInfo)
case "warn":
golog.GlobalSysLogger.SetLevel(golog.LevelWarn)
case "error":
golog.GlobalSysLogger.SetLevel(golog.LevelError)
default:
golog.GlobalSysLogger.SetLevel(golog.LevelError)
}
}