AlexStocks commented on code in PR #3099:
URL: https://github.com/apache/dubbo-go/pull/3099#discussion_r2700773569


##########
server/server.go:
##########
@@ -329,7 +333,31 @@ func (s *Server) Serve() error {
        if err := exposed_tmp.RegisterServiceInstance(); err != nil {
                return err
        }
-       select {}
+
+       // Check if graceful_shutdown package is handling signals internally
+       // If InternalSignal is true (default), graceful_shutdown.Init() 
already set up signal handling
+       // and will call os.Exit(0) after cleanup, so we just block here.
+       // If InternalSignal is false, we need to handle signals ourselves and 
call cleanup.
+       if s.cfg.Shutdown != nil && s.cfg.Shutdown.InternalSignal != nil && 
*s.cfg.Shutdown.InternalSignal {
+               // graceful_shutdown package is handling signals, just block 
until shutdown
+               select {}

Review Comment:
   chatgpt:根据注释,`select{}` 用于无限期阻塞,等待中断信号。不过,更优雅的做法是确保仅在必要时才使用 
`select{}`,并且要妥善处理优雅关闭的逻辑。
   
   注释中提到,如果 `graceful_shutdown` 包已经在内部处理信号,我们应该检查并确保仅在适当的情况下阻塞关闭。具体来说,可以将 
`select{}` 替换为更加有控制的逻辑。
   
   你可以按照以下方式重构代码:
   
   ```go
   // 检查 graceful_shutdown 包是否在内部处理信号
   if !gracefulShutdownPkg.Init() {
       // 如果 InternalSignal 为 false,表示我们需要自己处理信号并调用清理函数。
       // 只在必要时阻塞,例如在清理或等待中断信号时。
       sigChan := make(chan os.Signal, 1)
       signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
   
       // 阻塞,直到接收到信号。
       sig := <-sigChan
       logger.Info("Received signal: ", sig)
   
       // 在接收到信号后,进行优雅关闭或清理
       gracefulShutdownPkg.InternalSignal()
   } else {
       // 如果 graceful_shutdown 包已经在处理信号,我们就让它继续处理关闭
       select {} // 阻塞,允许优雅关闭继续进行
   }
   ```
   
   ### 主要变化:
   
   1. **检查 `graceful_shutdown.Init()`:** 确认该包是否已经处理了信号。
   2. **手动处理信号:** 如果包没有处理信号,则我们手动设置信号监听器(`sigChan`),并仅在必要时阻塞(等待信号)。
   3. **适当使用 `select{}`:** 如果 `graceful_shutdown` 包已经在处理信号,我们才使用 `select{}` 
阻塞,以允许包完成关闭过程。
   
   这种方式可以避免不必要的阻塞,更好地控制关闭和清理的流程。
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to