From 7066f92ffd6ae335537986470faf66ffe155b59a Mon Sep 17 00:00:00 2001 From: evgeniy-scherbina Date: Wed, 18 Oct 2023 18:04:21 -0400 Subject: [PATCH] Rename ChainID -> CachePrefix and add validation --- .env | 10 +++++++++- config/config.go | 15 +++++---------- config/validate.go | 14 ++++++++++++++ service/cachemdw/cache.go | 5 +++-- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/.env b/.env index a54ad67..8a89607 100644 --- a/.env +++ b/.env @@ -89,9 +89,17 @@ METRIC_PARTITIONING_ROUTINE_DELAY_FIRST_RUN_SECONDS=10 METRIC_PARTITIONINING_PREFILL_PERIOD_DAYS=7 # Used by `ready` script to ensure metric partitions have been created. MINIMUM_REQUIRED_PARTITIONS=30 +# RedisEndpointURL is an url of redis, for ex: redis:6379 REDIS_ENDPOINT_URL=redis:6379 REDIS_PASSWORD= -CHAIN_ID=local-chain +# TTL for cached evm requests +# TTL should be specified in seconds +CACHE_TTL=600 +# CachePrefix is used as prefix for any key in the cache, key has such structure: +# :evm-request::sha256: +# Possible values are testnet, mainnet, etc... +# CachePrefix must not contain colon symbol +CACHE_PREFIX=local-chain ##### Database Config POSTGRES_PASSWORD=password diff --git a/config/config.go b/config/config.go index baa784a..256a54a 100644 --- a/config/config.go +++ b/config/config.go @@ -39,12 +39,8 @@ type Config struct { MetricPartitioningPrefillPeriodDays int RedisEndpointURL string RedisPassword string - // TTL for cached evm requests - CacheTTL time.Duration - // CachePrefix is used as prefix for any key in the cache, key has such structure: - // :evm-request::sha256: - // Possible values are testnet, mainnet, etc... - CachePrefix string + CacheTTL time.Duration + CachePrefix string } const ( @@ -90,8 +86,7 @@ const ( REDIS_ENDPOINT_URL_ENVIRONMENT_KEY = "REDIS_ENDPOINT_URL" REDIS_PASSWORD_ENVIRONMENT_KEY = "REDIS_PASSWORD" CACHE_TTL_ENVIRONMENT_KEY = "CACHE_TTL" - DEFAULT_CACHE_TTL_SECONDS = 600 - CHAIN_ID_ENVIRONMENT_KEY = "CHAIN_ID" + CACHE_PREFIX_ENVIRONMENT_KEY = "CACHE_PREFIX" ) // EnvOrDefault fetches an environment variable value, or if not set returns the fallback value @@ -219,7 +214,7 @@ func ReadConfig() Config { MetricPartitioningPrefillPeriodDays: EnvOrDefaultInt(METRIC_PARTITIONING_PREFILL_PERIOD_DAYS_ENVIRONMENT_KEY, DEFAULT_METRIC_PARTITIONING_PREFILL_PERIOD_DAYS), RedisEndpointURL: os.Getenv(REDIS_ENDPOINT_URL_ENVIRONMENT_KEY), RedisPassword: os.Getenv(REDIS_PASSWORD_ENVIRONMENT_KEY), - CacheTTL: time.Duration(EnvOrDefaultInt(CACHE_TTL_ENVIRONMENT_KEY, DEFAULT_CACHE_TTL_SECONDS)) * time.Second, - CachePrefix: os.Getenv(CHAIN_ID_ENVIRONMENT_KEY), + CacheTTL: time.Duration(EnvOrDefaultInt(CACHE_TTL_ENVIRONMENT_KEY, 0)) * time.Second, + CachePrefix: os.Getenv(CACHE_PREFIX_ENVIRONMENT_KEY), } } diff --git a/config/validate.go b/config/validate.go index 446fb1c..afa15c8 100644 --- a/config/validate.go +++ b/config/validate.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strconv" + "strings" ) var ( @@ -48,5 +49,18 @@ func Validate(config Config) error { allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %d, must be non-zero and less than or equal to %d", METRIC_PARTITIONING_PREFILL_PERIOD_DAYS_ENVIRONMENT_KEY, config.MetricPartitioningPrefillPeriodDays, MaxMetricPartitioningPrefillPeriodDays)) } + if config.RedisEndpointURL == "" { + allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must not be empty", REDIS_ENDPOINT_URL_ENVIRONMENT_KEY, config.RedisEndpointURL)) + } + if config.CacheTTL <= 0 { + allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must be greater than zero", CACHE_TTL_ENVIRONMENT_KEY, config.CacheTTL)) + } + if strings.Contains(config.CachePrefix, ":") { + allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must not contain colon symbol", CACHE_PREFIX_ENVIRONMENT_KEY, config.CachePrefix)) + } + if config.CachePrefix == "" { + allErrs = errors.Join(allErrs, fmt.Errorf("invalid %s specified %s, must not be empty", CACHE_PREFIX_ENVIRONMENT_KEY, config.CachePrefix)) + } + return allErrs } diff --git a/service/cachemdw/cache.go b/service/cachemdw/cache.go index 7ecaa11..8114420 100644 --- a/service/cachemdw/cache.go +++ b/service/cachemdw/cache.go @@ -14,8 +14,9 @@ import ( // ServiceCache is responsible for caching EVM requests and provides corresponding middleware // ServiceCache can work with any underlying storage which implements simple cache.Cache interface type ServiceCache struct { - cacheClient cache.Cache - blockGetter decode.EVMBlockGetter + cacheClient cache.Cache + blockGetter decode.EVMBlockGetter + // TTL for cached evm requests cacheTTL time.Duration decodedRequestContextKey any // cachePrefix is used as prefix for any key in the cache