From 56b2672f9260d3262ce5e243ac4e3ba06dc36194 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 7 Jul 2022 14:27:58 +0100 Subject: [PATCH] Tweak max age handling, config key name --- dendrite-sample.monolith.yaml | 7 ++++--- dendrite-sample.polylith.yaml | 7 ++++--- internal/caching/impl_ristretto.go | 11 +++++++++-- setup/config/config_global.go | 4 ++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/dendrite-sample.monolith.yaml b/dendrite-sample.monolith.yaml index 88f259ef6..c8c06d6c5 100644 --- a/dendrite-sample.monolith.yaml +++ b/dendrite-sample.monolith.yaml @@ -41,15 +41,16 @@ global: max_idle_conns: 5 conn_max_lifetime: -1 - # Configuration for in-process caches, to help speed up processing transactions - # and reducing load on the database. + # Configuration for in-memory caches. Caches can often improve performance by + # keeping frequently accessed items (like events, identifiers etc.) in memory + # rather than having to read them from the database. cache: # The estimated maximum size for the global cache in bytes, or in terabytes, # gigabytes, megabytes or kilobytes when the appropriate 'tb', 'gb', 'mb' or # 'kb' suffix is specified. Note that this is not a hard limit, nor is it a # memory limit for the entire process. A cache that is too small may ultimately # provide little or no benefit. - max_bytes_est: 1gb + max_size_est: 1gb # The maximum amount of time that a cache entry can live for in memory before # it will be evicted and/or refreshed from the database. Lower values result in diff --git a/dendrite-sample.polylith.yaml b/dendrite-sample.polylith.yaml index b94d828ca..c87714b51 100644 --- a/dendrite-sample.polylith.yaml +++ b/dendrite-sample.polylith.yaml @@ -31,15 +31,16 @@ global: # considered valid by other homeservers. key_validity_period: 168h0m0s - # Configuration for in-process caches, to help speed up processing transactions - # and reducing load on the database. + # Configuration for in-memory caches. Caches can often improve performance by + # keeping frequently accessed items (like events, identifiers etc.) in memory + # rather than having to read them from the database. cache: # The estimated maximum size for the global cache in bytes, or in terabytes, # gigabytes, megabytes or kilobytes when the appropriate 'tb', 'gb', 'mb' or # 'kb' suffix is specified. Note that this is not a hard limit, nor is it a # memory limit for the entire process. A cache that is too small may ultimately # provide little or no benefit. - max_bytes_est: 1gb + max_size_est: 1gb # The maximum amount of time that a cache entry can live for in memory before # it will be evicted and/or refreshed from the database. Lower values result in diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 65d7af355..0db4de5e6 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -94,7 +94,7 @@ func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enableProm cache: cache, Prefix: federationPDUsCache, Mutable: true, - MaxAge: time.Hour / 2, + MaxAge: lesserOf(time.Hour/2, maxAge), }, }, FederationEDUs: &RistrettoCostedCachePartition[int64, *gomatrixserverlib.EDU]{ @@ -102,7 +102,7 @@ func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enableProm cache: cache, Prefix: federationEDUsCache, Mutable: true, - MaxAge: time.Hour / 2, + MaxAge: lesserOf(time.Hour/2, maxAge), }, }, SpaceSummaryRooms: &RistrettoCachePartition[string, gomatrixserverlib.MSC2946SpacesResponse]{ @@ -174,3 +174,10 @@ func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) { value, ok = v.(V) return } + +func lesserOf(a, b time.Duration) time.Duration { + if a < b { + return a + } + return b +} diff --git a/setup/config/config_global.go b/setup/config/config_global.go index 525e36ed6..e1ccad7b6 100644 --- a/setup/config/config_global.go +++ b/setup/config/config_global.go @@ -176,7 +176,7 @@ func (c *ServerNotices) Defaults(generate bool) { func (c *ServerNotices) Verify(errors *ConfigErrors, isMonolith bool) {} type Cache struct { - EstMaxSize DataUnit `yaml:"max_bytes_est"` + EstMaxSize DataUnit `yaml:"max_size_est"` MaxAge time.Duration `yaml:"max_age"` } @@ -186,7 +186,7 @@ func (c *Cache) Defaults(generate bool) { } func (c *Cache) Verify(errors *ConfigErrors, isMonolith bool) { - checkPositive(errors, "max_bytes_est", int64(c.EstMaxSize)) + checkPositive(errors, "max_size_est", int64(c.EstMaxSize)) } // ReportStats configures opt-in anonymous stats reporting.