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/11 00:53:33 UTC

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

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


##########
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:
   看下这个pr: https://github.com/apache/dubbo-go-pixiu-samples/pull/7



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