dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/router/condition/matcher/base.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package matcher
    19  
    20  import (
    21  	"sort"
    22  	"sync"
    23  )
    24  
    25  import (
    26  	"github.com/dubbogo/gost/log/logger"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/cluster/router/condition/matcher/pattern_value"
    31  	"dubbo.apache.org/dubbo-go/v3/common"
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  	"dubbo.apache.org/dubbo-go/v3/protocol"
    34  )
    35  
    36  var (
    37  	valueMatchers = make([]pattern_value.ValuePattern, 0, 8)
    38  
    39  	once sync.Once
    40  )
    41  
    42  // BaseConditionMatcher records the match and mismatch patterns of this matcher while at the same time
    43  // provides the common match logics.
    44  type BaseConditionMatcher struct {
    45  	key        string
    46  	matches    map[string]struct{}
    47  	misMatches map[string]struct{}
    48  }
    49  
    50  func NewBaseConditionMatcher(key string) *BaseConditionMatcher {
    51  	return &BaseConditionMatcher{
    52  		key:        key,
    53  		matches:    map[string]struct{}{},
    54  		misMatches: map[string]struct{}{},
    55  	}
    56  }
    57  
    58  // GetValue returns a value from different places of the request context.
    59  func (b *BaseConditionMatcher) GetValue(sample map[string]string, url *common.URL, invocation protocol.Invocation) string {
    60  	return ""
    61  }
    62  
    63  // IsMatch indicates whether this matcher matches the patterns with request context.
    64  func (b *BaseConditionMatcher) IsMatch(value string, param *common.URL, invocation protocol.Invocation, isWhenCondition bool) bool {
    65  	if value == "" {
    66  		// if key does not present in whichever of url, invocation or attachment based on the matcher type, then return false.
    67  		return false
    68  	}
    69  
    70  	if len(b.matches) != 0 && len(b.misMatches) == 0 {
    71  		return b.patternMatches(value, param, invocation, isWhenCondition)
    72  	}
    73  
    74  	if len(b.misMatches) != 0 && len(b.matches) == 0 {
    75  		return b.patternMisMatches(value, param, invocation, isWhenCondition)
    76  	}
    77  
    78  	if len(b.matches) != 0 && len(b.misMatches) != 0 {
    79  		// when both mismatches and matches contain the same value, then using mismatches first
    80  		return b.patternMisMatches(value, param, invocation, isWhenCondition) && b.patternMatches(value, param, invocation, isWhenCondition)
    81  	}
    82  	return false
    83  }
    84  
    85  // GetMatches returns matches.
    86  func (b *BaseConditionMatcher) GetMatches() map[string]struct{} {
    87  	return b.matches
    88  }
    89  
    90  // GetMismatches returns misMatches.
    91  func (b *BaseConditionMatcher) GetMismatches() map[string]struct{} {
    92  	return b.misMatches
    93  }
    94  
    95  func (b *BaseConditionMatcher) patternMatches(value string, param *common.URL, invocation protocol.Invocation, isWhenCondition bool) bool {
    96  	for match := range b.matches {
    97  		if doPatternMatch(match, value, param, invocation, isWhenCondition) {
    98  			return true
    99  		}
   100  	}
   101  	return false
   102  }
   103  
   104  func (b *BaseConditionMatcher) patternMisMatches(value string, param *common.URL, invocation protocol.Invocation, isWhenCondition bool) bool {
   105  	for mismatch := range b.misMatches {
   106  		if doPatternMatch(mismatch, value, param, invocation, isWhenCondition) {
   107  			return false
   108  		}
   109  	}
   110  	return true
   111  }
   112  
   113  func doPatternMatch(pattern string, value string, url *common.URL, invocation protocol.Invocation, isWhenCondition bool) bool {
   114  	once.Do(initValueMatchers)
   115  	for _, valueMatcher := range valueMatchers {
   116  		if valueMatcher.ShouldMatch(pattern) {
   117  			return valueMatcher.Match(pattern, value, url, invocation, isWhenCondition)
   118  		}
   119  	}
   120  	// If no value matcher is available, will force to use wildcard value matcher
   121  	logger.Error("Executing condition rule value match expression error, will force to use wildcard value matcher")
   122  
   123  	valuePattern := pattern_value.GetValuePattern(constant.Wildcard)
   124  	return valuePattern.Match(pattern, value, url, invocation, isWhenCondition)
   125  }
   126  
   127  // GetSampleValueFromURL returns the value of the conditionKey in the URL
   128  func GetSampleValueFromURL(conditionKey string, sample map[string]string, param *common.URL, invocation protocol.Invocation) string {
   129  	var sampleValue string
   130  	// get real invoked method name from invocation
   131  	if invocation != nil && (constant.MethodKey == conditionKey || constant.MethodsKey == conditionKey) {
   132  		sampleValue = invocation.MethodName()
   133  	} else {
   134  		sampleValue = sample[conditionKey]
   135  	}
   136  	return sampleValue
   137  }
   138  
   139  func Match(condition Matcher, sample map[string]string, param *common.URL, invocation protocol.Invocation, isWhenCondition bool) bool {
   140  	return condition.IsMatch(condition.GetValue(sample, param, invocation), param, invocation, isWhenCondition)
   141  }
   142  
   143  func initValueMatchers() {
   144  	valuePatterns := pattern_value.GetValuePatterns()
   145  	for _, valuePattern := range valuePatterns {
   146  		valueMatchers = append(valueMatchers, valuePattern())
   147  	}
   148  	sortValuePattern(valueMatchers)
   149  }
   150  
   151  func sortValuePattern(valuePatterns []pattern_value.ValuePattern) {
   152  	sort.Stable(byPriority(valuePatterns))
   153  }
   154  
   155  type byPriority []pattern_value.ValuePattern
   156  
   157  func (a byPriority) Len() int           { return len(a) }
   158  func (a byPriority) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
   159  func (a byPriority) Less(i, j int) bool { return a[i].Priority() < a[j].Priority() }