-
Notifications
You must be signed in to change notification settings - Fork 5
/
mysql_server.go
176 lines (155 loc) · 5.35 KB
/
mysql_server.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
package net
import (
"fmt"
"github.com/dubbogo/gost/sync"
"github.com/goioc/di"
log "github.com/sirupsen/logrus"
"github.com/zhukovaskychina/xmysql-server/server/conf"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
const (
pprofPath = "/debug/pprof/"
)
const logBanner = `
******************************************************************************************
__ ____ __ _____ ____ _ _____ ______ _______ ________ _____
\ \ / / \/ | / ____|/ __ \| | / ____| ____| __ \ \ / / ____| __ \
\ V /| \ / |_ _| (___ | | | | | _____| (___ | |__ | |__) \ \ / /| |__ | |__) |
> < | |\/| | | | |\___ \| | | | | |______\___ \| __| | _ / \ \/ / | __| | _ /
/ . \| | | | |_| |____) | |__| | |____ ____) | |____| | \ \ \ / | |____| | \ \
/_/ \_\_| |_|\__, |_____/ \___\_\______| |_____/|______|_| \_\ \/ |______|_| \_\
__/ |
|___/
******************************************************************************************
`
var (
mysqlPkgHandler = NewMySQLEchoPkgHandler() //Marsh Unmarsh
)
type MySQLServer struct {
conf *conf.Cfg
serverList []Server
taskPool gxsync.GenericTaskPool
}
func NewMySQLServer(conf *conf.Cfg) *MySQLServer {
return &MySQLServer{
conf: conf,
serverList: nil,
taskPool: gxsync.NewTaskPoolSimple(0),
}
}
func (srv *MySQLServer) Start() {
initProfiling(srv.conf)
di.RegisterBeanInstance("globalConfig", srv.conf)
srv.taskPool = gxsync.NewTaskPoolSimple(0)
srv.initServer(srv.conf)
log.Println(logBanner)
log.Info("%s starts successfull! its version=%s, its listen ends=%s:%s\n",
srv.conf.AppName, Version, srv.conf.BindAddress, srv.conf.Port)
log.Info("%s starts successfull! its version=%s, its listen ends=%s:%s\n",
srv.conf.AppName, Version, srv.conf.BindAddress, srv.conf.Port)
//srv.initPurgeThread()
di.InitializeContainer()
srv.initSignal()
}
func initProfiling(conf *conf.Cfg) {
var (
addr string
)
addr = net.JoinHostPort(conf.BindAddress, string(conf.ProfilePort))
log.Info("App Profiling startup on address{%v}", addr+pprofPath)
go func() {
log.Info(http.ListenAndServe(addr, nil))
}()
}
func (srv *MySQLServer) initServer(conf *conf.Cfg) {
var (
addr string
portList []string
server Server
)
mysqlMsgHandler := NewMySQLMessageHandler(conf)
portList = append(portList, strconv.Itoa(conf.Port))
if len(portList) == 0 {
panic("portList is nil")
}
for _, port := range portList {
addr = net.JoinHostPort(conf.BindAddress, port)
serverOpts := []ServerOption{WithLocalAddress(addr)}
//serverOpts = append(serverOpts, getty.WithServerTaskPool(srv.taskPool))
server = NewTCPServer(serverOpts...)
// run serverimpl
server.RunEventLoop(func(session Session) error {
var (
ok bool
tcpConn *net.TCPConn
)
if conf.MySQLSessionParam.CompressEncoding {
session.SetCompressType(CompressZip)
}
if tcpConn, ok = session.Conn().(*net.TCPConn); !ok {
panic(fmt.Sprintf("%s, session.conn{%#v} is not tcp connection\n", session.Stat(), session.Conn()))
}
tcpConn.SetNoDelay(conf.MySQLSessionParam.TcpNoDelay)
tcpConn.SetKeepAlive(conf.MySQLSessionParam.TcpKeepAlive)
if conf.MySQLSessionParam.TcpKeepAlive {
tcpConn.SetKeepAlivePeriod(conf.MySQLSessionParam.KeepAlivePeriodDuration)
}
tcpConn.SetReadBuffer(conf.MySQLSessionParam.TcpRBufSize)
tcpConn.SetWriteBuffer(conf.MySQLSessionParam.TcpWBufSize)
session.SetName(conf.MySQLSessionParam.SessionName)
session.SetMaxMsgLen(conf.MySQLSessionParam.MaxMsgLen)
session.SetPkgHandler(mysqlPkgHandler)
session.SetEventListener(mysqlMsgHandler)
session.SetWQLen(conf.MySQLSessionParam.PkgWQSize)
session.SetReadTimeout(conf.MySQLSessionParam.TcpReadTimeoutDuration)
session.SetWriteTimeout(conf.MySQLSessionParam.TcpWriteTimeoutDuration)
session.SetCronPeriod((int)(conf.SessionTimeoutDuration / 1e6))
session.SetWaitTime(conf.MySQLSessionParam.WaitTimeoutDuration)
//session.SetTaskPool(taskPool)
log.Debug("app accepts new session:%s\n", session.Stat())
return nil
})
log.Debug("serverimpl bind addr{%s} ok!", addr)
srv.serverList = append(srv.serverList, server)
}
}
func (srv *MySQLServer) uninitServer() {
for _, server := range srv.serverList {
server.Close()
}
if srv.taskPool != nil {
srv.taskPool.Close()
}
}
func (srv *MySQLServer) initSignal() {
// signal.Notify的ch信道是阻塞的(signal.Notify不会阻塞发送信号), 需要设置缓冲
signals := make(chan os.Signal, 1)
// It is not possible to block SIGKILL or syscall.SIGSTOP
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
sig := <-signals
log.Info("get signal %s", sig.String())
switch sig {
case syscall.SIGHUP:
// reload()
default:
go time.AfterFunc(srv.conf.FailFastTimeoutDuration, func() {
// log.Warn("app exit now by force...")
// os.Exit(1)
log.Info("app exit now by force...")
})
// 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出
srv.uninitServer()
di.Close()
// fmt.Println("app exit now...")
log.Info("app exit now...")
return
}
}
}