Skip to content

Commit

Permalink
bugfix: remove config check and fix memory leak in traffic-tag (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsnan authored Oct 28, 2024
1 parent acec48e commit 7c33ebf
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 40 deletions.
4 changes: 0 additions & 4 deletions plugins/wasm-go/extensions/traffic-tag/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions plugins/wasm-go/extensions/traffic-tag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
3 changes: 0 additions & 3 deletions plugins/wasm-go/extensions/traffic-tag/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down
22 changes: 0 additions & 22 deletions plugins/wasm-go/extensions/traffic-tag/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
9 changes: 2 additions & 7 deletions plugins/wasm-go/extensions/traffic-tag/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7c33ebf

Please sign in to comment.