From 9947a87a1bcfa3af43d51e301f75f9c5d79c5714 Mon Sep 17 00:00:00 2001 From: Knative Prow Robot Date: Tue, 13 Aug 2024 20:42:44 +0100 Subject: [PATCH] fix: prober cache returns StatusUnknown by default (#4052) Signed-off-by: Calum Murray Co-authored-by: Calum Murray --- control-plane/pkg/prober/async_prober.go | 2 +- control-plane/pkg/prober/cache.go | 21 ++++++++++++++------- control-plane/pkg/prober/cache_test.go | 11 +++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/control-plane/pkg/prober/async_prober.go b/control-plane/pkg/prober/async_prober.go index bfac392a66..a54390a15c 100644 --- a/control-plane/pkg/prober/async_prober.go +++ b/control-plane/pkg/prober/async_prober.go @@ -56,7 +56,7 @@ func NewAsync(ctx context.Context, client httpClient, port string, IPsLister IPs client: client, enqueue: enqueue, logger: logger, - cache: NewLocalExpiringCache[string, Status, types.NamespacedName](ctx, cacheExpiryTime), + cache: NewLocalExpiringCacheWithDefault[string, Status, types.NamespacedName](ctx, cacheExpiryTime, StatusUnknown), IPsLister: IPsLister, port: port, } diff --git a/control-plane/pkg/prober/cache.go b/control-plane/pkg/prober/cache.go index 437bf82166..49ceb93402 100644 --- a/control-plane/pkg/prober/cache.go +++ b/control-plane/pkg/prober/cache.go @@ -75,6 +75,9 @@ type localExpiringCache[K comparable, V, A interface{}] struct { entries *list.List expiration time.Duration + + // defaultValue is the default value returned by Get + defaultValue V } type value[K comparable, V, A interface{}] struct { @@ -86,11 +89,17 @@ type value[K comparable, V, A interface{}] struct { } func NewLocalExpiringCache[K comparable, V, A interface{}](ctx context.Context, expiration time.Duration) Cache[K, V, A] { + var defaultValue V + return NewLocalExpiringCacheWithDefault[K, V, A](ctx, expiration, defaultValue) +} + +func NewLocalExpiringCacheWithDefault[K comparable, V, A interface{}](ctx context.Context, expiration time.Duration, defaultValue V) Cache[K, V, A] { c := &localExpiringCache[K, V, A]{ - mu: sync.RWMutex{}, - targets: make(map[K]*list.Element, 64), - entries: list.New().Init(), - expiration: expiration, + mu: sync.RWMutex{}, + targets: make(map[K]*list.Element, 64), + entries: list.New().Init(), + expiration: expiration, + defaultValue: defaultValue, } go func() { for { @@ -106,8 +115,6 @@ func NewLocalExpiringCache[K comparable, V, A interface{}](ctx context.Context, } func (c *localExpiringCache[K, V, A]) Get(key K) (V, bool) { - var defaultValue V - c.mu.RLock() defer c.mu.RUnlock() @@ -117,7 +124,7 @@ func (c *localExpiringCache[K, V, A]) Get(key K) (V, bool) { return value.value, true } } - return defaultValue, false + return c.defaultValue, false } diff --git a/control-plane/pkg/prober/cache_test.go b/control-plane/pkg/prober/cache_test.go index 1c17bce032..08a8cc7bf3 100644 --- a/control-plane/pkg/prober/cache_test.go +++ b/control-plane/pkg/prober/cache_test.go @@ -27,6 +27,17 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) +func TestInMemoryLocalCacheDefaults(t *testing.T) { + d := time.Second + ctx, cancel := context.WithTimeout(context.Background(), d*4) + defer cancel() + c := NewLocalExpiringCacheWithDefault[string, Status, int](ctx, d, StatusUnknown) + + v, ok := c.Get("unknown") + require.False(t, ok) + require.Equal(t, v, StatusUnknown) +} + func TestInMemoryLocalCache(t *testing.T) { d := time.Second ctx, cancel := context.WithTimeout(context.Background(), d*4)