stash/pkg/sqlite/regex.go
its-josh4 2b8c2534dd
Update a number of dependencies (incl. CVE fixes) (#4107)
* Update a number of dependencies (incl. CVE fixes)

Includes some dependencies that were upgraded in #4106 as well as a few more dependencies.

Some deps that have been upgraded had CVEs.

Notably, upgrades deprecated dependencies such as:
- `github.com/go-chi/chi` (replaced with `/v5`)
- `github.com/gofrs/uuid` (replaced with `/v5`)
- `github.com/hashicorp/golang-lru` (replaced with `/v2` which uses generics)

* Upgraded a few more deps

* lint

* reverted yaml library to v2

* remove unnecessary mod replace

* Update chromedp

Fixes #3733
2023-10-26 16:24:32 +11:00

38 lines
1,021 B
Go

package sqlite
import (
"regexp"
lru "github.com/hashicorp/golang-lru/v2"
)
// size of the regex LRU cache in elements.
// A small number number was chosen because it's most likely use is for a
// single query - this function gets called for every row in the (filtered)
// results. It's likely to only need no more than 1 or 2 in any given query.
// After that point, it's just sitting in the cache and is unlikely to be used
// again.
const regexCacheSize = 10
var regexCache *lru.Cache[string, *regexp.Regexp]
func init() {
regexCache, _ = lru.New[string, *regexp.Regexp](regexCacheSize)
}
// regexFn is registered as an SQLite function as "regexp"
// It uses an LRU cache to cache recent regex patterns to reduce CPU load over
// identical patterns.
func regexFn(re, s string) (bool, error) {
compiled, ok := regexCache.Get(re)
if !ok {
var err error
compiled, err = regexp.Compile(re)
if err != nil {
return false, err
}
regexCache.Add(re, compiled)
}
return compiled.MatchString(s), nil
}