Skip to content

Commit

Permalink
ZING-31498: Add default Min and Max TTL (#26)
Browse files Browse the repository at this point in the history
* replace min and max TTL for constant

* add definitions to default mi and max TTL

* Increase default ttls

---------

Co-authored-by: Danylo Kondratiev <[email protected]>
  • Loading branch information
JuanC1303 and knightpp authored Dec 12, 2023
1 parent 035daec commit 373c1ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 21 additions & 8 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 373c1ca

Please sign in to comment.