π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Reuse limiter memory cache
Manav Rathi6250cb8f2bb3
diff --git a/server/pkg/utils/rate_limit_store.go b/server/pkg/utils/rate_limit_store.goindex 9d47e39b39..d5c487039a 100644--- a/server/pkg/utils/rate_limit_store.go+++ b/server/pkg/utils/rate_limit_store.go@@ -2,100 +2,47 @@ package utilsimport ("context"- "sync""time""github.com/ulule/limiter/v3""github.com/ulule/limiter/v3/drivers/store/common"+ "github.com/ulule/limiter/v3/drivers/store/memory")-// memoryLimiterStore keeps rate-limit keys as owned strings. This is needed-// because the limiter/v3 memory driver stores keys derived from pooled byte-// buffers, which can mutate after insertion and break sync.Map's hash invariant.+// memoryLimiterStore keeps rate-limit keys as owned strings while reusing+// limiter/v3's in-memory cache. This is needed because the limiter/v3 memory+// driver stores keys derived from pooled byte buffers, which can mutate after+// insertion and break sync.Map's hash invariant.type memoryLimiterStore struct {- mu sync.Mutex- prefix string- counters map[string]memoryLimiterCounter-}--type memoryLimiterCounter struct {- value int64- expiration time.Time+ prefix string+ cache *memory.CacheWrapper}func newMemoryLimiterStore() *memoryLimiterStore {- store := &memoryLimiterStore{- prefix: limiter.DefaultPrefix,- counters: make(map[string]memoryLimiterCounter),+ return &memoryLimiterStore{+ prefix: limiter.DefaultPrefix,+ cache: memory.NewCache(limiter.DefaultCleanUpInterval),}- go store.deleteExpiredCounters()- return store}func (store *memoryLimiterStore) Get(_ context.Context, key string, rate limiter.Rate) (limiter.Context, error) {now := time.Now()- expiration := now.Add(rate.Period)- cacheKey := store.cacheKey(key)-- store.mu.Lock()- defer store.mu.Unlock()-- counter := store.counters[cacheKey]- if !counter.expiration.After(now) {- counter.value = 1- counter.expiration = expiration- } else {- counter.value++- }- store.counters[cacheKey] = counter- return common.GetContextFromState(now, rate, counter.expiration, counter.value), nil+ count, expiration := store.cache.Increment(store.cacheKey(key), 1, rate.Period)+ return common.GetContextFromState(now, rate, expiration, count), nil}func (store *memoryLimiterStore) Peek(_ context.Context, key string, rate limiter.Rate) (limiter.Context, error) {now := time.Now()- expiration := now.Add(rate.Period)- count := int64(0)- cacheKey := store.cacheKey(key)-- store.mu.Lock()- defer store.mu.Unlock()-- if counter, ok := store.counters[cacheKey]; ok && counter.expiration.After(now) {- count = counter.value- expiration = counter.expiration- }+ count, expiration := store.cache.Get(store.cacheKey(key), rate.Period)return common.GetContextFromState(now, rate, expiration, count), nil}func (store *memoryLimiterStore) Reset(_ context.Context, key string, rate limiter.Rate) (limiter.Context, error) {now := time.Now()- expiration := now.Add(rate.Period)-- store.mu.Lock()- defer store.mu.Unlock()-- delete(store.counters, store.cacheKey(key))+ _, expiration := store.cache.Reset(store.cacheKey(key), rate.Period)return common.GetContextFromState(now, rate, expiration, 0), nil}func (store *memoryLimiterStore) cacheKey(key string) string {return store.prefix + ":" + key}--func (store *memoryLimiterStore) deleteExpiredCounters() {- ticker := time.NewTicker(limiter.DefaultCleanUpInterval)- for now := range ticker.C {- store.deleteExpiredCountersAt(now)- }-}--func (store *memoryLimiterStore) deleteExpiredCountersAt(now time.Time) {- store.mu.Lock()- defer store.mu.Unlock()-- for key, counter := range store.counters {- if !counter.expiration.After(now) {- delete(store.counters, key)- }- }-}diff --git a/server/pkg/utils/rate_limit_store_test.go b/server/pkg/utils/rate_limit_store_test.goindex dbd4702f07..7235b0d890 100644--- a/server/pkg/utils/rate_limit_store_test.go+++ b/server/pkg/utils/rate_limit_store_test.go@@ -3,7 +3,6 @@ package utilsimport ("context""testing"- "time""unsafe""github.com/ulule/limiter/v3"@@ -68,29 +67,3 @@ func TestNewRateLimiterUsesSafeStore(t *testing.T) {t.Fatalf("third request should be rate limited: %+v", third)}}--func TestMemoryLimiterStoreDeletesExpiredCounters(t *testing.T) {- store := &memoryLimiterStore{- prefix: limiter.DefaultPrefix,- counters: make(map[string]memoryLimiterCounter),- }- now := time.Now()-- store.counters[store.cacheKey("expired")] = memoryLimiterCounter{- value: 1,- expiration: now.Add(-time.Second),- }- store.counters[store.cacheKey("active")] = memoryLimiterCounter{- value: 1,- expiration: now.Add(time.Second),- }-- store.deleteExpiredCountersAt(now)-- if _, ok := store.counters[store.cacheKey("expired")]; ok {- t.Fatal("expired counter was not deleted")- }- if _, ok := store.counters[store.cacheKey("active")]; !ok {- t.Fatal("active counter was deleted")- }-}