Skip to content

Commit

Permalink
Merge pull request apache#198 from hxmhlt/add_mergeUrl_retries_param
Browse files Browse the repository at this point in the history
Add:retries mergeUrl
  • Loading branch information
hxmhlt authored Sep 11, 2019
2 parents 69bc3df + 8f9295b commit cf198d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
40 changes: 19 additions & 21 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,36 +419,19 @@ func (c URL) ToMap() map[string]string {
//TODO configuration merge, in the future , the configuration center's config should merge too.
func MergeUrl(serviceUrl URL, referenceUrl *URL) URL {
mergedUrl := serviceUrl
var methodConfigMergeFcn = []func(method string){}

//iterator the referenceUrl if serviceUrl not have the key ,merge in

for k, v := range referenceUrl.Params {
if _, ok := mergedUrl.Params[k]; !ok {
mergedUrl.Params.Set(k, v[0])
}
}
//loadBalance strategy config
if v := referenceUrl.Params.Get(constant.LOADBALANCE_KEY); v != "" {
mergedUrl.Params.Set(constant.LOADBALANCE_KEY, v)
}
methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) {
if v := referenceUrl.Params.Get(method + "." + constant.LOADBALANCE_KEY); v != "" {
mergedUrl.Params.Set(method+"."+constant.LOADBALANCE_KEY, v)
}
})

//cluster strategy config
if v := referenceUrl.Params.Get(constant.CLUSTER_KEY); v != "" {
mergedUrl.Params.Set(constant.CLUSTER_KEY, v)
}
methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) {
if v := referenceUrl.Params.Get(method + "." + constant.CLUSTER_KEY); v != "" {
mergedUrl.Params.Set(method+"."+constant.CLUSTER_KEY, v)
}
})
//loadBalance,cluster,retries strategy config
methodConfigMergeFcn := mergeNormalParam(mergedUrl, referenceUrl, []string{constant.LOADBALANCE_KEY, constant.CLUSTER_KEY, constant.RETRIES_KEY})

//remote timestamp
if v := serviceUrl.Params.Get(constant.TIMESTAMP_KEY); v != "" {
if v := serviceUrl.Params.Get(constant.TIMESTAMP_KEY); len(v) > 0 {
mergedUrl.Params.Set(constant.REMOTE_TIMESTAMP_KEY, v)
mergedUrl.Params.Set(constant.TIMESTAMP_KEY, referenceUrl.Params.Get(constant.TIMESTAMP_KEY))
}
Expand All @@ -462,3 +445,18 @@ func MergeUrl(serviceUrl URL, referenceUrl *URL) URL {

return mergedUrl
}

func mergeNormalParam(mergedUrl URL, referenceUrl *URL, paramKeys []string) []func(method string) {
var methodConfigMergeFcn = []func(method string){}
for _, paramKey := range paramKeys {
if v := referenceUrl.Params.Get(paramKey); len(v) > 0 {
mergedUrl.Params.Set(paramKey, v)
}
methodConfigMergeFcn = append(methodConfigMergeFcn, func(method string) {
if v := referenceUrl.Params.Get(method + "." + paramKey); len(v) > 0 {
mergedUrl.Params.Set(method+"."+paramKey, v)
}
})
}
return methodConfigMergeFcn
}
8 changes: 7 additions & 1 deletion common/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,21 @@ func TestURL_GetMethodParam(t *testing.T) {
func TestMergeUrl(t *testing.T) {
referenceUrlParams := url.Values{}
referenceUrlParams.Set(constant.CLUSTER_KEY, "random")
referenceUrlParams.Set(constant.RETRIES_KEY, "1")
referenceUrlParams.Set("test3", "1")
referenceUrlParams.Set("methods.testMethod."+constant.RETRIES_KEY, "1")
serviceUrlParams := url.Values{}
serviceUrlParams.Set("test2", "1")
serviceUrlParams.Set(constant.CLUSTER_KEY, "roundrobin")
referenceUrl, _ := NewURL(context.TODO(), "mock1://127.0.0.1:1111", WithParams(referenceUrlParams))
serviceUrlParams.Set(constant.RETRIES_KEY, "2")
serviceUrlParams.Set("methods.testMethod."+constant.RETRIES_KEY, "2")
referenceUrl, _ := NewURL(context.TODO(), "mock1://127.0.0.1:1111", WithParams(referenceUrlParams), WithMethods([]string{"testMethod"}))
serviceUrl, _ := NewURL(context.TODO(), "mock2://127.0.0.1:20000", WithParams(serviceUrlParams))

mergedUrl := MergeUrl(serviceUrl, &referenceUrl)
assert.Equal(t, "random", mergedUrl.GetParam(constant.CLUSTER_KEY, ""))
assert.Equal(t, "1", mergedUrl.GetParam("test2", ""))
assert.Equal(t, "1", mergedUrl.GetParam("test3", ""))
assert.Equal(t, "1", mergedUrl.GetParam(constant.RETRIES_KEY, ""))
assert.Equal(t, "1", mergedUrl.GetParam("methods.testMethod."+constant.RETRIES_KEY, ""))
}

0 comments on commit cf198d0

Please sign in to comment.