Skip to content

Commit

Permalink
Work around bug in bigcache. (#259)
Browse files Browse the repository at this point in the history
Bigcache will use arbitrary amounts of memory if HardMaxCacheSize is less than the number of shards.

Work around this by setting max size to the number of shards if it's too small. This means we can't have a cache smaller than 4MB.
  • Loading branch information
ggreer authored Dec 10, 2024
1 parent 85e3d00 commit 30e11dc
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/uhttp/gocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func NewGoCache(ctx context.Context, cfg CacheConfig) (*GoCache, error) {
config := bigCache.DefaultConfig(time.Duration(cfg.TTL) * time.Second)
config.Verbose = cfg.LogDebug
config.Shards = 4
if cfg.MaxSize > 0 && cfg.MaxSize < config.Shards {
// BigCache's config.maximumShardSizeInBytes does integer division, which returns zero if there are more shards than megabytes.
// Zero means unlimited cache size on each shard, so max size is effectively ignored.
// Work around this bug by increasing the max size to the number of shards. (4, so 4MB)
cfg.MaxSize = config.Shards
}
config.HardMaxCacheSize = cfg.MaxSize // value in MB, 0 value means no size limit
cache, err := bigCache.New(ctx, config)
if err != nil {
Expand Down

0 comments on commit 30e11dc

Please sign in to comment.