From 2b395f24aa463f537b1c646e443f6cef9f30ad08 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Fri, 17 May 2024 14:35:19 +0800 Subject: [PATCH] fix --- library/httpserver/httpserver.go | 49 +++++++++++++++++++------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/library/httpserver/httpserver.go b/library/httpserver/httpserver.go index b6178d80..bd169891 100644 --- a/library/httpserver/httpserver.go +++ b/library/httpserver/httpserver.go @@ -39,28 +39,39 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin setServerStatusFD(ServerStatusFDRun) }() - quit := make(chan os.Signal) - signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) - <-quit - logger.Info("Shutdown Server ...") + done := make(chan int) + go func() { + quit := make(chan os.Signal) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + for { + select { + case <-quit: + logger.Info("Shutdown Server ...") + if err := servicediscovery.DeRegister(ip, port); err != nil { + logger.Fatal("DeRegister fail: %v", err) + } - if err := servicediscovery.DeRegister(ip, port); err != nil { - logger.Fatal("DeRegister fail: %v", err) - } + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + if err := srv.Shutdown(ctx); err != nil { + logger.Fatal("Server Shutdown:", err) + } - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) - defer cancel() - if err := srv.Shutdown(ctx); err != nil { - logger.Fatal("Server Shutdown:", err) - } + select { + case <-ctx.Done(): + logger.Info("timeout of 1 seconds.") + } - select { - case <-ctx.Done(): - logger.Info("timeout of 1 seconds.") - } + logger.Info("Server exited") + setServerStatusFD(ServerStatusFDStop) + cancel() + done <- 1 + return + } + } + }() - logger.Info("Server exited") - setServerStatusFD(ServerStatusFDStop) + <-done + logger.Info("Server done") } func registerSD(ip string, port int) error { @@ -92,7 +103,7 @@ func registerSD(ip string, port int) error { } func setServerStatusFD(status string) { - serverStatusFdPath := "/app/SERVER_STATUS_FD" + serverStatusFdPath := "/Users/erwin/SERVER_STATUS_FD" cmd := exec.Command("sh", "-c", fmt.Sprintf("echo %s > %s", status, serverStatusFdPath)) err := cmd.Run() if err != nil {