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 2022/10/10 12:08:50 UTC

[GitHub] [dubbo-go-pixiu] PhilYue commented on a diff in pull request #501: OSPP: Traffic Distribution

PhilYue commented on code in PR #501:
URL: https://github.com/apache/dubbo-go-pixiu/pull/501#discussion_r991213926


##########
pixiu/pkg/filter/traffic/traffic.go:
##########
@@ -105,33 +103,45 @@ func (factory *FilterFactory) PrepareFilterChain(ctx *http.HttpContext, chain fi
 }
 
 func (f *Filter) Decode(ctx *http.HttpContext) filter.FilterStatus {
+	cluster := ""
 	if f.Rules != nil {
 		for _, wp := range f.Rules {
-			if f.traffic(wp, ctx) {
-				ctx.Route.Cluster = wp.Cluster.Name
+			if f.trafficHeader(wp, ctx) {
+				cluster = wp.Cluster.Name
 				logger.Debugf("[dubbo-go-pixiu] execute traffic split to cluster %s", wp.Cluster.Name)
 				break
 			}
 		}
+		if cluster == "" {
+			for _, wp := range f.Rules {
+				if f.trafficWeight(wp, ctx) {
+					ctx.Route.Cluster = wp.Cluster.Name
+					cluster = wp.Cluster.Name
+					logger.Debugf("[dubbo-go-pixiu] execute traffic split to cluster %s", wp.Cluster.Name)
+					break
+				}
+			}
+		}
+		if cluster != "" {
+			ctx.Route.Cluster = cluster
+		}
 	} else {
 		logger.Warnf("[dubbo-go-pixiu] execute traffic split fail because of empty rules.")
 	}
 	return filter.Continue
 }
 
-func (f *Filter) traffic(c *ClusterWrapper, ctx *http.HttpContext) bool {
+func (f *Filter) trafficHeader(c *ClusterWrapper, ctx *http.HttpContext) bool {
+	return spiltHeader(ctx.Request, c.header)
+}
+
+func (f *Filter) trafficWeight(c *ClusterWrapper, ctx *http.HttpContext) bool {
 	if f.weight == unInitialize {
 		rand.Seed(time.Now().UnixNano())
 		f.weight = rand.Intn(100) + 1
 	}
 
-	res := false
-	if c.header != "" {
-		res = spiltHeader(ctx.Request, c.header)
-	} else if !res && c.weightFloor != -1 && c.weightCeil != -1 {
-		res = spiltWeight(f.weight, c.weightFloor, c.weightCeil)
-	}
-	return res
+	return spiltWeight(f.weight, c.weightFloor, c.weightCeil)
 }
 
 func (factory *FilterFactory) rulesMatch(f *Filter, path string) []*ClusterWrapper {

Review Comment:
   想了解一下这个`path` 和 `cluster.Router` 是如何匹配上的,有没有具体的例子可以描述一下 ,比如 path="/xxx/xx", cluster.Router = "/xxx"



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