You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2020/08/05 03:46:31 UTC

[GitHub] [dubbo-go] pantianying commented on a change in pull request #665: Ftr: dynamic tag router

pantianying commented on a change in pull request #665:
URL: https://github.com/apache/dubbo-go/pull/665#discussion_r465451983



##########
File path: cluster/router/tag/tag_router.go
##########
@@ -63,7 +81,152 @@ func (c *tagRouter) Route(invokers []protocol.Invoker, url *common.URL, invocati
 	if len(invokers) == 0 {
 		return invokers
 	}
-	return filterUsingStaticTag(invokers, url, invocation)
+	if c.tagRouterRule == nil || !c.tagRouterRule.Valid || !c.tagRouterRule.Enabled {
+		return filterUsingStaticTag(invokers, url, invocation)
+	}
+	// since the rule can be changed by config center, we should copy one to use.
+	tagRouterRuleCopy := c.tagRouterRuleCopy()
+	tag, ok := invocation.Attachments()[constant.Tagkey]
+	if !ok {
+		tag = url.GetParam(constant.Tagkey, "")
+	}
+	var (
+		result    []protocol.Invoker
+		addresses []string
+	)

Review comment:
       The definition can be moved to the top.
   And can you break this pile of code into several parts,it is hard for me πŸ˜‚

##########
File path: cluster/router/tag/tag_router.go
##########
@@ -100,3 +263,163 @@ func isForceUseTag(url *common.URL, invocation protocol.Invocation) bool {
 	}
 	return false
 }
+
+type filter func(protocol.Invoker) bool
+
+func filterInvoker(invokers []protocol.Invoker, filters ...filter) []protocol.Invoker {
+	var res []protocol.Invoker
+OUTER:
+	for _, invoker := range invokers {
+		for _, filter := range filters {
+			if !filter(invoker) {
+				continue OUTER
+			}
+		}
+		res = append(res, invoker)
+	}
+	return res
+}
+
+// TODO: need move to dubbogo/gost
+func checkAddressMatch(addresses []string, host, port string) bool {
+	for _, address := range addresses {
+		if matchIp(address, host, port) {
+			return true
+		}
+		if address == net.JoinHostPort(constant.ANYHOST_VALUE, port) {
+			return true
+		}
+	}
+	return false
+}
+
+func matchIp(pattern, host, port string) bool {
+	// if the pattern is subnet format, it will not be allowed to config port param in pattern.
+	if strings.Contains(pattern, "/") {
+		_, subnet, _ := net.ParseCIDR(pattern)
+		if subnet != nil && subnet.Contains(net.ParseIP(host)) {
+			return true
+		}
+		return false
+	}
+	return matchIpRange(pattern, host, port)
+}
+
+func matchIpRange(pattern, host, port string) bool {
+	if pattern == "" || host == "" {
+		logger.Error("Illegal Argument pattern or hostName. Pattern:" + pattern + ", Host:" + host)
+		return false
+	}
+
+	pattern = strings.TrimSpace(pattern)
+	if "*.*.*.*" == pattern || "*" == pattern {
+		return true
+	}
+
+	isIpv4 := true
+	ip4 := net.ParseIP(host).To4()
+
+	if ip4 == nil {
+		isIpv4 = false
+	}
+
+	hostAndPort := getPatternHostAndPort(pattern, isIpv4)
+	if hostAndPort[1] != "" && hostAndPort[1] != port {
+		return false
+	}
+
+	pattern = hostAndPort[0]
+	// TODO εΈΈι‡εŒ–

Review comment:
       has some easy todo

##########
File path: cluster/router/tag/tag_router_test.go
##########
@@ -19,32 +19,59 @@ package tag
 
 import (
 	"context"
+	"fmt"
+	"github.com/stretchr/testify/suite"

Review comment:
       pkg import problem

##########
File path: cluster/router/tag/tag_router_test.go
##########
@@ -19,32 +19,59 @@ package tag
 
 import (
 	"context"
+	"fmt"
+	"github.com/stretchr/testify/suite"

Review comment:
       pkg import problem




----------------------------------------------------------------
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.

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