Skip to content

Commit 59489b8

Browse files
costelascalalang2
authored andcommitted
fix: be stricter when initializing
A size < 0 is not valid and causes runtime panics when setting items. Panicking earlier with a clearer message avoids more subtle erroneous usage.
1 parent 961982c commit 59489b8

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

s3fifo/s3fifo.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ type S3FIFO[K comparable, V any] struct {
4444
buckets []bucket[K, V]
4545

4646
// ttl is the time to live of the cache entry
47-
ttl time.Duration
48-
ttlEnabled bool
47+
ttl time.Duration
4948

5049
// nextCleanupBucket is an index of the next bucket to be cleaned up
5150
nextCleanupBucket int8

sieve/sieve.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ type Sieve[K comparable, V any] struct {
4343
buckets []bucket[K, V]
4444

4545
// ttl is the time to live of the cache entry
46-
ttl time.Duration
47-
ttlEnabled bool
46+
ttl time.Duration
4847

4948
// nextCleanupBucket is an index of the next bucket to be cleaned up
5049
nextCleanupBucket int8
@@ -62,6 +61,10 @@ func New[K comparable, V any](size int, ttl time.Duration) *Sieve[K, V] {
6261
ttl = 0
6362
}
6463

64+
if size <= 0 {
65+
panic("sieve: size must be greater than 0")
66+
}
67+
6568
cache := &Sieve[K, V]{
6669
ctx: ctx,
6770
cancel: cancel,

0 commit comments

Comments
 (0)