diff --git a/plugins/wasm-go/extensions/traffic-tag/content.go b/plugins/wasm-go/extensions/traffic-tag/content.go index 07750f3314..4d62c02c99 100644 --- a/plugins/wasm-go/extensions/traffic-tag/content.go +++ b/plugins/wasm-go/extensions/traffic-tag/content.go @@ -26,10 +26,6 @@ import ( ) func onContentRequestHeaders(conditionGroups []ConditionGroup, log wrapper.Log) bool { - if len(conditionGroups) == 0 { - return false - } - for _, cg := range conditionGroups { if matchCondition(&cg, log) { addTagHeader(cg.HeaderName, cg.HeaderValue, log) diff --git a/plugins/wasm-go/extensions/traffic-tag/main.go b/plugins/wasm-go/extensions/traffic-tag/main.go index 6f69f23d64..0e58f95084 100644 --- a/plugins/wasm-go/extensions/traffic-tag/main.go +++ b/plugins/wasm-go/extensions/traffic-tag/main.go @@ -16,6 +16,7 @@ package main import ( "math/rand" + "strings" "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" "github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types" @@ -60,7 +61,6 @@ type TrafficTagConfig struct { WeightGroups []WeightGroup `json:"weightGroups,omitempty"` DefaultTagKey string `json:"defaultTagKey,omitempty"` DefaultTagVal string `json:"defaultTagVal,omitempty"` - randGen *rand.Rand } type ConditionGroup struct { @@ -93,8 +93,11 @@ func main() { } func parseConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.Log) error { - if err := jsonValidate(json, log); err != nil { - return err + + jsonStr := strings.TrimSpace(json.Raw) + if jsonStr == "{}" || jsonStr == "" { + log.Error("plugin config is empty") + return nil } err := parseContentConfig(json, config, log) @@ -106,7 +109,17 @@ func parseConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.Log) e } func onHttpRequestHeaders(ctx wrapper.HttpContext, config TrafficTagConfig, log wrapper.Log) types.Action { - if add := (onContentRequestHeaders(config.ConditionGroups, log) || onWeightRequestHeaders(config.WeightGroups, config.randGen, log)); !add { + + add := false + if len(config.ConditionGroups) != 0 { + add = add || onContentRequestHeaders(config.ConditionGroups, log) + } + + if !add && len(config.WeightGroups) != 0 { + add = add || onWeightRequestHeaders(config.WeightGroups, rand.Uint64(), log) + } + + if !add { setDefaultTag(config.DefaultTagKey, config.DefaultTagVal, log) } diff --git a/plugins/wasm-go/extensions/traffic-tag/parse.go b/plugins/wasm-go/extensions/traffic-tag/parse.go index 7878c3ba9b..c41890c1a7 100644 --- a/plugins/wasm-go/extensions/traffic-tag/parse.go +++ b/plugins/wasm-go/extensions/traffic-tag/parse.go @@ -17,10 +17,8 @@ package main import ( "errors" "fmt" - "math/rand" "strconv" "strings" - "time" "github.com/higress-group/proxy-wasm-go-sdk/proxywasm" regexp "github.com/wasilibs/go-re2" @@ -85,7 +83,6 @@ func parseWeightConfig(json gjson.Result, config *TrafficTagConfig, log wrapper. var parseError error var accumulatedWeight int64 config.WeightGroups = []WeightGroup{} - config.randGen = rand.New(rand.NewSource(time.Now().UnixNano())) // parse default tag key and value if k, v := json.Get(DefaultTagKey), json.Get(DefaultTagVal); k.Exists() && v.Exists() { diff --git a/plugins/wasm-go/extensions/traffic-tag/utils.go b/plugins/wasm-go/extensions/traffic-tag/utils.go index 870150dcc0..dd33efae84 100644 --- a/plugins/wasm-go/extensions/traffic-tag/utils.go +++ b/plugins/wasm-go/extensions/traffic-tag/utils.go @@ -15,14 +15,12 @@ package main import ( - "errors" "fmt" "net/url" "strings" "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" "github.com/higress-group/proxy-wasm-go-sdk/proxywasm" - "github.com/tidwall/gjson" ) func setDefaultTag(k string, v string, log wrapper.Log) { @@ -75,23 +73,3 @@ func addTagHeader(key string, value string, log wrapper.Log) { } log.Infof("ADD HEADER: %s, value: %s", key, value) } - -func jsonValidate(json gjson.Result, log wrapper.Log) error { - if !json.Exists() { - log.Error("plugin config is missing in JSON") - return errors.New("plugin config is missing in JSON") - } - - jsonStr := strings.TrimSpace(json.Raw) - if jsonStr == "{}" || jsonStr == "" { - log.Error("plugin config is empty") - return errors.New("plugin config is empty") - } - - if !gjson.Valid(json.Raw) { - log.Error("plugin config is invalid JSON") - return errors.New("plugin config is invalid JSON") - } - - return nil -} diff --git a/plugins/wasm-go/extensions/traffic-tag/weight.go b/plugins/wasm-go/extensions/traffic-tag/weight.go index d6ea49f5d9..f825f1ec7c 100644 --- a/plugins/wasm-go/extensions/traffic-tag/weight.go +++ b/plugins/wasm-go/extensions/traffic-tag/weight.go @@ -15,16 +15,11 @@ package main import ( - "math/rand" - "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" ) -func onWeightRequestHeaders(weightGroups []WeightGroup, randGen *rand.Rand, log wrapper.Log) bool { - if len(weightGroups) == 0 { - return false - } - randomValue := randGen.Uint64() % TotalWeight +func onWeightRequestHeaders(weightGroups []WeightGroup, randomNum uint64, log wrapper.Log) bool { + randomValue := randomNum % TotalWeight log.Debugf("random value for weighted headers : %d", randomValue) // CDF for _, wg := range weightGroups {