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/09 06:59:31 UTC

[GitHub] [dubbo-go] zouyx commented on a change in pull request #703: Ftr: add dynamic tag router

zouyx commented on a change in pull request #703:
URL: https://github.com/apache/dubbo-go/pull/703#discussion_r467545829



##########
File path: cluster/router/tag/tag_router.go
##########
@@ -55,15 +64,118 @@ func (c *tagRouter) isEnabled() bool {
 	return c.enabled
 }
 
+func (c *tagRouter) tagRouterRuleCopy() RouterRule {
+	routerRule := *c.tagRouterRule
+	return routerRule
+}
+
 // Route gets a list of invoker
 func (c *tagRouter) Route(invokers []protocol.Invoker, url *common.URL, invocation protocol.Invocation) []protocol.Invoker {
-	if !c.isEnabled() {
+	var (
+		result    []protocol.Invoker
+		addresses []string
+	)
+
+	if !c.isEnabled() || len(invokers) == 0 {
 		return invokers
 	}
+
+	// Use static tags if dynamic tags are not set or invalid
+	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, "")
+	}
+
+	// if we are requesting for a Provider with a specific tag
+	if len(tag) > 0 {
+		return filterInvokersWithTag(invokers, url, invocation, tagRouterRuleCopy, tag)
+	}
+
+	// return all addresses in dynamic tag group.
+	addresses = tagRouterRuleCopy.getAddresses()
+	if len(addresses) > 0 {
+		filterAddressNotMatches := func(invoker protocol.Invoker) bool {
+			url := invoker.GetUrl()
+			return len(addresses) == 0 || !checkAddressMatch(addresses, url.Ip, url.Port)
+		}
+		result = filterInvoker(invokers, filterAddressNotMatches)
+		// 1. all addresses are in dynamic tag group, return empty list.
+		if len(result) == 0 {
+			return result
+		}
+	}
+	// 2. if there are some addresses that are not in any dynamic tag group, continue to filter using the
+	// static tag group.
+	filter := func(invoker protocol.Invoker) bool {
+		localTag := invoker.GetUrl().GetParam(constant.Tagkey, "")
+		return localTag == "" || !(tagRouterRuleCopy.hasTag(localTag))

Review comment:
       ```suggestion
   		return len(localTag)==0|| !(tagRouterRuleCopy.hasTag(localTag))
   ```

##########
File path: cluster/router/router.go
##########
@@ -50,3 +50,14 @@ type PriorityRouter interface {
 	// 0 to ^int(0) is better
 	Priority() int64
 }
+
+// NotifyRouter notify router use the invoker list. Invoker list may change from time to time. This method gives the router a
+// chance to prepare before {@link Router#route(List, URL, Invocation)} gets called.
+type NotifyRouter interface {
+	router

Review comment:
       ```suggestion
   	PriorityRouter
   ```
   if `NotifyRouter ` is a `PriorityRouter` , you just extends `PriorityRouter` not router.




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