From 8f3b1ef39ab51991fd3ddc77b2a832814eb3fec8 Mon Sep 17 00:00:00 2001 From: zohar7ch Date: Thu, 5 Jun 2025 13:19:19 +0300 Subject: [PATCH] Set TTL for reporting data to Otterize-cloud Even though the data does not necessarily changes, we want to report it every once-in-a-while, to make sure we get the updated status on Otterize cloud --- .../metrics_collection_traffic_cache.go | 9 ++++++--- src/mapper/pkg/webhook_traffic/webhook_services_cache.go | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/mapper/pkg/metrics_collection_traffic/metrics_collection_traffic_cache.go b/src/mapper/pkg/metrics_collection_traffic/metrics_collection_traffic_cache.go index 07f4f6e0..a7597aa2 100644 --- a/src/mapper/pkg/metrics_collection_traffic/metrics_collection_traffic_cache.go +++ b/src/mapper/pkg/metrics_collection_traffic/metrics_collection_traffic_cache.go @@ -11,6 +11,11 @@ import ( "github.com/spf13/viper" "golang.org/x/exp/slices" "hash/crc32" + "time" +) + +const ( + cacheTTL = 5 * time.Hour ) type CacheValue []byte @@ -21,9 +26,7 @@ type MetricsCollectionTrafficCache struct { func NewMetricsCollectionTrafficCache() *MetricsCollectionTrafficCache { size := viper.GetInt(config.MetricsCollectionTrafficCacheSizeKey) - // We don't want the cache to expire. It does not contain a lot of data, and we want to keep it as long as possible - // so we won't send unnecessary requests to the cloud. - cache := expirable.NewLRU[string, CacheValue](size, OnEvict, 0) + cache := expirable.NewLRU[string, CacheValue](size, OnEvict, cacheTTL) return &MetricsCollectionTrafficCache{ cache: cache, diff --git a/src/mapper/pkg/webhook_traffic/webhook_services_cache.go b/src/mapper/pkg/webhook_traffic/webhook_services_cache.go index e57b6133..d8587ded 100644 --- a/src/mapper/pkg/webhook_traffic/webhook_services_cache.go +++ b/src/mapper/pkg/webhook_traffic/webhook_services_cache.go @@ -14,6 +14,10 @@ import ( "time" ) +const ( + cacheTTL = 5 * time.Hour +) + type CacheValue []byte type WebhookServicesCache struct { @@ -22,7 +26,7 @@ type WebhookServicesCache struct { func NewWebhookServicesCache() *WebhookServicesCache { size := viper.GetInt(config.WebhookServicesCacheSizeKey) - cache := expirable.NewLRU[string, CacheValue](size, OnEvict, 5*time.Hour) + cache := expirable.NewLRU[string, CacheValue](size, OnEvict, cacheTTL) return &WebhookServicesCache{ cache: cache,