Skip to content

fix: runtime panic "index out of range" caused by nextCleanupBucket #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2024
Merged
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
8 changes: 6 additions & 2 deletions sieve/sieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/scalalang2/golang-fifo/types"
)

// numberOfBuckets is the number of buckets to store the cache entries
//
// Notice: if this number exceeds 256, the type of nextCleanupBucket
// in the Sieve struct should be changed to int16
const numberOfBuckets = 100

// entry holds the key and value of a cache entry.
Expand Down Expand Up @@ -249,8 +253,8 @@ func (s *Sieve[K, V]) addToBucket(e *entry[K, V]) {
if s.ttl == 0 {
return
}
bucketId := (numberOfBuckets + s.nextCleanupBucket - 1) % numberOfBuckets
e.bucketID = bucketId
bucketId := (numberOfBuckets + int(s.nextCleanupBucket) - 1) % numberOfBuckets
e.bucketID = int8(bucketId)
s.buckets[bucketId].entries[e.key] = e
if s.buckets[bucketId].newestEntry.Before(e.expiredAt) {
s.buckets[bucketId].newestEntry = e.expiredAt
Expand Down
4 changes: 2 additions & 2 deletions sieve/sieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func TestLargerWorkloadsThanCacheSize(t *testing.T) {
bytes []byte
}

cache := New[int32, value](128, 0)
workload := int32(256)
cache := New[int32, value](512, time.Millisecond)
workload := int32(10240)
for i := int32(0); i < workload; i++ {
val := value{
bytes: make([]byte, 10),
Expand Down
Loading