You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "justxuewei (via GitHub)" <gi...@apache.org> on 2023/08/13 03:48:03 UTC
[GitHub] [dubbo-go] justxuewei commented on a diff in pull request #2330: Improve tag and configurator rules to keep up with official site
justxuewei commented on code in PR #2330:
URL: https://github.com/apache/dubbo-go/pull/2330#discussion_r1292613015
##########
common/match.go:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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 (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ "fmt"
+ "net"
+ "regexp"
+ "strings"
+)
+
+type ParamMatch struct {
+ Key string `yaml:"key" json:"key,omitempty" property:"key"`
+ Value StringMatch `yaml:"value" json:"value,omitempty" property:"value"`
+}
+
+func (p ParamMatch) IsMatch(url *URL) bool {
+ return p.Value.IsMatch(url.GetParam(p.Key, ""))
+}
Review Comment:
```suggestion
func (p *ParamMatch) IsMatch(url *URL) bool {
return p.Value.IsMatch(url.GetParam(p.Key, ""))
}
```
##########
cluster/router/tag/match.go:
##########
@@ -99,22 +99,39 @@ func requestTag(invokers []protocol.Invoker, url *common.URL, invocation protoco
var (
addresses []string
result []protocol.Invoker
+ match []common.ParamMatch
)
for _, tagCfg := range cfg.Tags {
if tagCfg.Name == tag {
addresses = tagCfg.Addresses
+ match = tagCfg.Match
}
}
- if len(addresses) == 0 {
- // filter tag does not match
- result = filterInvokers(invokers, tag, func(invoker protocol.Invoker, tag interface{}) bool {
- return invoker.GetURL().GetParam(constant.Tagkey, "") != tag
+
+ // only one of 'match' and 'addresses' will take effect if both are specified.
+ if len(match) != 0 {
+ result = filterInvokers(invokers, match, func(invoker protocol.Invoker, match interface{}) bool {
+ matches := match.([]common.ParamMatch)
+ isMatch := true
+ for _, m := range matches {
+ if !m.IsMatch(invoker.GetURL()) {
+ isMatch = false
+ }
+ }
+ return !isMatch
Review Comment:
`isMatch` is an unnecessary variable here:
```suggestion
matches := match.([]common.ParamMatch)
for _, m := range matches {
if !m.IsMatch(invoker.GetURL()) {
return true
}
}
return false
```
##########
common/match.go:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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 (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ "fmt"
+ "net"
+ "regexp"
+ "strings"
+)
+
+type ParamMatch struct {
+ Key string `yaml:"key" json:"key,omitempty" property:"key"`
+ Value StringMatch `yaml:"value" json:"value,omitempty" property:"value"`
+}
+
+func (p ParamMatch) IsMatch(url *URL) bool {
+ return p.Value.IsMatch(url.GetParam(p.Key, ""))
+}
+
+type StringMatch struct {
+ Exact string `yaml:"exact" json:"exact,omitempty" property:"exact"`
+ Prefix string `yaml:"prefix" json:"prefix,omitempty" property:"prefix"`
+ Regex string `yaml:"regex" json:"regex,omitempty" property:"regex"`
+ Noempty string `yaml:"noempty" json:"noempty,omitempty" property:"noempty"`
+ Empty string `yaml:"empty" json:"empty,omitempty" property:"empty"`
+ Wildcard string `yaml:"wildcard" json:"wildcard,omitempty" property:"wildcard"`
+}
+
+func (p StringMatch) IsMatch(value string) bool {
Review Comment:
Ditto.
##########
common/constant/cluster.go:
##########
@@ -32,3 +32,8 @@ const (
const (
NonImportErrorMsgFormat = "Cluster for %s is not existing, make sure you have import the package."
)
+
+const (
+ MatchCondition = "MATCH_CONDITION"
+ ApiVersion = "v3.0"
Review Comment:
```suggestion
APIVersion = "v3.0"
```
It would be better if it is named `Dubbo3APIVersion`, since the Dubbo version might be upgraded to v4.0 in the future.
##########
config_center/parser/configuration_parser.go:
##########
@@ -145,13 +177,15 @@ func serviceItemToUrls(item ConfigItem, config ConfiguratorConfig) ([]*common.UR
if err != nil {
return nil, perrors.WithStack(err)
}
+ url.AddAttribute(constant.MatchCondition, item.Match)
urls = append(urls, url)
}
} else {
url, err := common.NewURL(urlStr)
if err != nil {
return nil, perrors.WithStack(err)
}
+ url.AddAttribute(constant.MatchCondition, item.Match)
Review Comment:
Ditto.
##########
config_center/configurator/override.go:
##########
@@ -57,34 +58,63 @@ func (c *overrideConfigurator) Configure(url *common.URL) {
// branch for version 2.7.x
apiVersion := c.configuratorUrl.GetParam(constant.ConfigVersionKey, "")
if len(apiVersion) != 0 {
+ var host string
currentSide := url.GetParam(constant.SideKey, "")
configuratorSide := c.configuratorUrl.GetParam(constant.SideKey, "")
- if currentSide == configuratorSide && common.DubboRole[common.CONSUMER] == currentSide && c.configuratorUrl.Port == "0" {
- localIP := common.GetLocalIp()
- c.configureIfMatch(localIP, url)
- } else if currentSide == configuratorSide && common.DubboRole[common.PROVIDER] == currentSide && c.configuratorUrl.Port == url.Port {
- c.configureIfMatch(url.Ip, url)
+ if currentSide == configuratorSide && common.DubboRole[common.CONSUMER] == currentSide {
+ host = common.GetLocalIp()
+ } else if currentSide == configuratorSide && common.DubboRole[common.PROVIDER] == currentSide {
+ host = url.Ip
+ }
+
+ if strings.HasPrefix(apiVersion, constant.ApiVersion) {
+ c.configureIfMatchV3(host, url)
+ } else {
+ c.configureIfMatch(host, url)
}
} else {
// branch for version 2.6.x and less
c.configureDeprecated(url)
}
}
+// configureIfMatch
+func (c *overrideConfigurator) configureIfMatchV3(host string, url *common.URL) {
+ conditionKeys := getConditionKeys()
+ matcher := c.configuratorUrl.GetAttribute(constant.MatchCondition)
+ if matcher != nil {
+ conditionMatcher := matcher.(parser.ConditionMatch)
Review Comment:
```suggestion
// Use a pointer to prevent extra copying
conditionMatcher := matcher.(*parser.ConditionMatch)
```
##########
config_center/parser/configuration_parser.go:
##########
@@ -145,13 +177,15 @@ func serviceItemToUrls(item ConfigItem, config ConfiguratorConfig) ([]*common.UR
if err != nil {
return nil, perrors.WithStack(err)
}
+ url.AddAttribute(constant.MatchCondition, item.Match)
Review Comment:
```suggestion
url.AddAttribute(constant.MatchCondition, &item.Match)
```
##########
common/match.go:
##########
@@ -0,0 +1,102 @@
+/*
+ * 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 (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ "fmt"
+ "net"
+ "regexp"
+ "strings"
+)
+
+type ParamMatch struct {
+ Key string `yaml:"key" json:"key,omitempty" property:"key"`
+ Value StringMatch `yaml:"value" json:"value,omitempty" property:"value"`
+}
+
+func (p ParamMatch) IsMatch(url *URL) bool {
+ return p.Value.IsMatch(url.GetParam(p.Key, ""))
+}
+
+type StringMatch struct {
+ Exact string `yaml:"exact" json:"exact,omitempty" property:"exact"`
+ Prefix string `yaml:"prefix" json:"prefix,omitempty" property:"prefix"`
+ Regex string `yaml:"regex" json:"regex,omitempty" property:"regex"`
+ Noempty string `yaml:"noempty" json:"noempty,omitempty" property:"noempty"`
+ Empty string `yaml:"empty" json:"empty,omitempty" property:"empty"`
+ Wildcard string `yaml:"wildcard" json:"wildcard,omitempty" property:"wildcard"`
+}
+
+func (p StringMatch) IsMatch(value string) bool {
+ if p.Exact != "" {
+ return p.Exact == value
+ } else if p.Prefix != "" {
+ return strings.HasPrefix(value, p.Prefix)
+ } else if p.Regex != "" {
+ match, _ := regexp.MatchString(p.Regex, value)
+ return match
+ } else if p.Wildcard != "" {
+ return value == p.Wildcard || constant.AnyValue == p.Wildcard
+ } else if p.Empty != "" {
+ return value == ""
+ } else if p.Noempty != "" {
+ return value != ""
+ }
+ return false
+}
+
+type AddressMatch struct {
+ Wildcard string `yaml:"wildcard" json:"wildcard,omitempty" property:"wildcard"`
+ Cird string `yaml:"cird" json:"cird,omitempty" property:"cird"`
+ Exact string `yaml:"exact" json:"exact,omitempty" property:"exact"`
+}
+
+func (p AddressMatch) IsMatch(value string) bool {
+ if p.Cird != "" && value != "" {
+ _, ipnet, err := net.ParseCIDR(p.Cird)
+ if err != nil {
+ fmt.Println("Error", p.Cird, err)
+ return false
+ }
+ return ipnet.Contains(net.ParseIP(value))
+ }
+ if p.Wildcard != "" && value != "" {
+ if constant.AnyValue == value || constant.AnyHostValue == value {
+ return true
+ }
+ return IsMatchGlobPattern(p.Wildcard, value)
+ }
+ if p.Exact != "" && value != "" {
+ return p.Exact == value
+ }
+ return false
+}
+
+type ListStringMatch struct {
+ Oneof []StringMatch `yaml:"oneof" json:"oneof,omitempty" property:"oneof"`
+}
+
+func (p ListStringMatch) IsMatch(value string) bool {
Review Comment:
Ditto.
##########
config_center/parser/configuration_parser.go:
##########
@@ -193,6 +227,7 @@ func appItemToUrls(item ConfigItem, config ConfiguratorConfig) ([]*common.URL, e
if err != nil {
return nil, perrors.WithStack(err)
}
+ url.AddAttribute(constant.MatchCondition, item.Match)
Review Comment:
Ditto.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org