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


##########
filter/hystrix/go-server/cmd/main.go:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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"
+       "fmt"
+       "time"
+)
+
+import (
+       _ "dubbo.apache.org/dubbo-go/v3/imports"
+       "dubbo.apache.org/dubbo-go/v3/protocol"
+       "dubbo.apache.org/dubbo-go/v3/server"
+
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       greet "github.com/apache/dubbo-go-samples/filter/hystrix/proto"
+)
+
+type GreetTripleServer struct {
+       requestCount int
+}
+
+func (srv *GreetTripleServer) Greet(ctx context.Context, req 
*greet.GreetRequest) (*greet.GreetResponse, error) {
+       srv.requestCount++

Review Comment:
   [P1] 这里的 `requestCount++` 在这个示例里会被并发访问。客户端第二阶段一次性起了 15 个 
goroutine,请求会同时打到服务端,这个自增和后面的读取都会形成数据竞争;`go test -race` 或实际高并发运行时都可能出现错误计数。建议改成 
`atomic` 计数,或者用互斥锁把递增和读取包起来。



##########
filter/hystrix/go-client/cmd/main.go:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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"
+       "fmt"
+       "time"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/client"
+       _ "dubbo.apache.org/dubbo-go/v3/imports"
+
+       "github.com/afex/hystrix-go/hystrix"
+
+       _ "github.com/apache/dubbo-go-extensions/filter/hystrix"
+
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       greet "github.com/apache/dubbo-go-samples/filter/hystrix/proto"
+)
+
+func init() {
+       // Configure hystrix command for the GreetService.Greet method
+       // Resource name format: 
dubbo:consumer:InterfaceName:group:version:Method
+       // For this example: dubbo:consumer:greet.GreetService:::Greet
+       cmdName := "dubbo:consumer:greet.GreetService:::Greet"
+
+       hystrix.ConfigureCommand(cmdName, hystrix.CommandConfig{
+               Timeout:                1000, // 1 second timeout
+               MaxConcurrentRequests:  10,   // Max 10 concurrent requests
+               RequestVolumeThreshold: 5,    // Minimum 5 requests before 
circuit can trip
+               SleepWindow:            5000, // 5 seconds to wait after 
circuit opens before testing
+               ErrorPercentThreshold:  50,   // 50% error rate triggers 
circuit opening
+       })
+
+       logger.Infof("Configured hystrix command: %s", cmdName)
+}
+
+func main() {
+       cli, err := client.NewClient(
+               client.WithClientURL("127.0.0.1:20000"),
+       )
+       if err != nil {
+               panic(err)
+       }
+
+       svc, err := greet.NewGreetService(cli, 
client.WithFilter("hystrix_consumer"))
+       if err != nil {
+               panic(err)
+       }
+
+       // Test 1: Normal requests
+       logger.Info("=== Test 1: Sending normal requests ===")
+       for i := 1; i <= 3; i++ {
+               resp, err := svc.Greet(context.Background(), 
&greet.GreetRequest{Name: fmt.Sprintf("request-%d", i)})
+               if err != nil {
+                       panic(err)
+               } else {
+                       logger.Infof("Request %d success: %s", i, resp.Greeting)
+               }
+       }
+
+       // Test 2: Multiple concurrent requests to potentially trigger circuit 
breaker
+       logger.Info("\n=== Test 2: Sending concurrent requests ===")
+       for i := 1; i <= 15; i++ {
+               go func(idx int) {
+                       resp, err := svc.Greet(context.Background(), 
&greet.GreetRequest{Name: fmt.Sprintf("concurrent-%d", idx)})
+                       if err != nil {

Review Comment:
   [P1] 这个示例的目标就是演示熔断器触发后的行为,但这里把调用 error 直接 `panic` 了。Hystrix 一旦按预期返回 
`hystrix: circuit open` 或超时错误,示例客户端会立刻崩溃,后面的“观察熔断/恢复”流程根本跑不完,和 README 
里描述的预期输出也矛盾。建议把这些 error 当成可观察结果记录下来,而不是直接 panic,至少在并发测试和熔断恢复阶段要继续执行。



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