diff --git a/cluster/router/chain/chain.go b/cluster/router/chain/chain.go index 4c3cf8c2a77e95f802e4cd644f53b818d70d3389..3efed158686322e80bc56166993d9d39da8169ce 100644 --- a/cluster/router/chain/chain.go +++ b/cluster/router/chain/chain.go @@ -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.GetURLTool().CompareURLEqual(&lurl, &rurl, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY) { + if common.GetURLComparator().CompareURLEqual(&lurl, &rurl, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY) { found = true break } diff --git a/common/url_tool_extension.go b/common/url_tool_extension.go index 84899fca51deb47ca0194983db6afbd265c746ba..e446f7ee69a8f95aacee5fda3208db84350bc988 100644 --- a/common/url_tool_extension.go +++ b/common/url_tool_extension.go @@ -17,50 +17,30 @@ package common -import "sync" - -var urlTool URLTool -var lock sync.Mutex +var urlComparator URLComparator // Define some func for URL, such as comparison of URL. -// Your can define your own implements, and invoke SetURLTool for high priority. -type URLTool interface { - // The higher of the number, the higher of the priority. - Priority() uint8 - // comparison of two URL, excluding some params +// Your can define your own implements, and invoke SetURLComparator. +type URLComparator interface { CompareURLEqual(*URL, *URL, ...string) bool } -func SetURLTool(tool URLTool) { - lock.Lock() - defer lock.Unlock() - if urlTool == nil { - urlTool = tool - return - } - if urlTool.Priority() < tool.Priority() { - urlTool = tool - } +func SetURLComparator(comparator URLComparator) { + urlComparator = comparator } -func GetURLTool() URLTool { - return urlTool +func GetURLComparator() URLComparator { + return urlComparator } -// Config default urlTools. +// Config default defaultURLComparator. func init() { - SetURLTool(defaultURLTool{}) -} - -type defaultURLTool struct { + SetURLComparator(defaultURLComparator{}) } -// default priority is 16. -func (defaultURLTool) Priority() uint8 { - //default is 16. - return 16 +type defaultURLComparator struct { } // default comparison implements -func (defaultURLTool) CompareURLEqual(l *URL, r *URL, execludeParam ...string) bool { - return IsEquals(*l, *r, execludeParam...) +func (defaultURLComparator) CompareURLEqual(l *URL, r *URL, excludeParam ...string) bool { + return IsEquals(*l, *r, excludeParam...) } diff --git a/common/url_tool_extension_test.go b/common/url_tool_extension_test.go index 8254c18aaa5c4eba110280db79f13b9e7c707075..6a5a3cf9b2b5f970c46b24fcfe7ee6213b59d6f4 100644 --- a/common/url_tool_extension_test.go +++ b/common/url_tool_extension_test.go @@ -34,8 +34,8 @@ func TestDefaultURLTool(t *testing.T) { "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×tamp=155650979798") - assert.False(t, GetURLTool().CompareURLEqual(&url1, &url2)) - assert.True(t, GetURLTool().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) + assert.False(t, GetURLComparator().CompareURLEqual(&url1, &url2)) + assert.True(t, GetURLComparator().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) } func TestNewCustomURLTool(t *testing.T) { @@ -49,10 +49,10 @@ func TestNewCustomURLTool(t *testing.T) { "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×tamp=155650979798") - assert.True(t, GetURLTool().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) - SetURLTool(customURLTool{}) - assert.False(t, GetURLTool().CompareURLEqual(&url1, &url2)) - assert.False(t, GetURLTool().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) + assert.True(t, GetURLComparator().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) + SetURLComparator(customURLTool{}) + assert.False(t, GetURLComparator().CompareURLEqual(&url1, &url2)) + assert.False(t, GetURLComparator().CompareURLEqual(&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&" + @@ -64,11 +64,11 @@ func TestNewCustomURLTool(t *testing.T) { "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, GetURLTool().CompareURLEqual(&url1, &url2)) - assert.True(t, GetURLTool().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) - SetURLTool(customURLTool{}) - assert.True(t, GetURLTool().CompareURLEqual(&url1, &url2)) - assert.True(t, GetURLTool().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) + assert.True(t, GetURLComparator().CompareURLEqual(&url1, &url2)) + assert.True(t, GetURLComparator().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) + SetURLComparator(customURLTool{}) + assert.True(t, GetURLComparator().CompareURLEqual(&url1, &url2)) + assert.True(t, GetURLComparator().CompareURLEqual(&url1, &url2, constant.TIMESTAMP_KEY, constant.REMOTE_TIMESTAMP_KEY)) } // just for no timestamp, it depend on write data. diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 2d75fe71d209f2f42319b6513a984e9ec5fd94fa..aaf699bdef621065dfe3e7bdce8386445bbbec83 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -304,7 +304,7 @@ func (dir *RegistryDirectory) cacheInvoker(url *common.URL) protocol.Invoker { // 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.GetURLTool().CompareURLEqual(newUrl, &urlTmp) { + if common.GetURLComparator().CompareURLEqual(newUrl, &urlTmp) { return nil }