-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ashish Naware <ashishnaware3@gmail.com>
- Loading branch information
1 parent
88036cd
commit 6f1f0ce
Showing
4 changed files
with
147 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package observer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cilium/tetragon/pkg/api/dataapi" | ||
lru "github.com/hashicorp/golang-lru/v2" | ||
) | ||
|
||
type cache struct { | ||
cache *lru.Cache[dataapi.DataEventId, []byte] | ||
size int | ||
} | ||
|
||
// newCache constructs a cache of fixed size with the callback function that increments | ||
// data_cache_evictions_total counter every time the cache is evicted. | ||
func newCache(dataCacheSize int) (*cache, error) { | ||
lruCache, err := lru.NewWithEvict( | ||
dataCacheSize, | ||
func(_ dataapi.DataEventId, _ []byte) { | ||
dataCacheEvictions.Inc() | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cache := &cache{ | ||
cache: lruCache, | ||
size: dataCacheSize, | ||
} | ||
return cache, nil | ||
} | ||
|
||
func (c *cache) get(dataEventId dataapi.DataEventId) ([]byte, error) { | ||
data, ok := c.cache.Get(dataEventId) | ||
if !ok { | ||
dataCacheMisses.WithLabelValues("get").Inc() | ||
return nil, fmt.Errorf("data event with id : %v not found", dataEventId) | ||
} | ||
return data, nil | ||
} | ||
|
||
func (c *cache) add(id dataapi.DataEventId, msgData []byte) bool { | ||
evicted := c.cache.Add(id, msgData) | ||
if !evicted { | ||
dataCacheTotal.Inc() | ||
} | ||
return evicted | ||
} | ||
|
||
func (c *cache) remove(desc dataapi.DataEventDesc) bool { | ||
present := c.cache.Remove(desc.Id) | ||
if present { | ||
dataCacheTotal.Dec() | ||
} else { | ||
dataCacheMisses.WithLabelValues("remove").Inc() | ||
} | ||
return present | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters