mirror of
				https://github.com/smallnest/rpcx.git
				synced 2025-10-31 11:26:56 +08:00 
			
		
		
		
	implement graceful restart
This commit is contained in:
		| @@ -20,7 +20,7 @@ import ( | ||||
|  | ||||
| func (s *Server) startGateway(network string, ln net.Listener) net.Listener { | ||||
| 	if network != "tcp" && network != "tcp4" && network != "tcp6" { | ||||
| 		log.Infof("network is not tcp/tcp4/tcp6 so can not start gateway") | ||||
| 		// log.Infof("network is not tcp/tcp4/tcp6 so can not start gateway") | ||||
| 		return ln | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -18,6 +18,7 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"os/signal" | ||||
| 	"syscall" | ||||
|  | ||||
| @@ -751,6 +752,46 @@ func (s *Server) Shutdown(ctx context.Context) error { | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| // Restart restarts this server gracefully. | ||||
| // It starts a new rpcx server with the same port with SO_REUSEPORT socket option, | ||||
| // and shutdown this rpcx server gracefully. | ||||
| func (s *Server) Restart(ctx context.Context) error { | ||||
| 	pid, err := s.startProcess() | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	log.Infof("restart a new rpcx server: %d", pid) | ||||
|  | ||||
| 	// TODO: is it necessary? | ||||
| 	time.Sleep(3 * time.Second) | ||||
| 	return s.Shutdown(ctx) | ||||
| } | ||||
|  | ||||
| func (s *Server) startProcess() (int, error) { | ||||
| 	argv0, err := exec.LookPath(os.Args[0]) | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
|  | ||||
| 	// Pass on the environment and replace the old count key with the new one. | ||||
| 	var env []string | ||||
| 	for _, v := range os.Environ() { | ||||
| 		env = append(env, v) | ||||
| 	} | ||||
|  | ||||
| 	var originalWD, _ = os.Getwd() | ||||
| 	allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}) | ||||
| 	process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{ | ||||
| 		Dir:   originalWD, | ||||
| 		Env:   env, | ||||
| 		Files: allFiles, | ||||
| 	}) | ||||
| 	if err != nil { | ||||
| 		return 0, err | ||||
| 	} | ||||
| 	return process.Pid, nil | ||||
| } | ||||
|  | ||||
| func (s *Server) checkProcessMsg() bool { | ||||
| 	size := atomic.LoadInt32(&s.handlerMsgNum) | ||||
| 	log.Info("need handle in-processing msg size:", size) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 smallnest
					smallnest