Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x](backport #41675) feat: bump golang-lru to current major version #41761

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19960,12 +19960,12 @@ Exhibit B - "Incompatible With Secondary Licenses" Notice


--------------------------------------------------------------------------------
Dependency : github.com/hashicorp/golang-lru
Version: v0.6.0
Dependency : github.com/hashicorp/golang-lru/v2
Version: v2.0.7
Licence type (autodetected): MPL-2.0
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/hashicorp/golang-lru@v0.6.0/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/hashicorp/golang-lru/[email protected]/LICENSE:

Copyright (c) 2014 HashiCorp, Inc.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ require (
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/h2non/filetype v1.1.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/hashicorp/golang-lru v0.6.0
github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95
github.com/insomniacslk/dhcp v0.0.0-20220119180841-3c283ff8b7dd
Expand Down Expand Up @@ -209,6 +208,7 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/icholy/digest v0.1.22
github.com/klauspost/compress v1.17.11
github.com/meraki/dashboard-api-go/v3 v3.0.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,8 @@ github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.6.0 h1:uL2shRDx7RTrOrTCUZEGP/wJUFiUI8QT6E7z5o8jga4=
github.com/hashicorp/golang-lru v0.6.0/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3 h1:fgVfQ4AC1avVOnu2cfms8VAiD8lUq3vWI8mTocOXN/w=
github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3/go.mod h1:svtxn6QnrQ69P23VvIWMR34tg3vmwLz4UdUzm1dSCgE=
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95 h1:S4qyfL2sEm5Budr4KVMyEniCy+PbS55651I/a+Kn/NQ=
Expand Down
4 changes: 2 additions & 2 deletions x-pack/osquerybeat/beater/osquerybeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/gofrs/uuid/v5"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/osquery/osquery-go"
kconfig "github.com/osquery/osquery-go/plugin/config"
klogger "github.com/osquery/osquery-go/plugin/logger"
Expand Down Expand Up @@ -254,7 +254,7 @@ func (bt *osquerybeat) runOsquery(ctx context.Context, b *beat.Beat, osq *osqd.O
socketPath := osq.SocketPath()

// Create a cache for queries types resolution
cache, err := lru.New(adhocOsqueriesTypesCacheSize)
cache, err := lru.New[string, map[string]string](adhocOsqueriesTypesCacheSize)
if err != nil {
bt.log.Errorf("Failed to create osquery query results types cache: %v", err)
return err
Expand Down
26 changes: 13 additions & 13 deletions x-pack/osquerybeat/internal/osqdcli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@

package osqdcli

type Cache interface {
Add(key, value interface{}) (evicted bool)
Get(key interface{}) (value interface{}, ok bool)
type Cache[K comparable, V any] interface {
Add(K, V) (evicted bool)
Get(K) (value V, ok bool)
Resize(size int) (evicted int)
}

func WithCache(cache Cache, minSize int) Option {
func WithCache(cache Cache[string, map[string]string], minSize int) Option {
return func(c *Client) {
nsc := &nullSafeCache{cache: cache}
nsc := &nullSafeCache[string, map[string]string]{cache: cache}
if minSize > 0 {
nsc.minSize = minSize
}
c.cache = nsc
}
}

type nullSafeCache struct {
cache Cache
type nullSafeCache[K comparable, V any] struct {
cache Cache[K, V]
minSize int
}

func (c *nullSafeCache) Add(key, value interface{}) (evicted bool) {
func (c *nullSafeCache[K, V]) Add(key K, value V) (evicted bool) {
if c.cache == nil {
return
return false
}
return c.cache.Add(key, value)
}

func (c *nullSafeCache) Get(key interface{}) (value interface{}, ok bool) {
func (c *nullSafeCache[K, V]) Get(key K) (value V, ok bool) {
if c.cache == nil {
return
return value, ok
}
return c.cache.Get(key)
}

func (c *nullSafeCache) Resize(size int) (evicted int) {
func (c *nullSafeCache[K, V]) Resize(size int) (evicted int) {
if c.cache == nil {
return
return 0
}
return c.cache.Resize(c.minSize + size)
}
16 changes: 5 additions & 11 deletions x-pack/osquerybeat/internal/osqdcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Client struct {
cli *osquery.ExtensionManagerClient
mx sync.Mutex

cache Cache
cache Cache[string, map[string]string]

cliLimiter *semaphore.Weighted
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func New(socketPath string, opts ...Option) *Client {
timeout: defaultTimeout,
maxTimeout: defaultMaxTimeout,
connectRetries: defaultConnectRetries,
cache: &nullSafeCache{},
cache: &nullSafeCache[string, map[string]string]{},
cliLimiter: semaphore.NewWeighted(limit),
}

Expand Down Expand Up @@ -258,15 +258,9 @@ func (c *Client) resolveResult(ctx context.Context, sql string, hits []map[strin
}

func (c *Client) queryColumnTypes(ctx context.Context, sql string) (map[string]string, error) {
var colTypes map[string]string

if v, ok := c.cache.Get(sql); ok {
colTypes, ok = v.(map[string]string)
if ok {
c.log.Debugf("using cached column types for query: %s", sql)
} else {
c.log.Error("failed get the column types from cache, incompatible type")
}
colTypes, ok := c.cache.Get(sql)
if ok {
c.log.Debugf("using cached column types for query: %s", sql)
}

if colTypes == nil {
Expand Down
Loading