From 6506b62bff603bac3eb29a3321ad71ce267986a2 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Wed, 14 Jun 2023 10:40:44 +0000 Subject: [PATCH] chore(cache): max entries type `int` -> `uint` --- internal/config/settings/lru.go | 11 +---------- internal/config/sources/env/cache.go | 21 +++++++++++++++------ pkg/cache/lru/lru.go | 4 ++-- pkg/cache/lru/mock_metrics_test.go | 2 +- pkg/cache/lru/settings.go | 2 +- pkg/cache/lru/settings_test.go | 2 +- pkg/cache/metrics/interface.go | 2 +- pkg/cache/metrics/noop/metrics.go | 20 ++++++++++---------- pkg/cache/metrics/prometheus/gauges.go | 2 +- 9 files changed, 33 insertions(+), 33 deletions(-) diff --git a/internal/config/settings/lru.go b/internal/config/settings/lru.go index fb45539c..d5d16d26 100644 --- a/internal/config/settings/lru.go +++ b/internal/config/settings/lru.go @@ -1,9 +1,6 @@ package settings import ( - "errors" - "fmt" - "github.com/qdm12/gosettings" "github.com/qdm12/gotree" ) @@ -12,7 +9,7 @@ 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() { @@ -20,13 +17,7 @@ func (c *CacheLRU) setDefaults() { 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 } diff --git a/internal/config/sources/env/cache.go b/internal/config/sources/env/cache.go index 0de2e25b..eb872b14 100644 --- a/internal/config/sources/env/cache.go +++ b/internal/config/sources/env/cache.go @@ -3,6 +3,7 @@ package env import ( "errors" "fmt" + "math" "os" "strconv" "strings" @@ -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 } diff --git a/pkg/cache/lru/lru.go b/pkg/cache/lru/lru.go index ea7c0bb7..f65ea354 100644 --- a/pkg/cache/lru/lru.go +++ b/pkg/cache/lru/lru.go @@ -11,7 +11,7 @@ import ( type LRU struct { // Configuration - maxEntries int + maxEntries uint // State kv map[string]*list.Element @@ -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() } diff --git a/pkg/cache/lru/mock_metrics_test.go b/pkg/cache/lru/mock_metrics_test.go index 3e39d43d..2c35a199 100644 --- a/pkg/cache/lru/mock_metrics_test.go +++ b/pkg/cache/lru/mock_metrics_test.go @@ -94,7 +94,7 @@ func (mr *MockMetricsMockRecorder) CacheInsertInc() *gomock.Call { } // CacheMaxEntriesSet mocks base method. -func (m *MockMetrics) CacheMaxEntriesSet(arg0 int) { +func (m *MockMetrics) CacheMaxEntriesSet(arg0 uint) { m.ctrl.T.Helper() m.ctrl.Call(m, "CacheMaxEntriesSet", arg0) } diff --git a/pkg/cache/lru/settings.go b/pkg/cache/lru/settings.go index 423c9ea3..a70faf42 100644 --- a/pkg/cache/lru/settings.go +++ b/pkg/cache/lru/settings.go @@ -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 diff --git a/pkg/cache/lru/settings_test.go b/pkg/cache/lru/settings_test.go index f5c24680..03ade166 100644 --- a/pkg/cache/lru/settings_test.go +++ b/pkg/cache/lru/settings_test.go @@ -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) } diff --git a/pkg/cache/metrics/interface.go b/pkg/cache/metrics/interface.go index ba6dc48f..d766187d 100644 --- a/pkg/cache/metrics/interface.go +++ b/pkg/cache/metrics/interface.go @@ -23,7 +23,7 @@ type Interface interface { CacheHitInc() CacheMissInc() CacheExpiredInc() - CacheMaxEntriesSet(maxEntries int) + CacheMaxEntriesSet(maxEntries uint) } type Settings struct { diff --git a/pkg/cache/metrics/noop/metrics.go b/pkg/cache/metrics/noop/metrics.go index 134ce436..20818e0f 100644 --- a/pkg/cache/metrics/noop/metrics.go +++ b/pkg/cache/metrics/noop/metrics.go @@ -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) {} diff --git a/pkg/cache/metrics/prometheus/gauges.go b/pkg/cache/metrics/prometheus/gauges.go index b09d7886..c2e42695 100644 --- a/pkg/cache/metrics/prometheus/gauges.go +++ b/pkg/cache/metrics/prometheus/gauges.go @@ -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)) }