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/07/30 07:35:08 UTC

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

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



##########
File path: cluster/router/tag/router_rule.go
##########
@@ -52,18 +52,44 @@ func getRule(rawRule string) (*RouterRule, error) {
 		return r, err
 	}
 	r.RawRule = rawRule
-	// TODO init tags
+	r.init()
 	return r, nil
 }
 
+func (t *RouterRule) init() {
+	t.addressToTagNames = make(map[string][]string)
+	t.tagNameToAddresses = make(map[string][]string)

Review comment:
       	t.addressToTagNames = make(map[string][]string, 8)
   	t.tagNameToAddresses = make(map[string][]string, 8)

##########
File path: cluster/router/tag/router_rule.go
##########
@@ -52,18 +52,44 @@ func getRule(rawRule string) (*RouterRule, error) {
 		return r, err
 	}
 	r.RawRule = rawRule
-	// TODO init tags
+	r.init()
 	return r, nil
 }
 
+func (t *RouterRule) init() {
+	t.addressToTagNames = make(map[string][]string)
+	t.tagNameToAddresses = make(map[string][]string)
+	for _, tag := range t.Tags {
+		for _, address := range tag.Addresses {
+			t.addressToTagNames[address] = append(t.addressToTagNames[address], tag.Name)
+		}
+		t.tagNameToAddresses[tag.Name] = tag.Addresses
+	}
+}
+
 func (t *RouterRule) getAddresses() []string {
-	// TODO get all tag addresses
-	return nil
+	var result []string

Review comment:
       var result = make([]string, 0, 8 * len(t.Tags))

##########
File path: cluster/router/tag/tag_router.go
##########
@@ -172,18 +185,51 @@ func isForceUseTag(url *common.URL, invocation protocol.Invocation) bool {
 	return false
 }
 
-func addressMatches(url *common.URL, addresses []string) bool {
-	return len(addresses) > 0 && checkAddressMatch(addresses, url.Ip, url.Port)
+func filterAddressMatches(invokers []protocol.Invoker, addresses []string) []protocol.Invoker {
+	var idx int
+	for _, invoker := range invokers {
+		url := invoker.GetUrl()
+		if !(len(addresses) > 0 && checkAddressMatch(addresses, url.Ip, url.Port)) {
+			continue
+		}
+		invokers[idx] = invoker
+		idx++
+	}
+	return invokers[:idx]
+}
+
+func filterAddressNotMatches(invokers []protocol.Invoker, addresses []string) []protocol.Invoker {
+	var idx int
+	for _, invoker := range invokers {
+		url := invoker.GetUrl()
+		if !(len(addresses) == 0 || !checkAddressMatch(addresses, url.Ip, url.Port)) {
+			continue
+		}
+		invokers[idx] = invoker
+		idx++
+	}
+	return invokers[:idx]
 }
 
-func addressNotMatches(url *common.URL, addresses []string) bool {
-	return len(addresses) == 0 || !checkAddressMatch(addresses, url.Ip, url.Port)
+func filterCondition(invokers []protocol.Invoker, condition func(protocol.Invoker) bool) []protocol.Invoker {
+	var idx int
+	for _, invoker := range invokers {
+		if !condition(invoker) {
+			continue
+		}
+		invokers[idx] = invoker
+		idx++
+	}
+	return invokers[:idx]
 }
 
 func checkAddressMatch(addresses []string, host, port string) bool {
 	for _, address := range addresses {
-		// TODO address parse
-		if address == (host + port) {
+		// TODO ip match
+		if address == host+":"+port {

Review comment:
       delete "host+":"+port", using net.JoinHostPort(host, port) instead.

##########
File path: cluster/router/tag/router_rule.go
##########
@@ -52,18 +52,44 @@ func getRule(rawRule string) (*RouterRule, error) {
 		return r, err
 	}
 	r.RawRule = rawRule
-	// TODO init tags
+	r.init()
 	return r, nil
 }
 
+func (t *RouterRule) init() {
+	t.addressToTagNames = make(map[string][]string)
+	t.tagNameToAddresses = make(map[string][]string)
+	for _, tag := range t.Tags {
+		for _, address := range tag.Addresses {
+			t.addressToTagNames[address] = append(t.addressToTagNames[address], tag.Name)
+		}
+		t.tagNameToAddresses[tag.Name] = tag.Addresses
+	}
+}
+
 func (t *RouterRule) getAddresses() []string {
-	// TODO get all tag addresses
-	return nil
+	var result []string
+	for _, tag := range t.Tags {
+		result = append(result, tag.Addresses...)
+	}
+	return result
 }
 
 func (t *RouterRule) getTagNames() []string {
-	// TODO get all tag names
-	return nil
+	var result []string

Review comment:
       var result = make([]string, 0, len(t.Tags))




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