From 373c1cab3b80194ce943962ad4a4d753f59f77ce Mon Sep 17 00:00:00 2001 From: JuanC1303 <122574454+JuanC1303@users.noreply.github.com> Date: Tue, 12 Dec 2023 11:53:38 -0600 Subject: [PATCH] ZING-31498: Add default Min and Max TTL (#26) * replace min and max TTL for constant * add definitions to default mi and max TTL * Increase default ttls --------- Co-authored-by: Danylo Kondratiev --- Makefile | 1 + endpoint/endpoint.go | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index a4baf3f..355046d 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ BUILD_IMG := zenoss/zenkit-build:$(ZENKIT_BUILD_VERSION) DOCKER_PARAMS := --rm \ --volume $(ROOTDIR):/workspace/:rw \ --env CGO_ENABLED=1 \ + --env LOCAL_USER_ID=$(shell id -u) \ --workdir /workspace/ DOCKER_CMD := docker run -t $(DOCKER_PARAMS) $(BUILD_IMG) diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index 9d70673..2006529 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -56,6 +56,14 @@ const ( // Overridden by Config.BundleConfig.BundleDelayThreshold. DefaultBundleDelayThreshold = 1 * time.Second + // DefaultMinTTL is the default minimum TTL for local cache. + // Overridden by Config.MinTTL. + DefaultMinTTL = 6 * time.Hour + + // DefaultMaxTTL is the default maximum TTL for local cache. + // Overridden by Config.MaxTTL. + DefaultMaxTTL = 12 * time.Hour + // APIKeyHeader is the gRPC header field containing a Zenoss API key. APIKeyHeader = "zenoss-api-key" ) @@ -114,9 +122,13 @@ type Config struct { TestRegClient data_registry.DataRegistryServiceClient // Min TTL for local cache + // + // Default: 6 hour MinTTL int `yaml:"minTTL"` // Max TTL for local cache + // + // Default: 12 hour MaxTTL int `yaml:"maxTTL"` // CacheSizeLimit for local cache @@ -179,21 +191,19 @@ type MetricIDNameAndHash struct { } // initCache initialises the ttl cache used to store metric ids -func initCache(cacheSizeLimit int, minTTL int, maxTTL int) *ttlcache.Cache[string, MetricIDNameAndHash] { - cache := ttlcache.New[string, MetricIDNameAndHash]( +func initCache(cacheSizeLimit, minTTL, maxTTL int) *ttlcache.Cache[string, MetricIDNameAndHash] { + return ttlcache.New[string, MetricIDNameAndHash]( ttlcache.WithTTL[string, MetricIDNameAndHash](getCacheItemTTL(minTTL, maxTTL)), ttlcache.WithCapacity[string, MetricIDNameAndHash](uint64(cacheSizeLimit)), ttlcache.WithDisableTouchOnHit[string, MetricIDNameAndHash](), ) - return cache } -// getCacheItemTTL gets a ttl selected at randon within max and min ttl limits +// getCacheItemTTL gets a ttl selected at random within max and min ttl limits func getCacheItemTTL(minTTL int, maxTTL int) time.Duration { return time.Duration(getRandInRange(minTTL, maxTTL)) * time.Second } -// getRandInRange gets a random number in range func getRandInRange(min, max int) int { return rand.Intn(max-min) + min } @@ -263,10 +273,13 @@ func New(config Config) (*Endpoint, error) { } var cache *ttlcache.Cache[string, MetricIDNameAndHash] - if config.MinTTL != 0 && config.MaxTTL != 0 { - // config.CacheSizeLimit == 0 means no limit - cache = initCache(config.CacheSizeLimit, config.MinTTL, config.MaxTTL) + if config.MinTTL <= 0 { + config.MinTTL = int(DefaultMinTTL.Seconds()) + } + if config.MaxTTL <= 0 { + config.MaxTTL = int(DefaultMaxTTL.Seconds()) } + cache = initCache(config.CacheSizeLimit, config.MinTTL, config.MaxTTL) e := &Endpoint{ config: config,