aboutsummaryrefslogtreecommitdiff
path: root/plugin/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/pkg')
-rw-r--r--plugin/pkg/cache/cache.go32
-rw-r--r--plugin/pkg/singleflight/singleflight.go6
2 files changed, 20 insertions, 18 deletions
diff --git a/plugin/pkg/cache/cache.go b/plugin/pkg/cache/cache.go
index 954befa76..8a5ad783e 100644
--- a/plugin/pkg/cache/cache.go
+++ b/plugin/pkg/cache/cache.go
@@ -9,10 +9,10 @@ import (
)
// Hash returns the FNV hash of what.
-func Hash(what []byte) uint32 {
- h := fnv.New32()
+func Hash(what []byte) uint64 {
+ h := fnv.New64()
h.Write(what)
- return h.Sum32()
+ return h.Sum64()
}
// Cache is cache.
@@ -22,7 +22,7 @@ type Cache struct {
// shard is a cache with random eviction.
type shard struct {
- items map[uint32]interface{}
+ items map[uint64]interface{}
size int
sync.RWMutex
@@ -45,19 +45,19 @@ func New(size int) *Cache {
}
// Add adds a new element to the cache. If the element already exists it is overwritten.
-func (c *Cache) Add(key uint32, el interface{}) {
+func (c *Cache) Add(key uint64, el interface{}) {
shard := key & (shardSize - 1)
c.shards[shard].Add(key, el)
}
// Get looks up element index under key.
-func (c *Cache) Get(key uint32) (interface{}, bool) {
+func (c *Cache) Get(key uint64) (interface{}, bool) {
shard := key & (shardSize - 1)
return c.shards[shard].Get(key)
}
// Remove removes the element indexed with key.
-func (c *Cache) Remove(key uint32) {
+func (c *Cache) Remove(key uint64) {
shard := key & (shardSize - 1)
c.shards[shard].Remove(key)
}
@@ -72,10 +72,10 @@ func (c *Cache) Len() int {
}
// newShard returns a new shard with size.
-func newShard(size int) *shard { return &shard{items: make(map[uint32]interface{}), size: size} }
+func newShard(size int) *shard { return &shard{items: make(map[uint64]interface{}), size: size} }
// Add adds element indexed by key into the cache. Any existing element is overwritten
-func (s *shard) Add(key uint32, el interface{}) {
+func (s *shard) Add(key uint64, el interface{}) {
l := s.Len()
if l+1 > s.size {
s.Evict()
@@ -87,7 +87,7 @@ func (s *shard) Add(key uint32, el interface{}) {
}
// Remove removes the element indexed by key from the cache.
-func (s *shard) Remove(key uint32) {
+func (s *shard) Remove(key uint64) {
s.Lock()
delete(s.items, key)
s.Unlock()
@@ -95,26 +95,28 @@ func (s *shard) Remove(key uint32) {
// Evict removes a random element from the cache.
func (s *shard) Evict() {
- key := -1
+ hasKey := false
+ var key uint64
s.RLock()
for k := range s.items {
- key = int(k)
+ key = k
+ hasKey = true
break
}
s.RUnlock()
- if key == -1 {
+ if !hasKey {
// empty cache
return
}
// If this item is gone between the RUnlock and Lock race we don't care.
- s.Remove(uint32(key))
+ s.Remove(key)
}
// Get looks up the element indexed under key.
-func (s *shard) Get(key uint32) (interface{}, bool) {
+func (s *shard) Get(key uint64) (interface{}, bool) {
s.RLock()
el, found := s.items[key]
s.RUnlock()
diff --git a/plugin/pkg/singleflight/singleflight.go b/plugin/pkg/singleflight/singleflight.go
index 365e3ef58..e70646cd1 100644
--- a/plugin/pkg/singleflight/singleflight.go
+++ b/plugin/pkg/singleflight/singleflight.go
@@ -31,17 +31,17 @@ type call struct {
// units of work can be executed with duplicate suppression.
type Group struct {
mu sync.Mutex // protects m
- m map[uint32]*call // lazily initialized
+ m map[uint64]*call // lazily initialized
}
// Do executes and returns the results of the given function, making
// sure that only one execution is in-flight for a given key at a
// time. If a duplicate comes in, the duplicate caller waits for the
// original to complete and receives the same results.
-func (g *Group) Do(key uint32, fn func() (interface{}, error)) (interface{}, error) {
+func (g *Group) Do(key uint64, fn func() (interface{}, error)) (interface{}, error) {
g.mu.Lock()
if g.m == nil {
- g.m = make(map[uint32]*call)
+ g.m = make(map[uint64]*call)
}
if c, ok := g.m[key]; ok {
g.mu.Unlock()