Explorar o código

修复热重启问题

zhanghongbo %!s(int64=9) %!d(string=hai) anos
pai
achega
3cc1358c06
Modificáronse 3 ficheiros con 24 adicións e 9 borrados
  1. 9 7
      weixin/src/endless/endless.go
  2. 3 1
      weixin/src/main.go
  3. 12 1
      weixin/src/qfw/weixin/rpc/rpc.go

+ 9 - 7
weixin/src/endless/endless.go

@@ -89,7 +89,7 @@ type endlessServer struct {
 NewServer returns an intialized endlessServer Object. Calling Serve on it will
 NewServer returns an intialized endlessServer Object. Calling Serve on it will
 actually "start" the server.
 actually "start" the server.
 */
 */
-func NewServer(addr string, handler http.Handler) (srv *endlessServer) {
+func NewServer(addr string, handler http.Handler, fn func()) (srv *endlessServer) {
 	runningServerReg.Lock()
 	runningServerReg.Lock()
 	defer runningServerReg.Unlock()
 	defer runningServerReg.Unlock()
 	if !flag.Parsed() {
 	if !flag.Parsed() {
@@ -109,7 +109,9 @@ func NewServer(addr string, handler http.Handler) (srv *endlessServer) {
 		isChild: isChild,
 		isChild: isChild,
 		SignalHooks: map[int]map[os.Signal][]func(){
 		SignalHooks: map[int]map[os.Signal][]func(){
 			PRE_SIGNAL: map[os.Signal][]func(){
 			PRE_SIGNAL: map[os.Signal][]func(){
-				syscall.SIGHUP:  []func(){},
+				syscall.SIGHUP: []func(){
+					fn,
+				},
 				syscall.SIGUSR1: []func(){},
 				syscall.SIGUSR1: []func(){},
 				syscall.SIGUSR2: []func(){},
 				syscall.SIGUSR2: []func(){},
 				syscall.SIGINT:  []func(){},
 				syscall.SIGINT:  []func(){},
@@ -146,8 +148,8 @@ ListenAndServe listens on the TCP network address addr and then calls Serve
 with handler to handle requests on incoming connections. Handler is typically
 with handler to handle requests on incoming connections. Handler is typically
 nil, in which case the DefaultServeMux is used.
 nil, in which case the DefaultServeMux is used.
 */
 */
-func ListenAndServe(addr string, handler http.Handler) error {
-	server := NewServer(addr, handler)
+func ListenAndServe(addr string, handler http.Handler, fn func()) error {
+	server := NewServer(addr, handler, fn)
 	return server.ListenAndServe()
 	return server.ListenAndServe()
 }
 }
 
 
@@ -158,8 +160,8 @@ private key for the server must be provided. If the certificate is signed by a
 certificate authority, the certFile should be the concatenation of the server's
 certificate authority, the certFile should be the concatenation of the server's
 certificate followed by the CA's certificate.
 certificate followed by the CA's certificate.
 */
 */
-func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler) error {
-	server := NewServer(addr, handler)
+func ListenAndServeTLS(addr string, certFile string, keyFile string, handler http.Handler, destoryfn func()) error {
+	server := NewServer(addr, handler, destoryfn)
 	return server.ListenAndServeTLS(certFile, keyFile)
 	return server.ListenAndServeTLS(certFile, keyFile)
 }
 }
 
 
@@ -418,7 +420,7 @@ func (srv *endlessServer) hammerTime(d time.Duration) {
 func (srv *endlessServer) fork() (err error) {
 func (srv *endlessServer) fork() (err error) {
 	runningServerReg.Lock()
 	runningServerReg.Lock()
 	defer runningServerReg.Unlock()
 	defer runningServerReg.Unlock()
-	
+
 	// only one server isntance should fork!
 	// only one server isntance should fork!
 	if runningServersForked {
 	if runningServersForked {
 		return errors.New("Another process already forked. Ignoring this one.")
 		return errors.New("Another process already forked. Ignoring this one.")

+ 3 - 1
weixin/src/main.go

@@ -38,5 +38,7 @@ func main() {
 	rpc.StartWeixinRpc(weixin.Mux)
 	rpc.StartWeixinRpc(weixin.Mux)
 	//启动web服务
 	//启动web服务
 	//http.ListenAndServe(":"+wf.SysConfig.Port, nil) // 启动接收微信数据服务器
 	//http.ListenAndServe(":"+wf.SysConfig.Port, nil) // 启动接收微信数据服务器
-	endless.ListenAndServe(":"+wf.SysConfig.Port, nil)
+	endless.ListenAndServe(":"+wf.SysConfig.Port, nil, func() {
+		rpc.DestoryRpc()
+	})
 }
 }

+ 12 - 1
weixin/src/qfw/weixin/rpc/rpc.go

@@ -200,15 +200,26 @@ func (wxrpc *WeiXinRpc) SendOffLineMsg(param *qrpc.NotifyMsg, ret *qrpc.RpcResul
 	return
 	return
 }
 }
 
 
+var listen net.Listener
+
 func StartWeixinRpc(wx *weixin.Weixin) {
 func StartWeixinRpc(wx *weixin.Weixin) {
 	wrpc := &WeiXinRpc{wx: wx}
 	wrpc := &WeiXinRpc{wx: wx}
 	//在此可以注册多个Rpc服务接口
 	//在此可以注册多个Rpc服务接口
 	rpc.Register(wrpc)
 	rpc.Register(wrpc)
 	rpc.HandleHTTP()
 	rpc.HandleHTTP()
-	listen, err := net.Listen("tcp", ":"+wf.SysConfig.Rpcport)
+	var err error
+	listen, err = net.Listen("tcp", ":"+wf.SysConfig.Rpcport)
 	if err != nil {
 	if err != nil {
 		log.Println(err.Error())
 		log.Println(err.Error())
 	} else {
 	} else {
 		go http.Serve(listen, nil)
 		go http.Serve(listen, nil)
 	}
 	}
 }
 }
+
+//
+func DestoryRpc() {
+	if listen != nil {
+		log.Println("close weixin rpc netlisten")
+		listen.Close()
+	}
+}