Skip to content

Commit

Permalink
Merge pull request #44 from andrewshan/main
Browse files Browse the repository at this point in the history
全匹配限流
  • Loading branch information
andrewshan authored Apr 14, 2022
2 parents 6c51ca3 + 4c62b34 commit de7643f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/flow/quota/assist.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ func matchRuleByLabels(
}
var allLabelsMatched = true
for labelKey, labelValue := range rule.Labels {
if labelKey == pb.MatchAll {
continue
}
if !matchLabels(labelKey, labelValue, labels, ruleCache) {
allLabelsMatched = false
break
Expand Down
5 changes: 4 additions & 1 deletion pkg/flow/quota/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ func createBehavior(supplier plugin.Supplier, behaviorName string) ratelimiter.S
// matchLabels 校验输入的元数据是否符合规则
func matchLabels(ruleMetaKey string, ruleMetaValue *namingpb.MatchString,
labels map[string]string, ruleCache model.RuleCache) bool {
ruleMetaValueStr := ruleMetaValue.GetValue().GetValue()
if ruleMetaValueStr == pb.MatchAll {
return true
}
if len(labels) == 0 {
return false
}
Expand All @@ -474,7 +478,6 @@ func matchLabels(ruleMetaKey string, ruleMetaValue *namingpb.MatchString,
// 集成的路由规则不包含这个key,就不匹配
return false
}
ruleMetaValueStr := ruleMetaValue.GetValue().GetValue()
switch ruleMetaValue.Type {
case namingpb.MatchString_REGEX:
regexObj := ruleCache.GetRegexMatcher(ruleMetaValueStr)
Expand Down
5 changes: 5 additions & 0 deletions pkg/model/pb/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,18 @@ func (s *ServiceRuleInProto) ValidateAndBuildCache() error {
return nil
}

const MatchAll = "*"

// buildCacheFromMatcher 通过metadata来构建缓存
func buildCacheFromMatcher(metadata map[string]*namingpb.MatchString, ruleCache model.RuleCache) error {
if len(metadata) == 0 {
return nil
}
for _, metaValue := range metadata {
valueRawStr := metaValue.GetValue().GetValue()
if valueRawStr == MatchAll {
continue
}
// 如果是 variable 类型,但是value 是空的,此时无法通过 value 获取环境变量,报错
if metaValue.ValueType == namingpb.MatchString_VARIABLE && valueRawStr == "" {
return fmt.Errorf("value of variable type can not be empty")
Expand Down

0 comments on commit de7643f

Please sign in to comment.