forked from AlexStocks/getty
-
Notifications
You must be signed in to change notification settings - Fork 70
/
server.go
204 lines (177 loc) · 5.32 KB
/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
// "strings"
"crypto/tls"
"syscall"
"time"
)
import (
getty "github.com/apache/dubbo-getty"
"github.com/dubbogo/gost/net"
)
const (
pprofPath = "/debug/pprof/"
)
var (
// host = flag.String("host", "127.0.0.1", "local host address that server app will use")
// ports = flag.String("ports", "12345,12346,12347", "local host port list that the server app will bind")
)
var (
serverList []getty.Server
log = getty.GetLogger()
)
func main() {
// flag.Parse()
// if *host == "" || *ports == "" {
// panic(fmt.Sprintf("Please intput local host ip or port lists"))
// }
initConf()
initProfiling()
initServer()
log.Infof("%s starts successfull! its listen ends=%s:%s:%s",
conf.AppName, conf.Host, conf.Ports, conf.Paths)
initSignal()
}
func initProfiling() {
var addr string
// addr = *host + ":" + "10000"
addr = gxnet.HostAddress(conf.Host, conf.ProfilePort)
log.Infof("App Profiling startup on address{%v}", addr+pprofPath)
go func() {
log.Info(http.ListenAndServe(addr, nil))
}()
}
func newSession(session getty.Session) error {
var (
flag1, flag2 bool
tcpConn *net.TCPConn
)
_, flag1 = session.Conn().(*tls.Conn)
tcpConn, flag2 = session.Conn().(*net.TCPConn)
if !flag1 && !flag2 {
panic(fmt.Sprintf("%s, session.conn{%#v} is not tcp/tls connection\n", session.Stat(), session.Conn()))
}
if conf.GettySessionParam.CompressEncoding {
session.SetCompressType(getty.CompressZip)
}
// else {
// session.SetCompressType(getty.CompressNone)
// }
if flag2 {
tcpConn.SetNoDelay(conf.GettySessionParam.TcpNoDelay)
tcpConn.SetKeepAlive(conf.GettySessionParam.TcpKeepAlive)
tcpConn.SetReadBuffer(conf.GettySessionParam.TcpRBufSize)
tcpConn.SetWriteBuffer(conf.GettySessionParam.TcpWBufSize)
}
session.SetName(conf.GettySessionParam.SessionName)
session.SetMaxMsgLen(conf.GettySessionParam.MaxMsgLen)
session.SetPkgHandler(echoPkgHandler)
session.SetEventListener(echoMsgHandler)
session.SetReadTimeout(conf.GettySessionParam.tcpReadTimeout)
session.SetWriteTimeout(conf.GettySessionParam.tcpWriteTimeout)
session.SetCronPeriod((int)(conf.heartbeatPeriod.Nanoseconds() / 1e6))
session.SetWaitTime(conf.GettySessionParam.waitTimeout)
log.Debugf("app accepts new session:%s", session.Stat())
return nil
}
func initServer() {
var (
addr string
portList []string
pathList []string
server getty.Server
)
// if *host == "" {
// panic("host can not be nil")
// }
// if *ports == "" {
// panic("ports can not be nil")
// }
// portList = strings.Split(*ports, ",")
portList = conf.Ports
if len(portList) == 0 {
panic("portList is nil")
}
pathList = conf.Paths
if len(pathList) == 0 {
panic("pathList is nil")
}
if len(portList) != len(pathList) {
panic("the @Ports's length is not equal to @Paths.")
}
for idx, port := range portList {
addr = gxnet.HostAddress2(conf.Host, port)
if conf.CertFile != "" && conf.KeyFile != "" {
server = getty.NewWSSServer(
getty.WithLocalAddress(addr),
getty.WithWebsocketServerPath(pathList[idx]),
getty.WithWebsocketServerCert(conf.CertFile),
getty.WithWebsocketServerPrivateKey(conf.KeyFile),
getty.WithWebsocketServerRootCert(conf.CACert),
)
log.Debugf("server bind addr{wss://%s/%s} ok!", addr, pathList[idx])
} else {
server = getty.NewWSServer(
getty.WithLocalAddress(addr),
getty.WithWebsocketServerPath(pathList[idx]),
)
log.Debugf("server bind addr{ws://%s/%s} ok!", addr, pathList[idx])
}
server.RunEventLoop(newSession)
// run server
serverList = append(serverList, server)
}
}
func uninitServer() {
for _, server := range serverList {
server.Close()
}
}
func 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.Infof("get signal %s", sig.String())
switch sig {
case syscall.SIGHUP:
// reload()
default:
go time.AfterFunc(conf.failFastTimeout, func() {
// log.Warn("app exit now by force...")
// os.Exit(1)
log.Info("app exit now by force...")
})
// 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出
uninitServer()
// fmt.Println("app exit now...")
log.Info("app exit now...")
return
}
}
}