This is an automated email from the ASF dual-hosted git repository.

tigerlee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git


The following commit(s) were added to refs/heads/master by this push:
     new 0c9a9b3a fix: fix endpoints parsing in Go Client (#444)
0c9a9b3a is described below

commit 0c9a9b3a01e444a331e367f59308602757f2496e
Author: imp2002 <[email protected]>
AuthorDate: Thu Apr 6 11:36:37 2023 +0800

    fix: fix endpoints parsing in Go Client (#444)
---
 golang/pkg/utils/utils.go      | 47 ++++++++++++++++++++++--------------------
 golang/pkg/utils/utils_test.go | 20 ++++++++++++++++++
 2 files changed, 45 insertions(+), 22 deletions(-)

diff --git a/golang/pkg/utils/utils.go b/golang/pkg/utils/utils.go
index 76be7935..db145e4d 100644
--- a/golang/pkg/utils/utils.go
+++ b/golang/pkg/utils/utils.go
@@ -72,31 +72,34 @@ func ParseAddress(address *v2.Address) string {
 func ParseTarget(target string) (*v2.Endpoints, error) {
        ret := &v2.Endpoints{
                Scheme: v2.AddressScheme_DOMAIN_NAME,
-               Addresses: []*v2.Address{
-                       {
-                               Host: "",
-                               Port: 80,
-                       },
-               },
        }
-       path := target
-       u, err := url.Parse(target)
-       if err != nil {
-               path = target
-               ret.Scheme = v2.AddressScheme_IPv4
-       } else {
-               if u.Host != "" {
-                       path = u.Host
+       addressRawList := strings.Split(target, ";")
+       for _, path := range addressRawList {
+               if len(path) == 0 {
+                       continue
                }
-       }
-       paths := strings.Split(path, ":")
-       if len(paths) > 1 {
-               if port, err2 := strconv.ParseInt(paths[1], 10, 32); err2 == 
nil {
-                       ret.Addresses[0].Port = int32(port)
+               address := &v2.Address{
+                       Host: "",
+                       Port: 80,
                }
-               ret.Addresses[0].Host = paths[0]
-       } else {
-               return nil, fmt.Errorf("parse target failed, target=%s", target)
+               if u, err := url.Parse(path); err != nil {
+                       address.Host = path
+                       ret.Scheme = v2.AddressScheme_IPv4
+               } else {
+                       if u.Host != "" {
+                               address.Host = u.Host
+                       }
+               }
+               paths := strings.Split(path, ":")
+               if len(paths) > 1 {
+                       if port, err2 := strconv.ParseInt(paths[1], 10, 32); 
err2 == nil {
+                               address.Port = int32(port)
+                       }
+                       address.Host = paths[0]
+               } else {
+                       return nil, fmt.Errorf("parse target failed, 
target=%s", target)
+               }
+               ret.Addresses = append(ret.Addresses, address)
        }
        return ret, nil
 }
diff --git a/golang/pkg/utils/utils_test.go b/golang/pkg/utils/utils_test.go
index c26f8c4f..bf43349a 100644
--- a/golang/pkg/utils/utils_test.go
+++ b/golang/pkg/utils/utils_test.go
@@ -73,6 +73,26 @@ func TestParseTarget(t *testing.T) {
        if err != nil {
                t.Error(err)
        }
+
+       endpointsExpect := &v2.Endpoints{
+               Scheme: v2.AddressScheme_IPv4,
+               Addresses: []*v2.Address{
+                       {
+                               Host: "127.0.0.1",
+                               Port: 80,
+                       },
+                       {
+                               Host: "127.0.0.1",
+                               Port: 81,
+                       },
+               },
+       }
+       endpoints, err := ParseTarget("127.0.0.1:80;127.0.0.1:81;")
+       if err != nil {
+               t.Error(err)
+       } else if !CompareEndpoints(endpointsExpect, endpoints) {
+               t.Errorf("Expected endpoints: %v, but got: %v", 
endpointsExpect, endpoints)
+       }
 }
 
 func TestMatchMessageType(t *testing.T) {

Reply via email to