Don't return errors when creating caches (it is better just to crash since otherwise we'll nil-pointer exception everywhere)

This commit is contained in:
Neil Alexander 2022-07-08 10:40:51 +01:00
parent 0c63a8e0d3
commit 7b8216775a
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
5 changed files with 21 additions and 40 deletions

View file

@ -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)
}

View file

@ -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*")

View file

@ -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 {

View file

@ -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)

View file

@ -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(),