AlexStocks commented on code in PR #1032:
URL: https://github.com/apache/dubbo-go-samples/pull/1032#discussion_r2825399864


##########
router/condition/go-client/cmd/client.go:
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package main
+
+import (
+       "context"
+       "time"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3"
+       "dubbo.apache.org/dubbo-go/v3/config_center"
+       _ "dubbo.apache.org/dubbo-go/v3/imports"
+       "dubbo.apache.org/dubbo-go/v3/registry"
+
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       greet "github.com/apache/dubbo-go-samples/direct/proto"
+)
+
+const (
+       RegistryAddress = "127.0.0.1:8848"
+)
+
+func main() {
+       ins, err := dubbo.NewInstance(
+               dubbo.WithName("condition-client"),
+               dubbo.WithRegistry(
+                       registry.WithNacos(),
+                       registry.WithAddress(RegistryAddress),
+               ),
+               dubbo.WithConfigCenter( // configure config center to enable 
condition router
+                       config_center.WithNacos(),
+                       config_center.WithAddress(RegistryAddress),
+               ),
+       )
+
+       if err != nil {
+               logger.Errorf("new instance failed: %v", err)
+               panic(err)
+       }
+
+       cli, err := ins.NewClient()
+
+       if err != nil {
+               logger.Errorf("new client failed: %v", err)
+               panic(err)
+       }
+
+       srv, err := greet.NewGreetService(cli)
+
+       if err != nil {
+               logger.Errorf("new service failed: %v", err)
+               panic(err)
+       }
+
+       for {
+               time.Sleep(5 * time.Second) // sleep 5 seconds
+               rep, err := srv.Greet(context.Background(), 
&greet.GreetRequest{Name: "hello world"})
+               printRes(rep, err)
+       }

Review Comment:
   // 1. 准备信号监听
       sigs := make(chan os.Signal, 1)
       signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
   
       // 2. 初始化 Ticker
       ticker := time.NewTicker(5 * time.Second)
       
       // 【关键】使用 defer 确保函数退出时,计时器资源被释放
       // 无论是由于 return 还是 panic,这行都会执行
       defer ticker.Stop() 
   
       ctx, cancel := context.WithCancel(context.Background())
       defer cancel()
   
       fmt.Println("服务运行中,按 Ctrl+C 安全退出...")
   
       // 3. 主循环
       done := false
       for !done {
           select {
           case <-sigs:
               fmt.Println("\n[信号] 接收到退出请求,清理资源中...")
               // 停止向通道发送心跳,虽然 defer 也会做,但这里显式停止是好习惯
               ticker.Stop() 
               done = true // 跳出循环
   
           case <-ticker.C:
               // 正常执行业务逻辑
               rep, err := srv.Greet(ctx, &greet.GreetRequest{Name: "hello 
world"})
               printRes(rep, err)
           }
       }
   
   问下 gpt 或者 doubao,上面这种优雅退出代码很多



-- 
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