Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ module github.com/neelp03/throttlex

go 1.21

require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
golang.org/x/net v0.28.0 // indirect
)

require (
github.com/go-redis/redis/v8 v8.11.5
golang.org/x/sys v0.24.0 // indirect
)
require ()

// new pre-release versions available

Expand Down
10 changes: 9 additions & 1 deletion store/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ func (s *MemoryStore) Increment(key string, delta int64, expiration time.Duratio

counter, exists := s.counters[key]
if !exists || time.Now().After(counter.expiration) {
// Start from zero when the counter doesn't exist or has expired
newCount := delta
if newCount < 0 {
newCount = 0
}
counter = &memoryCounter{
count: delta,
count: newCount,
expiration: time.Now().Add(expiration),
}
s.counters[key] = counter
} else {
counter.count += delta
if counter.count < 0 {
counter.count = 0
}
}
return counter.count, nil
}
Expand Down
14 changes: 14 additions & 0 deletions store/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ func TestMemoryStore_Increment(t *testing.T) {
}
}

func TestMemoryStore_IncrementNegativeNewKey(t *testing.T) {
memStore := NewMemoryStore()
key := "new_key"
expiration := time.Second

count, err := memStore.Increment(key, -1, expiration)
if err != nil {
t.Fatalf("Increment failed: %v", err)
}
if count != 0 {
t.Errorf("Expected count 0 for negative increment on new key, got %d", count)
}
}

func TestMemoryStore_AddTimestamp(t *testing.T) {
memStore := NewMemoryStore()
key := "test_key"
Expand Down
3 changes: 3 additions & 0 deletions store/redis.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package store

import (
Expand Down
3 changes: 3 additions & 0 deletions store/redis_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package store

import (
Expand Down