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

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


The following commit(s) were added to refs/heads/master by this push:
     new 5eab91b  fix: snapshot file will be overwritten and mixed if multiple 
consumers or producers use different name server domains (#1099)
5eab91b is described below

commit 5eab91baccb1401dfd72d6559bdb9d9a1a97be00
Author: WeizhongTu <tuweizh...@163.com>
AuthorDate: Mon Sep 25 16:09:03 2023 +0800

    fix: snapshot file will be overwritten and mixed if multiple consumers or 
producers use different name server domains (#1099)
---
 primitive/nsresolver.go      | 14 +++++++++-----
 primitive/nsresolver_test.go | 14 +++++++-------
 2 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/primitive/nsresolver.go b/primitive/nsresolver.go
index 67ca7a7..95e1196 100644
--- a/primitive/nsresolver.go
+++ b/primitive/nsresolver.go
@@ -17,6 +17,8 @@ limitations under the License.
 package primitive
 
 import (
+       "crypto/md5"
+       "encoding/hex"
        "fmt"
        "io/ioutil"
        "net/http"
@@ -139,7 +141,7 @@ func (h *HttpResolver) Resolve() []string {
 }
 
 func (h *HttpResolver) Description() string {
-       return fmt.Sprintf("passthrough resolver of domain:%v instance:%v", 
h.domain, h.instance)
+       return fmt.Sprintf("http resolver of domain:%v", h.domain)
 }
 
 func (h *HttpResolver) get() []string {
@@ -177,7 +179,7 @@ func (h *HttpResolver) get() []string {
 }
 
 func (h *HttpResolver) saveSnapshot(body []byte) error {
-       filePath := h.getSnapshotFilePath(h.instance)
+       filePath := h.getSnapshotFilePath()
        err := ioutil.WriteFile(filePath, body, 0644)
        if err != nil {
                rlog.Error("name server snapshot save failed", 
map[string]interface{}{
@@ -194,7 +196,7 @@ func (h *HttpResolver) saveSnapshot(body []byte) error {
 }
 
 func (h *HttpResolver) loadSnapshot() []string {
-       filePath := h.getSnapshotFilePath(h.instance)
+       filePath := h.getSnapshotFilePath()
        _, err := os.Stat(filePath)
        if os.IsNotExist(err) {
                rlog.Warning("name server snapshot local file not exists", 
map[string]interface{}{
@@ -214,7 +216,7 @@ func (h *HttpResolver) loadSnapshot() []string {
        return strings.Split(string(bs), ";")
 }
 
-func (h *HttpResolver) getSnapshotFilePath(instanceName string) string {
+func (h *HttpResolver) getSnapshotFilePath() string {
        homeDir := ""
        if usr, err := user.Current(); err == nil {
                homeDir = usr.HomeDir
@@ -232,6 +234,8 @@ func (h *HttpResolver) getSnapshotFilePath(instanceName 
string) string {
                        })
                }
        }
-       filePath := path.Join(storePath, fmt.Sprintf("nameserver_addr-%s", 
instanceName))
+       hash := md5.Sum([]byte(h.domain))
+       domainHash := hex.EncodeToString(hash[:])
+       filePath := path.Join(storePath, fmt.Sprintf("nameserver_addr-%s", 
domainHash))
        return filePath
 }
diff --git a/primitive/nsresolver_test.go b/primitive/nsresolver_test.go
index 94d80db..3056cd8 100644
--- a/primitive/nsresolver_test.go
+++ b/primitive/nsresolver_test.go
@@ -75,7 +75,7 @@ func TestHttpResolverWithGet(t *testing.T) {
                resolver.Resolve()
 
                // check snapshot saved
-               filePath := resolver.getSnapshotFilePath("DEFAULT")
+               filePath := resolver.getSnapshotFilePath()
                body := strings.Join(srvs, ";")
                bs, _ := ioutil.ReadFile(filePath)
                So(string(bs), ShouldEqual, body)
@@ -112,7 +112,7 @@ func TestHttpResolverWithGetUnitName(t *testing.T) {
                resolver.Resolve()
 
                // check snapshot saved
-               filePath := resolver.getSnapshotFilePath("DEFAULT")
+               filePath := resolver.getSnapshotFilePath()
                body := strings.Join(srvs, ";")
                bs, _ := ioutil.ReadFile(filePath)
                So(string(bs), ShouldEqual, body)
@@ -133,7 +133,7 @@ func TestHttpResolverWithSnapshotFile(t *testing.T) {
 
                os.Setenv("NAMESRV_ADDR", "") // clear env
                // setup local snapshot file
-               filePath := resolver.getSnapshotFilePath("DEFAULT")
+               filePath := resolver.getSnapshotFilePath()
                body := strings.Join(srvs, ";")
                _ = ioutil.WriteFile(filePath, []byte(body), 0644)
 
@@ -143,7 +143,7 @@ func TestHttpResolverWithSnapshotFile(t *testing.T) {
        })
 }
 
-func TesHttpReslverWithSnapshotFileOnce(t *testing.T) {
+func TestHttpResolverWithSnapshotFileOnce(t *testing.T) {
        Convey("Test UpdateNameServerAddress Load Local Snapshot Once", t, 
func() {
                srvs := []string{
                        "192.168.100.1",
@@ -157,18 +157,18 @@ func TesHttpReslverWithSnapshotFileOnce(t *testing.T) {
 
                os.Setenv("NAMESRV_ADDR", "") // clear env
                // setup local snapshot file
-               filePath := resolver.getSnapshotFilePath("DEFAULT")
+               filePath := resolver.getSnapshotFilePath()
                body := strings.Join(srvs, ";")
                _ = ioutil.WriteFile(filePath, []byte(body), 0644)
                // load local snapshot file first time
                addrs1 := resolver.Resolve()
 
-               // change the local snapshot file to check load once
+               // change the local snapshot file
                _ = ioutil.WriteFile(filePath, []byte("127.0.0.1;127.0.0.2"), 
0644)
 
                addrs2 := resolver.Resolve()
 
-               So(Diff(addrs1, addrs2), ShouldBeFalse)
+               So(Diff(addrs1, addrs2), ShouldBeTrue)
                So(Diff(addrs1, srvs), ShouldBeFalse)
        })
 }

Reply via email to