AlexStocks commented on code in PR #668: URL: https://github.com/apache/dubbo-go-pixiu/pull/668#discussion_r2061132901
########## pkg/cluster/healthcheck/utils.go: ########## @@ -0,0 +1,65 @@ +/* + * 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 healthcheck + +import ( + "net" + "strings" + "time" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/logger" +) + +func CheckTcpConn(address string, port string, timeout time.Duration) bool { Review Comment: ```go func CheckTcpConn(address, port string, timeout time.Duration) error { normalizedAddress, err := normalizeAddress(address, port) if err != nil { return fmt.Errorf("[health check] address error: %w", err) } conn, err := net.DialTimeout("tcp", normalizedAddress, timeout) if err != nil { return fmt.Errorf("[health check] TCP connection to %s failed: %w", normalizedAddress, err) } defer conn.Close() return nil } func normalizeAddress(address, port string) (string, error) { host, parsedPort, err := net.SplitHostPort(address) if err == nil { if port != "" && parsedPort != port { return net.JoinHostPort(host, port), nil } return address, nil } // 解析失败,可能是因为缺少端口 if port == "" { return "", fmt.Errorf("no port specified and address is missing port: %s", address) } // 如果是缺少端口的错误 if strings.Contains(err.Error(), "missing port in address") { return net.JoinHostPort(address, port), nil } return "", fmt.Errorf("invalid address format: %s", address) } ``` 你这个函数写的比较绕,我用个 gpt 重构了下,你看着改进下,新函数还支持 ipv6 测试代码1: err := CheckTcpConn("qq.com", "80", 3*time.Second) if err != nil { logger.Warnf("TCP check failed: %v", err) } else { logger.Infof("TCP check succeeded") } 测试代码2: err := CheckTcpConn("[::1]", "8080", 2*time.Second) 可以把测试代码改成单测 -- 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: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org