|
@@ -4,12 +4,6 @@ import (
|
|
|
"context"
|
|
|
"crypto/md5"
|
|
|
"encoding/hex"
|
|
|
- "git.nspix.com/golang/micro/broker"
|
|
|
- "git.nspix.com/golang/micro/gateway/cli"
|
|
|
- "git.nspix.com/golang/micro/helper/machineid"
|
|
|
- "git.nspix.com/golang/micro/helper/rbtree"
|
|
|
- "git.nspix.com/golang/micro/stats/prometheusbackend"
|
|
|
- "github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
"math"
|
|
|
"math/rand"
|
|
|
"net"
|
|
@@ -23,6 +17,13 @@ import (
|
|
|
"syscall"
|
|
|
"time"
|
|
|
|
|
|
+ "git.nspix.com/golang/micro/broker"
|
|
|
+ "git.nspix.com/golang/micro/gateway/cli"
|
|
|
+ "git.nspix.com/golang/micro/helper/machineid"
|
|
|
+ "git.nspix.com/golang/micro/helper/rbtree"
|
|
|
+ "git.nspix.com/golang/micro/stats/prometheusbackend"
|
|
|
+ "github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
+
|
|
|
"git.nspix.com/golang/micro/gateway"
|
|
|
"git.nspix.com/golang/micro/gateway/http"
|
|
|
"git.nspix.com/golang/micro/gateway/rpc"
|
|
@@ -54,6 +55,7 @@ type Service struct {
|
|
|
timer *time.Timer
|
|
|
tickTree *rbtree.Tree
|
|
|
environment string
|
|
|
+ exitFlag int32
|
|
|
}
|
|
|
|
|
|
func (svr *Service) async(f func()) {
|
|
@@ -261,7 +263,7 @@ func (svr *Service) startHTTPServe() (err error) {
|
|
|
l := gateway.NewListener(svr.listener.Addr())
|
|
|
if err = svr.gateway.Attaches([][]byte{[]byte("GET"), []byte("POST"), []byte("PUT"), []byte("DELETE"), []byte("OPTIONS")}, l); err == nil {
|
|
|
svr.async(func() {
|
|
|
- if err = svr.httpSvr.Serve(l); err != nil {
|
|
|
+ if err = svr.httpSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
|
log.Warnf("http serve error: %s", err.Error())
|
|
|
}
|
|
|
log.Infof("http server stopped")
|
|
@@ -301,7 +303,7 @@ func (svr *Service) startRPCServe() (err error) {
|
|
|
l := gateway.NewListener(svr.listener.Addr())
|
|
|
if err = svr.gateway.Attach([]byte("RPC"), l); err == nil {
|
|
|
svr.async(func() {
|
|
|
- if err = svr.rpcSvr.Serve(l); err != nil {
|
|
|
+ if err = svr.rpcSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
|
log.Warnf("rpc serve start error: %s", err.Error())
|
|
|
}
|
|
|
log.Infof("rpc server stopped")
|
|
@@ -317,7 +319,7 @@ func (svr *Service) startCliServe() (err error) {
|
|
|
l := gateway.NewListener(svr.listener.Addr())
|
|
|
if err = svr.gateway.Attach([]byte("CLI"), l); err == nil {
|
|
|
svr.async(func() {
|
|
|
- if err = svr.cliSvr.Serve(l); err != nil {
|
|
|
+ if err = svr.cliSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
|
log.Warnf("cli serve start error: %s", err.Error())
|
|
|
}
|
|
|
log.Infof("cli server stopped")
|
|
@@ -391,6 +393,9 @@ func (svr *Service) prepare() (err error) {
|
|
|
}
|
|
|
|
|
|
func (svr *Service) destroy() (err error) {
|
|
|
+ if !atomic.CompareAndSwapInt32(&svr.exitFlag, 0, 1) {
|
|
|
+ return
|
|
|
+ }
|
|
|
log.Infof("service stopping")
|
|
|
svr.cancelFunc()
|
|
|
if !svr.opts.DisableRegister {
|
|
@@ -412,7 +417,6 @@ func (svr *Service) destroy() (err error) {
|
|
|
}
|
|
|
svr.timer.Stop()
|
|
|
if svr.tickTree.Size() > 0 {
|
|
|
- log.Warnf("num of %d defer tick dropped", svr.tickTree.Size())
|
|
|
node := svr.tickTree.Iterator()
|
|
|
for node != nil {
|
|
|
tick := node.Value.(*tickPtr)
|