mirror of
https://github.com/alibaba/higress.git
synced 2026-06-08 12:17:28 +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 {
|
func onContentRequestHeaders(conditionGroups []ConditionGroup, log wrapper.Log) bool {
|
||||||
if len(conditionGroups) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, cg := range conditionGroups {
|
for _, cg := range conditionGroups {
|
||||||
if matchCondition(&cg, log) {
|
if matchCondition(&cg, log) {
|
||||||
addTagHeader(cg.HeaderName, cg.HeaderValue, log)
|
addTagHeader(cg.HeaderName, cg.HeaderValue, log)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
|
||||||
@@ -60,7 +61,6 @@ type TrafficTagConfig struct {
|
|||||||
WeightGroups []WeightGroup `json:"weightGroups,omitempty"`
|
WeightGroups []WeightGroup `json:"weightGroups,omitempty"`
|
||||||
DefaultTagKey string `json:"defaultTagKey,omitempty"`
|
DefaultTagKey string `json:"defaultTagKey,omitempty"`
|
||||||
DefaultTagVal string `json:"defaultTagVal,omitempty"`
|
DefaultTagVal string `json:"defaultTagVal,omitempty"`
|
||||||
randGen *rand.Rand
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConditionGroup struct {
|
type ConditionGroup struct {
|
||||||
@@ -93,8 +93,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.Log) error {
|
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)
|
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 {
|
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)
|
setDefaultTag(config.DefaultTagKey, config.DefaultTagVal, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,10 +17,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
regexp "github.com/wasilibs/go-re2"
|
regexp "github.com/wasilibs/go-re2"
|
||||||
@@ -85,7 +83,6 @@ func parseWeightConfig(json gjson.Result, config *TrafficTagConfig, log wrapper.
|
|||||||
var parseError error
|
var parseError error
|
||||||
var accumulatedWeight int64
|
var accumulatedWeight int64
|
||||||
config.WeightGroups = []WeightGroup{}
|
config.WeightGroups = []WeightGroup{}
|
||||||
config.randGen = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
|
|
||||||
// parse default tag key and value
|
// parse default tag key and value
|
||||||
if k, v := json.Get(DefaultTagKey), json.Get(DefaultTagVal); k.Exists() && v.Exists() {
|
if k, v := json.Get(DefaultTagKey), json.Get(DefaultTagVal); k.Exists() && v.Exists() {
|
||||||
|
|||||||
@@ -15,14 +15,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setDefaultTag(k string, v string, log wrapper.Log) {
|
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)
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
|
|
||||||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func onWeightRequestHeaders(weightGroups []WeightGroup, randGen *rand.Rand, log wrapper.Log) bool {
|
func onWeightRequestHeaders(weightGroups []WeightGroup, randomNum uint64, log wrapper.Log) bool {
|
||||||
if len(weightGroups) == 0 {
|
randomValue := randomNum % TotalWeight
|
||||||
return false
|
|
||||||
}
|
|
||||||
randomValue := randGen.Uint64() % TotalWeight
|
|
||||||
log.Debugf("random value for weighted headers : %d", randomValue)
|
log.Debugf("random value for weighted headers : %d", randomValue)
|
||||||
// CDF
|
// CDF
|
||||||
for _, wg := range weightGroups {
|
for _, wg := range weightGroups {
|
||||||
|
|||||||
Reference in New Issue
Block a user