Skip to content

Commit ef5cb24

Browse files
Laiskyscalalang2
authored andcommitted
fix: runtime error index out of range caused by nextCleanupBucket
- Adjusted cache size and TTL in `sieve_test.go` for more comprehensive testing - Implemented a constant `numberOfBuckets` and utilized it in `sieve.go` for cache storage - Updated `addToBucket` function to incorporate the new `numberOfBuckets` constant and `int8` `bucketID` type - Modified `bucketId` calculation in `addToBucket` function using `s.nextCleanupBucket` of type `float64` - Enhanced workload for testing in `sieve_test.go` from 256 to 10240 for rigorous testing
1 parent 75f0941 commit ef5cb24

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

sieve/sieve.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"github.com/scalalang2/golang-fifo/types"
1010
)
1111

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

1418
// entry holds the key and value of a cache entry.
@@ -249,8 +253,8 @@ func (s *Sieve[K, V]) addToBucket(e *entry[K, V]) {
249253
if s.ttl == 0 {
250254
return
251255
}
252-
bucketId := (numberOfBuckets + s.nextCleanupBucket - 1) % numberOfBuckets
253-
e.bucketID = bucketId
256+
bucketId := (numberOfBuckets + int(s.nextCleanupBucket) - 1) % numberOfBuckets
257+
e.bucketID = int8(bucketId)
254258
s.buckets[bucketId].entries[e.key] = e
255259
if s.buckets[bucketId].newestEntry.Before(e.expiredAt) {
256260
s.buckets[bucketId].newestEntry = e.expiredAt

sieve/sieve_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ func TestLargerWorkloadsThanCacheSize(t *testing.T) {
205205
bytes []byte
206206
}
207207

208-
cache := New[int32, value](128, 0)
209-
workload := int32(256)
208+
cache := New[int32, value](512, time.Millisecond)
209+
workload := int32(10240)
210210
for i := int32(0); i < workload; i++ {
211211
val := value{
212212
bytes: make([]byte, 10),

0 commit comments

Comments
 (0)