mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
8d3fc1ad3e
* added in slasher metrics * Merge branch 'master' into slasher-metrics * add in prom bolt metrics for slasher * Merge branch 'slasher-metrics' of github.com:prysmaticlabs/prysm into slasher-metrics * imports * include all metrics * no dup bolt collector * Update slasher/detection/attestations/spanner.go Co-Authored-By: Ivan Martinez <ivanthegreatdev@gmail.com> * naming best practices for prom, thx Terence * Merge branch 'slasher-metrics' of github.com:prysmaticlabs/prysm into slasher-metrics
73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package cache
|
|
|
|
import (
|
|
lru "github.com/hashicorp/golang-lru"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
|
|
)
|
|
|
|
var (
|
|
// epochSpansCacheSize defines the max number of epoch spans the cache can hold.
|
|
epochSpansCacheSize = 256
|
|
// Metrics for the span cache.
|
|
epochSpansCacheHit = promauto.NewCounter(prometheus.CounterOpts{
|
|
Name: "epoch_spans_cache_hit",
|
|
Help: "The total number of cache hits on the epoch spans cache.",
|
|
})
|
|
epochSpansCacheMiss = promauto.NewCounter(prometheus.CounterOpts{
|
|
Name: "epoch_spans_cache_miss",
|
|
Help: "The total number of cache misses on the epoch spans cache.",
|
|
})
|
|
)
|
|
|
|
// EpochSpansCache is used to store the spans needed on a per-epoch basis for slashing detection.
|
|
type EpochSpansCache struct {
|
|
cache *lru.Cache
|
|
}
|
|
|
|
// NewEpochSpansCache initializes the map and underlying cache.
|
|
func NewEpochSpansCache(size int, onEvicted func(key interface{}, value interface{})) (*EpochSpansCache, error) {
|
|
if size != 0 {
|
|
epochSpansCacheSize = size
|
|
}
|
|
cache, err := lru.NewWithEvict(epochSpansCacheSize, onEvicted)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &EpochSpansCache{cache: cache}, nil
|
|
}
|
|
|
|
// Get returns an ok bool and the cached value for the requested epoch key, if any.
|
|
func (c *EpochSpansCache) Get(epoch uint64) (map[uint64]types.Span, bool) {
|
|
item, exists := c.cache.Get(epoch)
|
|
if exists && item != nil {
|
|
epochSpansCacheHit.Inc()
|
|
return item.(map[uint64]types.Span), true
|
|
}
|
|
|
|
epochSpansCacheMiss.Inc()
|
|
return make(map[uint64]types.Span), false
|
|
}
|
|
|
|
// Set the response in the cache.
|
|
func (c *EpochSpansCache) Set(epoch uint64, epochSpans map[uint64]types.Span) {
|
|
_ = c.cache.Add(epoch, epochSpans)
|
|
}
|
|
|
|
// Delete removes an epoch from the cache and returns if it existed or not.
|
|
// Performs the onEviction function before removal.
|
|
func (c *EpochSpansCache) Delete(epoch uint64) bool {
|
|
return c.cache.Remove(epoch)
|
|
}
|
|
|
|
// Has returns true if the key exists in the cache.
|
|
func (c *EpochSpansCache) Has(epoch uint64) bool {
|
|
return c.cache.Contains(epoch)
|
|
}
|
|
|
|
// Clear removes all keys from the SpanCache.
|
|
func (c *EpochSpansCache) Clear() {
|
|
c.cache.Purge()
|
|
}
|