diff --git a/algorithms.go b/algorithms.go index c9496742..590d780b 100644 --- a/algorithms.go +++ b/algorithms.go @@ -213,7 +213,7 @@ func tokenBucketNewItem(ctx context.Context, s Store, c Cache, r *RateLimitReq) now := MillisecondNow() expire := now + r.Duration - item := CacheItem{ + item := &CacheItem{ Algorithm: Algorithm_TOKEN_BUCKET, Key: r.HashKey(), Value: &TokenBucketItem{ @@ -440,20 +440,6 @@ func leakyBucketNewItem(ctx context.Context, s Store, c Cache, r *RateLimitReq) defer span.Finish() now := MillisecondNow() - expire := now + r.Duration - - item := CacheItem{ - Algorithm: Algorithm_LEAKY_BUCKET, - Key: r.HashKey(), - Value: &TokenBucketItem{ - Limit: r.Limit, - Duration: r.Duration, - Remaining: r.Limit - r.Hits, - CreatedAt: now, - }, - ExpireAt: expire, - } - duration := r.Duration rate := float64(duration) / float64(r.Limit) if HasBehavior(r.Behavior, Behavior_DURATION_IS_GREGORIAN) { @@ -492,7 +478,7 @@ func leakyBucketNewItem(ctx context.Context, s Store, c Cache, r *RateLimitReq) b.Remaining = 0 } - item = CacheItem{ + item := &CacheItem{ ExpireAt: now + duration, Algorithm: r.Algorithm, Key: r.HashKey(), diff --git a/cache.go b/cache.go index f6e64bc1..04ef11a0 100644 --- a/cache.go +++ b/cache.go @@ -18,10 +18,10 @@ package gubernator // So algorithms can interface with different cache implementations type Cache interface { - Add(item CacheItem) bool + Add(item *CacheItem) bool UpdateExpiration(key string, expireAt int64) bool - GetItem(key string) (value CacheItem, ok bool) - Each() chan CacheItem + GetItem(key string) (value *CacheItem, ok bool) + Each() chan *CacheItem Remove(key string) Size() int64 Close() error diff --git a/gubernator.go b/gubernator.go index 3b9e90c2..95d9bd77 100644 --- a/gubernator.go +++ b/gubernator.go @@ -460,7 +460,7 @@ func (s *V1Instance) UpdatePeerGlobals(ctx context.Context, r *UpdatePeerGlobals defer span.Finish() for _, g := range r.Globals { - item := CacheItem{ + item := &CacheItem{ ExpireAt: g.Status.ResetTime, Algorithm: g.Algorithm, Value: g.Status, diff --git a/gubernator_pool.go b/gubernator_pool.go index cf7a0956..263822a8 100644 --- a/gubernator_pool.go +++ b/gubernator_pool.go @@ -70,7 +70,7 @@ type poolHashRingNode struct { type poolStoreRequest struct { ctx context.Context response chan poolStoreResponse - out chan<- CacheItem + out chan<- *CacheItem } type poolStoreResponse struct{} @@ -78,7 +78,7 @@ type poolStoreResponse struct{} type poolLoadRequest struct { ctx context.Context response chan poolLoadResponse - in <-chan CacheItem + in <-chan *CacheItem } type poolLoadResponse struct{} @@ -86,7 +86,7 @@ type poolLoadResponse struct{} type poolAddCacheItemRequest struct { ctx context.Context response chan poolAddCacheItemResponse - item CacheItem + item *CacheItem } type poolAddCacheItemResponse struct { @@ -100,7 +100,7 @@ type poolGetCacheItemRequest struct { } type poolGetCacheItemResponse struct { - item CacheItem + item *CacheItem ok bool } @@ -340,7 +340,7 @@ func (chp *GubernatorPool) Load(ctx context.Context) error { } type loadChannel struct { - ch chan CacheItem + ch chan *CacheItem worker *poolWorker respChan chan poolLoadResponse } @@ -351,7 +351,7 @@ func (chp *GubernatorPool) Load(ctx context.Context) error { // Send each item to assigned channel's cache. mainloop: for { - var item CacheItem + var item *CacheItem var ok bool select { @@ -372,7 +372,7 @@ mainloop: loadCh, exist := loadChMap[worker] if !exist { loadCh = loadChannel{ - ch: make(chan CacheItem), + ch: make(chan *CacheItem), worker: worker, respChan: make(chan poolLoadResponse), } @@ -422,7 +422,7 @@ func (chp *GubernatorPool) handleLoad(request poolLoadRequest, cache Cache) { mainloop: for { - var item CacheItem + var item *CacheItem var ok bool select { @@ -460,7 +460,7 @@ func (chp *GubernatorPool) Store(ctx context.Context) error { defer span.Finish() var wg sync.WaitGroup - out := make(chan CacheItem, 500) + out := make(chan *CacheItem, 500) // Iterate each worker's cache to `out` channel. for _, worker := range chp.workers { @@ -543,7 +543,7 @@ func (chp *GubernatorPool) handleStore(request poolStoreRequest, cache Cache) { } // Add to worker's cache. -func (chp *GubernatorPool) AddCacheItem(ctx context.Context, key string, item CacheItem) error { +func (chp *GubernatorPool) AddCacheItem(ctx context.Context, key string, item *CacheItem) error { span, ctx := tracing.StartSpan(ctx) defer span.Finish() @@ -596,7 +596,7 @@ func (chp *GubernatorPool) handleAddCacheItem(request poolAddCacheItemRequest, c } // Get item from worker's cache. -func (chp *GubernatorPool) GetCacheItem(ctx context.Context, key string) (CacheItem, bool, error) { +func (chp *GubernatorPool) GetCacheItem(ctx context.Context, key string) (*CacheItem, bool, error) { span, ctx := tracing.StartSpan(ctx) defer span.Finish() @@ -621,13 +621,13 @@ func (chp *GubernatorPool) GetCacheItem(ctx context.Context, key string) (CacheI case <-ctx.Done(): // Context canceled. ext.LogError(span, ctx.Err()) - return CacheItem{}, false, ctx.Err() + return nil, false, ctx.Err() } case <-ctx.Done(): // Context canceled. ext.LogError(span, ctx.Err()) - return CacheItem{}, false, ctx.Err() + return nil, false, ctx.Err() } } diff --git a/gubernator_pool_test.go b/gubernator_pool_test.go index 4169829f..2c3db4a7 100644 --- a/gubernator_pool_test.go +++ b/gubernator_pool_test.go @@ -43,9 +43,9 @@ func TestGubernatorPool(t *testing.T) { t.Run(testCase.name, func(t *testing.T) { // Setup mock data. const NumCacheItems = 100 - cacheItems := []guber.CacheItem{} + cacheItems := []*guber.CacheItem{} for i := 0; i < NumCacheItems; i++ { - cacheItems = append(cacheItems, guber.CacheItem{ + cacheItems = append(cacheItems, &guber.CacheItem{ Key: fmt.Sprintf("Foobar%04d", i), Value: fmt.Sprintf("Stuff%04d", i), ExpireAt: 4131978658000, @@ -65,7 +65,7 @@ func TestGubernatorPool(t *testing.T) { chp := guber.NewGubernatorPool(conf, testCase.concurrency) // Mock Loader. - fakeLoadCh := make(chan guber.CacheItem, NumCacheItems) + fakeLoadCh := make(chan *guber.CacheItem, NumCacheItems) for _, item := range cacheItems { fakeLoadCh <- item } @@ -100,8 +100,8 @@ func TestGubernatorPool(t *testing.T) { mockLoader.On("Save", mock.Anything).Once().Return(nil). Run(func(args mock.Arguments) { // Verify items sent over the channel passed to Save(). - saveCh := args.Get(0).(chan guber.CacheItem) - savedItems := []guber.CacheItem{} + saveCh := args.Get(0).(chan *guber.CacheItem) + savedItems := []*guber.CacheItem{} for item := range saveCh { savedItems = append(savedItems, item) } @@ -114,7 +114,7 @@ func TestGubernatorPool(t *testing.T) { }) // Mock Cache. - eachCh := make(chan guber.CacheItem, NumCacheItems) + eachCh := make(chan *guber.CacheItem, NumCacheItems) for _, item := range cacheItems { eachCh <- item } diff --git a/lrucache.go b/lrucache.go index 3bde85e1..c639b5c5 100644 --- a/lrucache.go +++ b/lrucache.go @@ -70,11 +70,11 @@ func NewLRUCache(maxSize int) *LRUCache { // It would be safer if this were done using an iterator or delegate pattern // that doesn't require a goroutine. // May need to reassess functional requirements. -func (c *LRUCache) Each() chan CacheItem { - out := make(chan CacheItem) +func (c *LRUCache) Each() chan *CacheItem { + out := make(chan *CacheItem) go func() { for _, ele := range c.cache { - out <- ele.Value.(CacheItem) + out <- ele.Value.(*CacheItem) } close(out) }() @@ -82,7 +82,7 @@ func (c *LRUCache) Each() chan CacheItem { } // Adds a value to the cache. -func (c *LRUCache) Add(item CacheItem) bool { +func (c *LRUCache) Add(item *CacheItem) bool { // If the key already exist, set the new value if ee, ok := c.cache[item.Key]; ok { c.ll.MoveToFront(ee) @@ -105,9 +105,9 @@ func MillisecondNow() int64 { } // GetItem returns the item stored in the cache -func (c *LRUCache) GetItem(key string) (item CacheItem, ok bool) { +func (c *LRUCache) GetItem(key string) (item *CacheItem, ok bool) { if ele, hit := c.cache[key]; hit { - entry := ele.Value.(CacheItem) + entry := ele.Value.(*CacheItem) now := MillisecondNow() // If the entry is invalidated @@ -150,7 +150,7 @@ func (c *LRUCache) removeOldest() { func (c *LRUCache) removeElement(e *list.Element) { c.ll.Remove(e) - kv := e.Value.(CacheItem) + kv := e.Value.(*CacheItem) delete(c.cache, kv.Key) atomic.StoreInt64(&c.cacheLen, int64(c.ll.Len())) } @@ -163,7 +163,7 @@ func (c *LRUCache) Size() int64 { // Update the expiration time for the key func (c *LRUCache) UpdateExpiration(key string, expireAt int64) bool { if ele, hit := c.cache[key]; hit { - entry := ele.Value.(CacheItem) + entry := ele.Value.(*CacheItem) entry.ExpireAt = expireAt return true } diff --git a/lrucache_test.go b/lrucache_test.go index 5cce7e71..a188e6d7 100644 --- a/lrucache_test.go +++ b/lrucache_test.go @@ -42,7 +42,7 @@ func TestLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -82,7 +82,7 @@ func TestLRUCache(t *testing.T) { const key = "foobar" // Add key. - item1 := gubernator.CacheItem{ + item1 := &gubernator.CacheItem{ Key: key, Value: "initial value", ExpireAt: expireAt, @@ -91,7 +91,7 @@ func TestLRUCache(t *testing.T) { require.False(t, exists1) // Update same key. - item2 := gubernator.CacheItem{ + item2 := &gubernator.CacheItem{ Key: key, Value: "new value", ExpireAt: expireAt, @@ -111,7 +111,7 @@ func TestLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -163,7 +163,7 @@ func TestLRUCache(t *testing.T) { for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -186,7 +186,7 @@ func TestLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -225,7 +225,7 @@ func TestLRUCache(t *testing.T) { for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -248,7 +248,7 @@ func TestLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -291,7 +291,7 @@ func TestLRUCache(t *testing.T) { for i := 0; i < iterations; i++ { // Add existing. key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -302,7 +302,7 @@ func TestLRUCache(t *testing.T) { // Add new. key2 := strconv.Itoa(rand.Intn(1000) + 20000) - item2 := gubernator.CacheItem{ + item2 := &gubernator.CacheItem{ Key: key2, Value: i, ExpireAt: expireAt, @@ -344,7 +344,7 @@ func BenchmarkLRUCache(b *testing.B) { // Populate cache. for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -373,7 +373,7 @@ func BenchmarkLRUCache(b *testing.B) { for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -391,7 +391,7 @@ func BenchmarkLRUCache(b *testing.B) { // Populate cache. for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -437,7 +437,7 @@ func BenchmarkLRUCache(b *testing.B) { defer doneWg.Done() launchWg.Wait() - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -463,7 +463,7 @@ func BenchmarkLRUCache(b *testing.B) { // Populate cache. for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -489,7 +489,7 @@ func BenchmarkLRUCache(b *testing.B) { defer doneWg.Done() launchWg.Wait() - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -530,7 +530,7 @@ func BenchmarkLRUCache(b *testing.B) { launchWg.Wait() key := "z" + strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, diff --git a/mock_cache_test.go b/mock_cache_test.go index c1d0645c..4a5fd9ad 100644 --- a/mock_cache_test.go +++ b/mock_cache_test.go @@ -29,7 +29,7 @@ type MockCache struct { var _ guber.Cache = &MockCache{} -func (m *MockCache) Add(item guber.CacheItem) bool { +func (m *MockCache) Add(item *guber.CacheItem) bool { args := m.Called(item) return args.Bool(0) } @@ -39,19 +39,19 @@ func (m *MockCache) UpdateExpiration(key string, expireAt int64) bool { return args.Bool(0) } -func (m *MockCache) GetItem(key string) (value guber.CacheItem, ok bool) { +func (m *MockCache) GetItem(key string) (value *guber.CacheItem, ok bool) { args := m.Called(key) - var retval guber.CacheItem - if retval2, ok := args.Get(0).(guber.CacheItem); ok { + var retval *guber.CacheItem + if retval2, ok := args.Get(0).(*guber.CacheItem); ok { retval = retval2 } return retval, args.Bool(1) } -func (m *MockCache) Each() chan guber.CacheItem { +func (m *MockCache) Each() chan *guber.CacheItem { args := m.Called() - var retval chan guber.CacheItem - if retval2, ok := args.Get(0).(chan guber.CacheItem); ok { + var retval chan *guber.CacheItem + if retval2, ok := args.Get(0).(chan *guber.CacheItem); ok { retval = retval2 } return retval diff --git a/mock_loader_test.go b/mock_loader_test.go index 2cf428e2..220ba5c0 100644 --- a/mock_loader_test.go +++ b/mock_loader_test.go @@ -29,16 +29,16 @@ type MockLoader2 struct { var _ guber.Loader = &MockLoader2{} -func (m *MockLoader2) Load() (chan guber.CacheItem, error) { +func (m *MockLoader2) Load() (chan *guber.CacheItem, error) { args := m.Called() - var retval chan guber.CacheItem - if retval2, ok := args.Get(0).(chan guber.CacheItem); ok { + var retval chan *guber.CacheItem + if retval2, ok := args.Get(0).(chan *guber.CacheItem); ok { retval = retval2 } return retval, args.Error(1) } -func (m *MockLoader2) Save(ch chan guber.CacheItem) error { +func (m *MockLoader2) Save(ch chan *guber.CacheItem) error { args := m.Called(ch) return args.Error(0) } diff --git a/mock_store_test.go b/mock_store_test.go index ba7fa82c..a5f5fab2 100644 --- a/mock_store_test.go +++ b/mock_store_test.go @@ -29,14 +29,14 @@ type MockStore2 struct { var _ guber.Store = &MockStore2{} -func (m *MockStore2) OnChange(r *guber.RateLimitReq, item guber.CacheItem) { +func (m *MockStore2) OnChange(r *guber.RateLimitReq, item *guber.CacheItem) { m.Called(r, item) } -func (m *MockStore2) Get(r *guber.RateLimitReq) (guber.CacheItem, bool) { +func (m *MockStore2) Get(r *guber.RateLimitReq) (*guber.CacheItem, bool) { args := m.Called(r) - var retval guber.CacheItem - if retval2, ok := args.Get(0).(guber.CacheItem); ok { + var retval *guber.CacheItem + if retval2, ok := args.Get(0).(*guber.CacheItem); ok { retval = retval2 } return retval, args.Bool(1) diff --git a/store.go b/store.go index d4b6c7f3..2c754279 100644 --- a/store.go +++ b/store.go @@ -48,12 +48,12 @@ type Store interface { // decide if this rate limit item should be persisted in the store. It's up to the // store to expire old rate limit items. The CacheItem represents the current state of // the rate limit item *after* the RateLimitReq has been applied. - OnChange(r *RateLimitReq, item CacheItem) + OnChange(r *RateLimitReq, item *CacheItem) // Called by gubernator when a rate limit is missing from the cache. It's up to the store // to decide if this request is fulfilled. Should return true if the request is fulfilled // and false if the request is not fulfilled or doesn't exist in the store. - Get(r *RateLimitReq) (CacheItem, bool) + Get(r *RateLimitReq) (*CacheItem, bool) // Called by gubernator when an existing rate limit should be removed from the store. // NOTE: This is NOT called when an rate limit expires from the cache, store implementors @@ -67,17 +67,17 @@ type Loader interface { // Load is called by gubernator just before the instance is ready to accept requests. The implementation // should return a channel gubernator can read to load all rate limits that should be loaded into the // instance cache. The implementation should close the channel to indicate no more rate limits left to load. - Load() (chan CacheItem, error) + Load() (chan *CacheItem, error) // Save is called by gubernator just before the instance is shutdown. The passed channel should be // read until the channel is closed. - Save(chan CacheItem) error + Save(chan *CacheItem) error } func NewMockStore() *MockStore { ml := &MockStore{ Called: make(map[string]int), - CacheItems: make(map[string]CacheItem), + CacheItems: make(map[string]*CacheItem), } ml.Called["OnChange()"] = 0 ml.Called["Remove()"] = 0 @@ -87,17 +87,17 @@ func NewMockStore() *MockStore { type MockStore struct { Called map[string]int - CacheItems map[string]CacheItem + CacheItems map[string]*CacheItem } var _ Store = &MockStore{} -func (ms *MockStore) OnChange(r *RateLimitReq, item CacheItem) { +func (ms *MockStore) OnChange(r *RateLimitReq, item *CacheItem) { ms.Called["OnChange()"] += 1 ms.CacheItems[item.Key] = item } -func (ms *MockStore) Get(r *RateLimitReq) (CacheItem, bool) { +func (ms *MockStore) Get(r *RateLimitReq) (*CacheItem, bool) { ms.Called["Get()"] += 1 item, ok := ms.CacheItems[r.HashKey()] return item, ok @@ -119,15 +119,15 @@ func NewMockLoader() *MockLoader { type MockLoader struct { Called map[string]int - CacheItems []CacheItem + CacheItems []*CacheItem } var _ Loader = &MockLoader{} -func (ml *MockLoader) Load() (chan CacheItem, error) { +func (ml *MockLoader) Load() (chan *CacheItem, error) { ml.Called["Load()"] += 1 - ch := make(chan CacheItem, 10) + ch := make(chan *CacheItem, 10) go func() { for _, i := range ml.CacheItems { ch <- i @@ -137,7 +137,7 @@ func (ml *MockLoader) Load() (chan CacheItem, error) { return ch, nil } -func (ml *MockLoader) Save(in chan CacheItem) error { +func (ml *MockLoader) Save(in chan *CacheItem) error { ml.Called["Save()"] += 1 for i := range in { diff --git a/store_test.go b/store_test.go index ee25cbdc..e94db599 100644 --- a/store_test.go +++ b/store_test.go @@ -161,7 +161,7 @@ func TestStore(t *testing.T) { matchItem := func(algorithm gubernator.Algorithm, req *gubernator.RateLimitReq) interface{} { switch algorithm { case gubernator.Algorithm_TOKEN_BUCKET: - return mock.MatchedBy(func(item gubernator.CacheItem) bool { + return mock.MatchedBy(func(item *gubernator.CacheItem) bool { titem, ok := item.Value.(*gubernator.TokenBucketItem) if !ok { return false @@ -174,7 +174,7 @@ func TestStore(t *testing.T) { }) case gubernator.Algorithm_LEAKY_BUCKET: - return mock.MatchedBy(func(item gubernator.CacheItem) bool { + return mock.MatchedBy(func(item *gubernator.CacheItem) bool { litem, ok := item.Value.(*gubernator.LeakyBucketItem) if !ok { return false @@ -240,7 +240,7 @@ func TestStore(t *testing.T) { } // Setup mocks. - store.On("Get", matchReq(req)).Once().Return(gubernator.CacheItem{}, false) + store.On("Get", matchReq(req)).Once().Return(nil, false) store.On("OnChange", matchReq(req), matchItem(testCase.Algorithm, req)).Once() // Call code. @@ -287,7 +287,7 @@ func TestStore(t *testing.T) { // Setup mocks. now := gubernator.MillisecondNow() expire := now + req.Duration - storedItem := gubernator.CacheItem{ + storedItem := &gubernator.CacheItem{ Algorithm: req.Algorithm, ExpireAt: expire, Key: req.HashKey(), @@ -326,7 +326,7 @@ func TestStore(t *testing.T) { // Setup mocks. now := gubernator.MillisecondNow() expire := now + req.Duration - storedItem := gubernator.CacheItem{ + storedItem := &gubernator.CacheItem{ Algorithm: req.Algorithm, ExpireAt: expire, Key: req.HashKey(), @@ -379,7 +379,7 @@ func TestStore(t *testing.T) { case gubernator.Algorithm_LEAKY_BUCKET: bucketItem.(*gubernator.LeakyBucketItem).Duration = oldDuration } - storedItem := gubernator.CacheItem{ + storedItem := &gubernator.CacheItem{ Algorithm: req.Algorithm, ExpireAt: oldExpire, Key: req.HashKey(), @@ -390,7 +390,7 @@ func TestStore(t *testing.T) { store.On("OnChange", matchReq(req), - mock.MatchedBy(func(item gubernator.CacheItem) bool { + mock.MatchedBy(func(item *gubernator.CacheItem) bool { switch req.Algorithm { case gubernator.Algorithm_TOKEN_BUCKET: titem, ok := item.Value.(*gubernator.TokenBucketItem) @@ -468,7 +468,7 @@ func TestStore(t *testing.T) { bucketItem.(*gubernator.LeakyBucketItem).Duration = oldDuration bucketItem.(*gubernator.LeakyBucketItem).UpdatedAt = longTimeAgo } - storedItem := gubernator.CacheItem{ + storedItem := &gubernator.CacheItem{ Algorithm: req.Algorithm, ExpireAt: oldExpire, Key: req.HashKey(), @@ -479,7 +479,7 @@ func TestStore(t *testing.T) { store.On("OnChange", matchReq(req), - mock.MatchedBy(func(item gubernator.CacheItem) bool { + mock.MatchedBy(func(item *gubernator.CacheItem) bool { switch req.Algorithm { case gubernator.Algorithm_TOKEN_BUCKET: titem, ok := item.Value.(*gubernator.TokenBucketItem) @@ -529,7 +529,7 @@ func TestStore(t *testing.T) { } } -func getRemaining(item gubernator.CacheItem) int64 { +func getRemaining(item *gubernator.CacheItem) int64 { switch item.Algorithm { case gubernator.Algorithm_TOKEN_BUCKET: return item.Value.(*gubernator.TokenBucketItem).Remaining diff --git a/sync_lrucache.go b/sync_lrucache.go index 1f10a298..3e20bb88 100644 --- a/sync_lrucache.go +++ b/sync_lrucache.go @@ -34,7 +34,7 @@ type SyncLRUCache struct { } type syncLRUCacheAddRequest struct { - Item CacheItem + Item *CacheItem } type syncLRUCacheAddResponse struct { @@ -55,14 +55,14 @@ type syncLRUCacheGetItemRequest struct { } type syncLRUCacheGetItemResponse struct { - Item CacheItem + Item *CacheItem Ok bool } type syncLRUCacheEachRequest struct{} type syncLRUCacheEachResponse struct { - Each chan CacheItem + Each chan *CacheItem } type syncLRUCacheRemoveRequest struct { @@ -129,7 +129,7 @@ func (c *SyncLRUCache) listen() { } } -func (c *SyncLRUCache) Add(item CacheItem) bool { +func (c *SyncLRUCache) Add(item *CacheItem) bool { c.addRequest <- syncLRUCacheAddRequest{Item: item} response := <-c.addResponse return response.Exists @@ -141,13 +141,13 @@ func (c *SyncLRUCache) UpdateExpiration(key string, expireAt int64) bool { return response.Ok } -func (c *SyncLRUCache) GetItem(key string) (CacheItem, bool) { +func (c *SyncLRUCache) GetItem(key string) (*CacheItem, bool) { c.getItemRequest <- syncLRUCacheGetItemRequest{Key: key} response := <-c.getItemResponse return response.Item, response.Ok } -func (c *SyncLRUCache) Each() chan CacheItem { +func (c *SyncLRUCache) Each() chan *CacheItem { c.eachRequest <- syncLRUCacheEachRequest{} response := <-c.eachResponse return response.Each diff --git a/sync_lrucache_test.go b/sync_lrucache_test.go index 5da5f660..f3621637 100644 --- a/sync_lrucache_test.go +++ b/sync_lrucache_test.go @@ -40,7 +40,7 @@ func TestSyncLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -77,7 +77,7 @@ func TestSyncLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -128,7 +128,7 @@ func TestSyncLRUCache(t *testing.T) { for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -151,7 +151,7 @@ func TestSyncLRUCache(t *testing.T) { // Populate cache. for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -186,7 +186,7 @@ func TestSyncLRUCache(t *testing.T) { for i := 0; i < iterations; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -211,7 +211,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { // Populate cache. for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -239,7 +239,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -256,7 +256,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { // Populate cache. for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -301,7 +301,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { defer doneWg.Done() launchWg.Wait() - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -326,7 +326,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { // Populate cache. for i := 0; i < b.N; i++ { key := strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -350,7 +350,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { defer doneWg.Done() launchWg.Wait() - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt, @@ -388,7 +388,7 @@ func BenchmarkSyncLRUCache(b *testing.B) { launchWg.Wait() key := "z" + strconv.Itoa(i) - item := gubernator.CacheItem{ + item := &gubernator.CacheItem{ Key: key, Value: i, ExpireAt: expireAt,