Skip to content

Commit

Permalink
fix: prober cache returns StatusUnknown by default (#4052)
Browse files Browse the repository at this point in the history
Signed-off-by: Calum Murray <[email protected]>
Co-authored-by: Calum Murray <[email protected]>
  • Loading branch information
knative-prow-robot and Cali0707 authored Aug 13, 2024
1 parent 5a06cc5 commit 9947a87
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion control-plane/pkg/prober/async_prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
21 changes: 14 additions & 7 deletions control-plane/pkg/prober/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()

Expand All @@ -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

}

Expand Down
11 changes: 11 additions & 0 deletions control-plane/pkg/prober/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9947a87

Please sign in to comment.