Skip to content

Commit

Permalink
feat: bump golang-lru to current major version (#41675) (#41761)
Browse files Browse the repository at this point in the history
* feat: bump golang-lru to current major version

v0 is no longer support, bump lib to v2

* fix compile errors

* lint: fix linter issues

(cherry picked from commit 6d1c81e)

Co-authored-by: kruskall <[email protected]>
  • Loading branch information
mergify[bot] and kruskall authored Nov 25, 2024
1 parent f5df794 commit 34b23fe
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 32 deletions.
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

0 comments on commit 34b23fe

Please sign in to comment.