Skip to content

Commit

Permalink
fix: prevent cache size underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill committed Feb 6, 2025
1 parent cda61e5 commit d56ae07
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/storer/cachestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"fmt"
"time"

storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
Expand Down Expand Up @@ -39,7 +39,7 @@ func (db *DB) cacheWorker(ctx context.Context) {
continue
}

evict := size - capc
evict := uint64(size - capc)
if evict < db.reserveOptions.cacheMinEvictCount { // evict at least a min count
evict = db.reserveOptions.cacheMinEvictCount
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/storer/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"sync/atomic"
"time"

storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -61,12 +61,14 @@ func New(ctx context.Context, store storage.Reader, capacity uint64) (*Cache, er
}

// Size returns the current size of the cache.
func (c *Cache) Size() uint64 {
return uint64(c.size.Load())
func (c *Cache) Size() int64 {
return c.size.Load()
}

// Capacity returns the capacity of the cache.
func (c *Cache) Capacity() uint64 { return uint64(c.capacity) }
func (c *Cache) Capacity() int64 {
return int64(c.capacity)
}

// Putter returns a Storage.Putter instance which adds the chunk to the underlying
// chunkstore and also adds a Cache entry for the chunk.
Expand Down
4 changes: 2 additions & 2 deletions pkg/storer/internal/cache/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"time"

storage "github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
Expand Down Expand Up @@ -43,7 +43,7 @@ func (c *Cache) RemoveOldestMaxBatch(ctx context.Context, st transaction.Storage

func (c *Cache) State(store storage.Reader) CacheState {
state := CacheState{}
state.Size = c.Size()
state.Size = uint64(c.Size())
runner := swarm.ZeroAddress

err := store.Iterate(
Expand Down

0 comments on commit d56ae07

Please sign in to comment.