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/02 09:37:53 UTC
[GitHub] [dubbo-go-pixiu] baerwang commented on a diff in pull request #501: OSPP: Traffic Distribution
baerwang commented on code in PR #501:
URL: https://github.com/apache/dubbo-go-pixiu/pull/501#discussion_r985209996
##########
pixiu/pkg/filter/traffic/traffic.go:
##########
@@ -79,41 +91,99 @@ func (factory *FilterFactory) Config() interface{} {
}
func (factory *FilterFactory) Apply() error {
- initActions()
return nil
}
func (factory *FilterFactory) PrepareFilterChain(ctx *http.HttpContext, chain filter.FilterChain) error {
- f := &Filter{}
- f.Rules = factory.rulesMatch(ctx.Request.RequestURI)
+ f := &Filter{
+ weight: unInitialize,
+ record: map[string]struct{}{},
+ }
+ f.Rules = factory.rulesMatch(f, ctx.Request.RequestURI)
chain.AppendDecodeFilters(f)
return nil
}
func (f *Filter) Decode(ctx *http.HttpContext) filter.FilterStatus {
+ cluster := ""
if f.Rules != nil {
- cluster := spilt(ctx.Request, f.Rules)
- if cluster != nil {
- ctx.Route.Cluster = cluster.Name
- logger.Debugf("[dubbo-go-pixiu] execute traffic split to cluster %s", cluster.Name)
+ for _, wp := range f.Rules {
+ 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 (factory *FilterFactory) rulesMatch(path string) map[CanaryHeaders]*Cluster {
+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
+ }
+
+ return spiltWeight(f.weight, c.weightFloor, c.weightCeil)
+}
+
+func (factory *FilterFactory) rulesMatch(f *Filter, path string) []*ClusterWrapper {
clusters := factory.cfg.Traffics
if clusters != nil {
- mp := make(map[CanaryHeaders]*Cluster)
- for i := range clusters {
- if strings.HasPrefix(clusters[i].Router, path) {
- mp[clusters[i].Model] = clusters[i]
+ rules := make([]*ClusterWrapper, 0)
+ up := 0
+ for _, cluster := range clusters {
+ if strings.HasPrefix(path, cluster.Router) {
+ wp := &ClusterWrapper{
+ Cluster: cluster,
+ header: "",
+ weightCeil: -1,
+ weightFloor: -1,
+ }
+ if cluster.CanaryByHeader != "" {
+ if _, ok := f.record[cluster.CanaryByHeader]; ok {
+ logger.Errorf("Duplicate canary-by-header values")
+ } else {
+ f.record[cluster.CanaryByHeader] = struct{}{}
+ wp.header = cluster.CanaryByHeader
+ }
+ }
+ if cluster.CanaryWeight != "" {
+ val, err := strconv.Atoi(cluster.CanaryWeight)
+ if err != nil || val < 0 {
+ logger.Errorf(fmt.Sprintf("Wrong canary-weight value: %v", cluster.CanaryWeight))
+ }
+ wp.weightFloor = up
+ up += val
+ if up > 100 {
+ logger.Errorf("[dubbo-go-pixiu] clusters' weight sum more than 100 in %v service!", cluster.Router)
Review Comment:
是不是应该continue,跳过
--
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