-
Notifications
You must be signed in to change notification settings - Fork 930
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
Changes from 3 commits
62bdc06
83d0578
f4ce65e
6ab25e4
0d7baa5
2f432d5
93932e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
var urlComparator URLComparator | ||
|
||
// Define some func for URL, such as comparison of URL. | ||
// Your can define your own implements, and invoke SetURLComparator. | ||
type URLComparator interface { | ||
CompareURLEqual(*URL, *URL, ...string) bool | ||
} | ||
|
||
func SetURLComparator(comparator URLComparator) { | ||
urlComparator = comparator | ||
} | ||
func GetURLComparator() URLComparator { | ||
return urlComparator | ||
} | ||
|
||
// Config default defaultURLComparator. | ||
func init() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be moved after the package declaration There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the file. Just use func instead of Interface. |
||
SetURLComparator(defaultURLComparator{}) | ||
} | ||
|
||
type defaultURLComparator struct { | ||
} | ||
|
||
// default comparison implements | ||
func (defaultURLComparator) CompareURLEqual(l *URL, r *URL, excludeParam ...string) bool { | ||
return IsEquals(*l, *r, excludeParam...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check But if both of them are nil, I am not sure whether we should return true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @flycash 我们需要一个更系统性的 URL 改进方案,必须加锁确保安全,然后至少添加一个 Copy 函数,目前的实现很容易被人误用。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我的理解是,现在URL只有我们这种开发维护人员才会直接使用到。误用是指哪方面? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add some check in IsEquals func. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package common | ||
|
||
import ( | ||
"github.com/apache/dubbo-go/common/constant" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split pkg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the file. |
||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestDefaultURLTool(t *testing.T) { | ||
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×tamp=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×tamp=155650979798") | ||
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) { | ||
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×tamp=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×tamp=155650979798") | ||
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&" + | ||
"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, 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. | ||
type customURLTool struct { | ||
} | ||
|
||
func (customURLTool) Priority() uint8 { | ||
//default is 16. | ||
return 32 | ||
} | ||
func (customURLTool) CompareURLEqual(l *URL, r *URL, execludeParam ...string) bool { | ||
return l.PrimitiveURL == r.PrimitiveURL | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be a simple function is better?
like this
var CompareURLEqualFunc(*URL, *URL, ...string) bool;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea !
Done.