Skip to content

Commit

Permalink
chore(cache): max entries type int -> uint
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jun 14, 2023
1 parent 6bd785f commit 6506b62
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 33 deletions.
11 changes: 1 addition & 10 deletions internal/config/settings/lru.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package settings

import (
"errors"
"fmt"

"github.com/qdm12/gosettings"
"github.com/qdm12/gotree"
)
Expand All @@ -12,21 +9,15 @@ type CacheLRU struct {
// MaxEntries is the number of maximum entries
// to keep in the LRU cache. It defaults to 10e4
// if letf unset.
MaxEntries int
MaxEntries uint
}

func (c *CacheLRU) setDefaults() {
const defaultMaxEntries = 10e4
c.MaxEntries = gosettings.DefaultNumber(c.MaxEntries, defaultMaxEntries)
}

var ErrMaxEntriesNegative = errors.New("max entries must be positive")

func (c *CacheLRU) validate() error {
if c.MaxEntries < 0 {
return fmt.Errorf("%w: %d", ErrMaxEntriesNegative, c.MaxEntries)
}

return nil
}

Expand Down
21 changes: 15 additions & 6 deletions internal/config/sources/env/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package env
import (
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
Expand All @@ -23,18 +24,26 @@ func readCache() (settings settings.Cache, err error) {

var ErrCacheLRUMaxEntries = errors.New("invalid value for max entries of the LRU cache")

func getLRUCacheMaxEntries() (maxEntries int, err error) {
func getLRUCacheMaxEntries() (maxEntries uint, err error) {
s := os.Getenv("CACHE_LRU_MAX_ENTRIES")
if s == "" {
return 0, nil
}

maxEntries, err = strconv.Atoi(s)
if err != nil {
const base, bits = 10, 64
maxEntriesUint64, err := strconv.ParseUint(s, base, bits)
switch {
case err != nil:
return 0, fmt.Errorf("environment variable CACHE_LRU_MAX_ENTRIES: %w", err)
} else if maxEntries < 1 {
return 0, fmt.Errorf("%w: must be strictly positive: %d",
ErrCacheLRUMaxEntries, maxEntries)
case maxEntries == 0:
return 0, fmt.Errorf("%w: cannot be zero", ErrCacheLRUMaxEntries)
case maxEntries > math.MaxInt:
// down the call stack, maxEntries is converted to int
// for a map size, and the max int depends on the platform.
return 0, fmt.Errorf("%w: %s must be less than %d",
ErrCacheLRUMaxEntries, s, math.MaxInt)
}

maxEntries = uint(maxEntriesUint64)
return maxEntries, nil
}
4 changes: 2 additions & 2 deletions pkg/cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type LRU struct {
// Configuration
maxEntries int
maxEntries uint

// State
kv map[string]*list.Element
Expand Down Expand Up @@ -70,7 +70,7 @@ func (l *LRU) Add(request, response *dns.Msg) {
response: responseCopy,
}

if l.maxEntries > 0 && l.linkedList.Len() == l.maxEntries {
if l.maxEntries > 0 && l.linkedList.Len() == int(l.maxEntries) {
l.removeOldest()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/lru/mock_metrics_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/cache/lru/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type Settings struct {
// MaxEntries is the maximum number of request<->response pairs
// to be stored in the cache. It defaults to 10e4 if left unset.
MaxEntries int
MaxEntries uint
// Metrics is the metrics interface to record metric information
// for the cache. It defaults to a No-Op metric implementation.
Metrics metrics.Interface
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/lru/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ func Test_Settings_SetDefaults(t *testing.T) {
settings := Settings{}
settings.SetDefaults()

assert.Greater(t, settings.MaxEntries, 1)
assert.Greater(t, settings.MaxEntries, uint(1))
assert.NotNil(t, settings.Metrics)
}
2 changes: 1 addition & 1 deletion pkg/cache/metrics/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Interface interface {
CacheHitInc()
CacheMissInc()
CacheExpiredInc()
CacheMaxEntriesSet(maxEntries int)
CacheMaxEntriesSet(maxEntries uint)
}

type Settings struct {
Expand Down
20 changes: 10 additions & 10 deletions pkg/cache/metrics/noop/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ func New() (metrics *Metrics) {
return new(Metrics)
}

func (m *Metrics) SetCacheType(string) {}
func (m *Metrics) CacheInsertInc() {}
func (m *Metrics) CacheRemoveInc() {}
func (m *Metrics) CacheMoveInc() {}
func (m *Metrics) CacheGetEmptyInc() {}
func (m *Metrics) CacheInsertEmptyInc() {}
func (m *Metrics) CacheHitInc() {}
func (m *Metrics) CacheMissInc() {}
func (m *Metrics) CacheExpiredInc() {}
func (m *Metrics) CacheMaxEntriesSet(int) {}
func (m *Metrics) SetCacheType(string) {}
func (m *Metrics) CacheInsertInc() {}
func (m *Metrics) CacheRemoveInc() {}
func (m *Metrics) CacheMoveInc() {}
func (m *Metrics) CacheGetEmptyInc() {}
func (m *Metrics) CacheInsertEmptyInc() {}
func (m *Metrics) CacheHitInc() {}
func (m *Metrics) CacheMissInc() {}
func (m *Metrics) CacheExpiredInc() {}
func (m *Metrics) CacheMaxEntriesSet(uint) {}
2 changes: 1 addition & 1 deletion pkg/cache/metrics/prometheus/gauges.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func newGauges(settings prom.Settings) (g *gauges, err error) {
return g, nil
}

func (g *gauges) CacheMaxEntriesSet(maxEntries int) {
func (g *gauges) CacheMaxEntriesSet(maxEntries uint) {
g.maxEntries.Set(float64(maxEntries))
}

0 comments on commit 6506b62

Please sign in to comment.