diff --git a/cmd/resolve-state/main.go b/cmd/resolve-state/main.go index 74d41c2aa..6852ab4fb 100644 --- a/cmd/resolve-state/main.go +++ b/cmd/resolve-state/main.go @@ -54,12 +54,10 @@ func main() { fmt.Println("Fetching", len(snapshotNIDs), "snapshot NIDs") - cache, err := caching.NewRistrettoCache(128*1024*1024, time.Hour, true) - if err != nil { - panic(err) - } - - roomserverDB, err := storage.Open(base, &cfg.RoomServer.Database, cache) + roomserverDB, err := storage.Open( + base, &cfg.RoomServer.Database, + caching.NewRistrettoCache(128*1024*1024, time.Hour, true), + ) if err != nil { panic(err) } diff --git a/federationapi/federationapi_keys_test.go b/federationapi/federationapi_keys_test.go index 5860ff2d0..b9ccb57e8 100644 --- a/federationapi/federationapi_keys_test.go +++ b/federationapi/federationapi_keys_test.go @@ -64,10 +64,7 @@ func TestMain(m *testing.M) { } // Create a new cache but don't enable prometheus! - s.cache, err = caching.NewRistrettoCache(8*1024*1024, time.Hour, false) - if err != nil { - panic("can't create cache: " + err.Error()) - } + s.cache = caching.NewRistrettoCache(8*1024*1024, time.Hour, false) // Create a temporary directory for JetStream. d, err := ioutil.TempDir("./", "jetstream*") diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 9a5224129..56d2f70f9 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -29,7 +29,19 @@ import ( "github.com/prometheus/client_golang/prometheus/promauto" ) -func mustCreateCache(maxCost config.DataUnit, enablePrometheus bool) *ristretto.Cache { +const ( + roomVersionsCache byte = iota + 1 + serverKeysCache + roomIDsCache + roomEventsCache + roomInfosCache + federationPDUsCache + federationEDUsCache + spaceSummaryRoomsCache + lazyLoadingCache +) + +func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enablePrometheus bool) *Caches { cache, err := ristretto.NewCache(&ristretto.Config{ NumCounters: 1e5, MaxCost: int64(maxCost), @@ -58,23 +70,6 @@ func mustCreateCache(maxCost config.DataUnit, enablePrometheus bool) *ristretto. return float64(cache.Metrics.CostAdded() - cache.Metrics.CostEvicted()) }) } - return cache -} - -const ( - roomVersionsCache byte = iota + 1 - serverKeysCache - roomIDsCache - roomEventsCache - roomInfosCache - federationPDUsCache - federationEDUsCache - spaceSummaryRoomsCache - lazyLoadingCache -) - -func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enablePrometheus bool) (*Caches, error) { - cache := mustCreateCache(maxCost, enablePrometheus) return &Caches{ RoomVersions: &RistrettoCachePartition[string, gomatrixserverlib.RoomVersion]{ cache: cache, @@ -133,7 +128,7 @@ func NewRistrettoCache(maxCost config.DataUnit, maxAge time.Duration, enableProm Mutable: true, MaxAge: maxAge, }, - }, nil + } } type RistrettoCostedCachePartition[k keyable, v costable] struct { diff --git a/roomserver/internal/input/input_test.go b/roomserver/internal/input/input_test.go index 9108f0ad9..4708560ac 100644 --- a/roomserver/internal/input/input_test.go +++ b/roomserver/internal/input/input_test.go @@ -48,10 +48,6 @@ func TestSingleTransactionOnInput(t *testing.T) { Kind: api.KindOutlier, // don't panic if we generate an output event Event: event.Headered(gomatrixserverlib.RoomVersionV6), } - cache, err := caching.NewRistrettoCache(8*1024*1024, time.Hour, false) - if err != nil { - t.Fatal(err) - } db, err := storage.Open( nil, &config.DatabaseOptions{ @@ -59,7 +55,7 @@ func TestSingleTransactionOnInput(t *testing.T) { MaxOpenConnections: 1, MaxIdleConnections: 1, }, - cache, + caching.NewRistrettoCache(8*1024*1024, time.Hour, false), ) if err != nil { t.Logf("PostgreSQL not available (%s), skipping", err) diff --git a/setup/base/base.go b/setup/base/base.go index 991d2f25c..93ab87de1 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -161,11 +161,6 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, options ...Base } } - cache, err := caching.NewRistrettoCache(cfg.Global.Cache.EstimatedMaxSize, cfg.Global.Cache.MaxAge, enableMetrics) - if err != nil { - logrus.WithError(err).Fatalf("Failed to create cache") - } - var dnsCache *gomatrixserverlib.DNSCache if cfg.Global.DNSCache.Enabled { dnsCache = gomatrixserverlib.NewDNSCache( @@ -233,7 +228,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, options ...Base UseHTTPAPIs: useHTTPAPIs, tracerCloser: closer, Cfg: cfg, - Caches: cache, + Caches: caching.NewRistrettoCache(cfg.Global.Cache.EstimatedMaxSize, cfg.Global.Cache.MaxAge, enableMetrics), DNSCache: dnsCache, PublicClientAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicClientPathPrefix).Subrouter().UseEncodedPath(), PublicFederationAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicFederationPathPrefix).Subrouter().UseEncodedPath(),