Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl: support extension of two urls comparison. #854

Merged
merged 7 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change from URLComparator interface to Func
  • Loading branch information
cvictory committed Dec 9, 2020
commit 6ab25e4a363e7c6ec25ec8824add4468ad3f464c
2 changes: 1 addition & 1 deletion cluster/router/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func isInvokersChanged(left []protocol.Invoker, right []protocol.Invoker) bool {
rurl := r.GetUrl()
for _, l := range left {
lurl := l.GetUrl()
if common.GetURLComparator().CompareURLEqual(&lurl, &rurl, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY) {
if common.GetCompareURLEqualFunc()(lurl, rurl, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY) {
found = true
break
}
Expand Down
20 changes: 20 additions & 0 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var (
DubboNodes = [...]string{"consumers", "configurators", "routers", "providers"}
// DubboRole Dubbo service role
DubboRole = [...]string{"consumer", "", "routers", "provider"}
// CompareURLEqualFunc compare two url is equal
compareURLEqualFunc CompareURLEqualFunc
)

// nolint
Expand Down Expand Up @@ -770,3 +772,21 @@ func (s URLSlice) Less(i, j int) bool {
func (s URLSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

type CompareURLEqualFunc func(l *URL, r *URL, excludeParam ...string) bool

func defaultCompareURLEqual(l *URL, r *URL, excludeParam ...string) bool {
return IsEquals(l, r, excludeParam...)
}

func init() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to the top of this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

compareURLEqualFunc = defaultCompareURLEqual
}

func SetCompareURLEqualFunc(f CompareURLEqualFunc) {
compareURLEqualFunc = f
}

func GetCompareURLEqualFunc() CompareURLEqualFunc {
return compareURLEqualFunc
}
51 changes: 51 additions & 0 deletions common/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,5 +329,56 @@ func TestColonSeparatedKey(t *testing.T) {
assert.Equal(t, u1.ColonSeparatedKey(), u1.GetParam(constant.INTERFACE_KEY, "")+":version1:group1")
u1.SetParam(constant.VERSION_KEY, "")
assert.Equal(t, u1.ColonSeparatedKey(), u1.GetParam(constant.INTERFACE_KEY, "")+"::group1")
}

func TestCompareURLEqualFunc(t *testing.T) {
// test Default
url1, _ := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=1556509797245")
url2, _ := NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=155650979798")
assert.False(t, GetCompareURLEqualFunc()(url1, url2))
assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY))

// test custom
url1, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=1556509797245")
url2, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000&timestamp=155650979798")
assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY))
SetCompareURLEqualFunc(CustomCompareURLEqual)
assert.False(t, GetCompareURLEqualFunc()(url1, url2))
assert.False(t, GetCompareURLEqualFunc()(url1, url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY))

url1, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000")
url2, _ = NewURL("dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&" +
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
"side=provider&timeout=3000")
assert.True(t, GetCompareURLEqualFunc()(url1, url2))
assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY))
SetCompareURLEqualFunc(CustomCompareURLEqual)
assert.True(t, GetCompareURLEqualFunc()(url1, url2))
assert.True(t, GetCompareURLEqualFunc()(url1, url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY))
}

func CustomCompareURLEqual(l *URL, r *URL, execludeParam ...string) bool {
return l.PrimitiveURL == r.PrimitiveURL
}
46 changes: 0 additions & 46 deletions common/url_tool_extension.go

This file was deleted.

84 changes: 0 additions & 84 deletions common/url_tool_extension_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions registry/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ func (dir *RegistryDirectory) cacheInvoker(url *common.URL) protocol.Invoker {
} else {
// if cached invoker has the same URL with the new URL, then no need to re-refer, and no need to destroy
// the old invoker.
urlTmp := cacheInvoker.(protocol.Invoker).GetUrl()
if common.GetURLComparator().CompareURLEqual(newUrl, &urlTmp) {
if common.GetCompareURLEqualFunc()(newUrl, cacheInvoker.(protocol.Invoker).GetUrl()) {
return nil
}

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.