mirror of
https://github.com/smallnest/rpcx.git
synced 2025-10-20 22:59:26 +08:00
#245 shutdown gateway graceful too
This commit is contained in:
@@ -38,11 +38,24 @@ func (s *Server) startHTTP1APIGateway(ln net.Listener) {
|
||||
router.GET("/*servicePath", s.handleGatewayRequest)
|
||||
router.PUT("/*servicePath", s.handleGatewayRequest)
|
||||
|
||||
if err := http.Serve(ln, router); err != nil {
|
||||
s.mu.Lock()
|
||||
s.gatewayHTTPServer = &http.Server{Handler: router}
|
||||
s.mu.Unlock()
|
||||
if err := s.gatewayHTTPServer.Serve(ln); err != nil {
|
||||
log.Errorf("error in gateway Serve: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) closeHTTP1APIGateway(ctx context.Context) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.gatewayHTTPServer != nil {
|
||||
return s.gatewayHTTPServer.Shutdown(ctx)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) handleGatewayRequest(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
|
||||
if r.Header.Get(XServicePath) == "" {
|
||||
servicePath := params.ByName("servicePath")
|
||||
|
@@ -17,12 +17,13 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/smallnest/rpcx/log"
|
||||
"github.com/smallnest/rpcx/protocol"
|
||||
"github.com/smallnest/rpcx/share"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/smallnest/rpcx/log"
|
||||
"github.com/smallnest/rpcx/protocol"
|
||||
"github.com/smallnest/rpcx/share"
|
||||
)
|
||||
|
||||
// ErrServerClosed is returned by the Server's Serve, ListenAndServe after a call to Shutdown or Close.
|
||||
@@ -59,6 +60,7 @@ type Server struct {
|
||||
ln net.Listener
|
||||
readTimeout time.Duration
|
||||
writeTimeout time.Duration
|
||||
gatewayHTTPServer *http.Server
|
||||
|
||||
serviceMapMu sync.RWMutex
|
||||
serviceMap map[string]*service
|
||||
@@ -346,7 +348,6 @@ func (s *Server) serveConn(conn net.Conn) {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if s.writeTimeout != 0 {
|
||||
conn.SetWriteDeadline(t0.Add(s.writeTimeout))
|
||||
}
|
||||
@@ -423,7 +424,7 @@ func (s *Server) serveConn(conn net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
func isShutdown(s *Server) (bool) {
|
||||
func isShutdown(s *Server) bool {
|
||||
return atomic.LoadInt32(&s.inShutdown) == 1
|
||||
}
|
||||
|
||||
@@ -640,13 +641,13 @@ func (s *Server) RegisterOnShutdown(f func(s *Server)) {
|
||||
|
||||
var shutdownPollInterval = 1000 * time.Millisecond
|
||||
|
||||
// // Shutdown gracefully shuts down the server without interrupting any
|
||||
// // active connections. Shutdown works by first closing the
|
||||
// // listener, then closing all idle connections, and then waiting
|
||||
// // indefinitely for connections to return to idle and then shut down.
|
||||
// // If the provided context expires before the shutdown is complete,
|
||||
// // Shutdown returns the context's error, otherwise it returns any
|
||||
// // error returned from closing the Server's underlying Listener.
|
||||
// Shutdown gracefully shuts down the server without interrupting any
|
||||
// active connections. Shutdown works by first closing the
|
||||
// listener, then closing all idle connections, and then waiting
|
||||
// indefinitely for connections to return to idle and then shut down.
|
||||
// If the provided context expires before the shutdown is complete,
|
||||
// Shutdown returns the context's error, otherwise it returns any
|
||||
// error returned from closing the Server's underlying Listener.
|
||||
func (s *Server) Shutdown(ctx context.Context) error {
|
||||
if atomic.CompareAndSwapInt32(&s.inShutdown, 0, 1) {
|
||||
log.Info("shutdown begin")
|
||||
@@ -663,12 +664,20 @@ func (s *Server) Shutdown(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
s.Close()
|
||||
|
||||
if s.gatewayHTTPServer != nil {
|
||||
if err := s.closeHTTP1APIGateway(ctx); err != nil {
|
||||
log.Warnf("failed to close gateway: %v", err)
|
||||
} else {
|
||||
log.Info("closed gateway")
|
||||
}
|
||||
}
|
||||
log.Info("shutdown end")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) checkProcessMsg() (bool) {
|
||||
func (s *Server) checkProcessMsg() bool {
|
||||
size := s.handlerMsgNum
|
||||
log.Info("need handle msg size:", size)
|
||||
if size == 0 {
|
||||
|
Reference in New Issue
Block a user