Skip to content

Commit

Permalink
modify canServe and make motanV1Compatible compatible with motan2, mo…
Browse files Browse the repository at this point in the history
…tan and motanV1Compatible
  • Loading branch information
liangwei3 committed Apr 8, 2024
1 parent bc604a1 commit 59d70fa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 1 addition & 2 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const (
DefaultMetaPrefix = "META_"
EnvMetaPrefixKey = "envMetaPrefix"
URLRegisterMeta = "registerMeta"
DefaultRegisterMeta = true
DefaultRegisterMeta = false
MetaCacheExpireSecondKey = "metaCacheExpireSecond"
DynamicMetaKey = "dynamicMeta"
DefaultDynamicMeta = true
Expand All @@ -163,7 +163,6 @@ const (
ServiceNotSupport = "service not support"
)


//----------- runtime -------------

const (
Expand Down
10 changes: 9 additions & 1 deletion core/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (u *URL) MergeParams(params map[string]string) {
}

func (u *URL) CanServe(other *URL) bool {
if u.Protocol != other.Protocol && u.Protocol != ProtocolLocal {
if !u.CanServeProtocol(other) {
vlog.Errorf("can not serve protocol, err : p1:%s, p2:%s", u.Protocol, other.Protocol)
return false
}
Expand All @@ -350,6 +350,14 @@ func (u *URL) CanServe(other *URL) bool {
return true
}

func (u *URL) CanServeProtocol(other *URL) bool {
// motanV1Compatible should compatible with motan2, motan and motanV1Compatible
if other.Protocol == "motanV1Compatible" && (u.Protocol == "motan2" || u.Protocol == "motan" || u.Protocol == "motanV1Compatible") {
return true
}
return u.Protocol == other.Protocol || u.Protocol == ProtocolLocal
}

func IsSame(m1 map[string]string, m2 map[string]string, key string, defaultValue string) bool {
if m1 == nil && m2 == nil {
return true
Expand Down
29 changes: 28 additions & 1 deletion core/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestCopyAndMerge(t *testing.T) {
}
}

func TestCanServer(t *testing.T) {
func TestCanServe(t *testing.T) {
params1 := make(map[string]string)
params2 := make(map[string]string)
url1 := &URL{Protocol: "motan", Path: "test/path", Parameters: params1}
Expand Down Expand Up @@ -146,6 +146,33 @@ func TestCanServer(t *testing.T) {
t.Fatalf("url CanServe testFail url1: %+v, url2: %+v\n", url1, url2)
}
fmt.Printf("url1:%+v, url2:%+v\n", url1, url2)
url1.Path = ""
url2.Path = ""
url1.Protocol = "motan2"
url2.Protocol = "motanV1Compatible"
if !url1.CanServe(url2) {
t.Fatalf("url CanServe testFail url1: %+v, url2: %+v\n", url1, url2)
}
url1.Protocol = "motan"
url2.Protocol = "motanV1Compatible"
if !url1.CanServe(url2) {
t.Fatalf("url CanServe testFail url1: %+v, url2: %+v\n", url1, url2)
}
url1.Protocol = "local"
url2.Protocol = "motanV1Compatible"
if !url1.CanServe(url2) {
t.Fatalf("url CanServe testFail url1: %+v, url2: %+v\n", url1, url2)
}
url1.Protocol = "abc"
url2.Protocol = "motanV1Compatible"
if url1.CanServe(url2) {
t.Fatalf("url CanServe testFail url1: %+v, url2: %+v\n", url1, url2)
}
url1.Protocol = "motan"
url2.Protocol = "motan2"
if url1.CanServe(url2) {
t.Fatalf("url CanServe testFail url1: %+v, url2: %+v\n", url1, url2)
}
}

func TestGetPositiveIntValue(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions dynamicConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ func (c *DynamicConfigurer) doUnregister(url *core.URL) error {
}

func (c *DynamicConfigurer) Subscribe(url *core.URL) error {
// use motanV1Compatible to compatible with protocol: motan
if url.Protocol == "motan" {
url.Protocol = "motanV1Compatible"
}
err := c.doSubscribe(url)
if err != nil {
return err
Expand Down

0 comments on commit 59d70fa

Please sign in to comment.