update dependencies (#6267)

Signed-off-by: hongming <coder.scala@gmail.com>
This commit is contained in:
hongming
2024-11-06 10:27:06 +08:00
committed by GitHub
parent faf255a084
commit cfebd96a1f
4263 changed files with 341374 additions and 132036 deletions

View File

@@ -10,10 +10,13 @@ import (
"github.com/open-policy-agent/opa/topdown/builtins"
)
const globCacheMaxSize = 100
const globInterQueryValueCacheHits = "rego_builtin_glob_interquery_value_cache_hits"
var globCacheLock = sync.Mutex{}
var globCache map[string]glob.Glob
func builtinGlobMatch(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
func builtinGlobMatch(bctx BuiltinContext, operands []*ast.Term, iter func(*ast.Term) error) error {
pattern, err := builtins.StringOperand(operands[0].Value, 1)
if err != nil {
return err
@@ -48,14 +51,41 @@ func builtinGlobMatch(_ BuiltinContext, operands []*ast.Term, iter func(*ast.Ter
}
id := builder.String()
m, err := globCompileAndMatch(id, string(pattern), string(match), delimiters)
m, err := globCompileAndMatch(bctx, id, string(pattern), string(match), delimiters)
if err != nil {
return err
}
return iter(ast.BooleanTerm(m))
}
func globCompileAndMatch(id, pattern, match string, delimiters []rune) (bool, error) {
func globCompileAndMatch(bctx BuiltinContext, id, pattern, match string, delimiters []rune) (bool, error) {
if bctx.InterQueryBuiltinValueCache != nil {
val, ok := bctx.InterQueryBuiltinValueCache.Get(ast.String(id))
if ok {
pat, valid := val.(glob.Glob)
if !valid {
// The cache key may exist for a different value type (eg. regex).
// In this case, we calculate the glob and return the result w/o updating the cache.
var err error
if pat, err = glob.Compile(pattern, delimiters...); err != nil {
return false, err
}
return pat.Match(match), nil
}
bctx.Metrics.Counter(globInterQueryValueCacheHits).Incr()
out := pat.Match(match)
return out, nil
}
res, err := glob.Compile(pattern, delimiters...)
if err != nil {
return false, err
}
bctx.InterQueryBuiltinValueCache.Insert(ast.String(id), res)
return res.Match(match), nil
}
globCacheLock.Lock()
defer globCacheLock.Unlock()
p, ok := globCache[id]
@@ -64,6 +94,13 @@ func globCompileAndMatch(id, pattern, match string, delimiters []rune) (bool, er
if p, err = glob.Compile(pattern, delimiters...); err != nil {
return false, err
}
if len(globCache) >= globCacheMaxSize {
// Delete a (semi-)random key to make room for the new one.
for k := range globCache {
delete(globCache, k)
break
}
}
globCache[id] = p
}
out := p.Match(match)