mirror of
https://github.com/alibaba/higress.git
synced 2026-02-28 22:50:57 +08:00
bugfix: remove config check and fix memory leak in traffic-tag (#1435)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user