mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-07 06:03:09 -06:00
Cost estimation fixes
This commit is contained in:
parent
e0466c7777
commit
8460f1dc08
2
go.mod
2
go.mod
|
|
@ -25,7 +25,7 @@ require (
|
|||
github.com/matrix-org/dugong v0.0.0-20210921133753-66e6b1c67e2e
|
||||
github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220614141330-dff20984ec15
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220615144844-8535862e3770
|
||||
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48
|
||||
github.com/matrix-org/util v0.0.0-20200807132607-55161520e1d4
|
||||
github.com/mattn/go-sqlite3 v1.14.13
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -341,8 +341,8 @@ github.com/matrix-org/go-sqlite3-js v0.0.0-20220419092513-28aa791a1c91/go.mod h1
|
|||
github.com/matrix-org/gomatrix v0.0.0-20190528120928-7df988a63f26/go.mod h1:3fxX6gUjWyI/2Bt7J1OLhpCzOfO/bB3AiX0cJtEKud0=
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16 h1:ZtO5uywdd5dLDCud4r0r55eP4j9FuUNpl60Gmntcop4=
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220614141330-dff20984ec15 h1:hfOBccdH5cIp8EQCbKx64XOaQ3ExDnR9P864N+qYaEo=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220614141330-dff20984ec15/go.mod h1:jX38yp3SSLJNftBg3PXU1ayd0PCLIiDHQ4xAc9DIixk=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220615144844-8535862e3770 h1:1czNJFGVb2K6kvpU7j9DYdmBmEe1/rDRffDj//BD3/Q=
|
||||
github.com/matrix-org/gomatrixserverlib v0.0.0-20220615144844-8535862e3770/go.mod h1:jX38yp3SSLJNftBg3PXU1ayd0PCLIiDHQ4xAc9DIixk=
|
||||
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48 h1:W0sjjC6yjskHX4mb0nk3p0fXAlbU5bAFUFeEtlrPASE=
|
||||
github.com/matrix-org/pinecone v0.0.0-20220408153826-2999ea29ed48/go.mod h1:ulJzsVOTssIVp1j/m5eI//4VpAGDkMt5NrRuAVX7wpc=
|
||||
github.com/matrix-org/util v0.0.0-20190711121626-527ce5ddefc7/go.mod h1:vVQlW/emklohkZnOPwD3LrZUBqdfsbiyO3p1lNV8F6U=
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ type keyable interface {
|
|||
}
|
||||
|
||||
type costable interface {
|
||||
CacheCost() int64
|
||||
CacheCost() int
|
||||
}
|
||||
|
||||
type CacheSize int64
|
||||
|
|
|
|||
|
|
@ -75,10 +75,12 @@ func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error
|
|||
Prefix: roomIDsCache,
|
||||
MaxAge: time.Hour,
|
||||
},
|
||||
RoomServerEvents: &RistrettoCachePartition[int64, *gomatrixserverlib.Event]{
|
||||
cache: cache,
|
||||
Prefix: roomEventsCache,
|
||||
MaxAge: time.Hour,
|
||||
RoomServerEvents: &RistrettoCostedCachePartition[int64, *gomatrixserverlib.Event]{
|
||||
&RistrettoCachePartition[int64, *gomatrixserverlib.Event]{
|
||||
cache: cache,
|
||||
Prefix: roomEventsCache,
|
||||
MaxAge: time.Hour,
|
||||
},
|
||||
},
|
||||
RoomInfos: &RistrettoCachePartition[string, types.RoomInfo]{
|
||||
cache: cache,
|
||||
|
|
@ -86,17 +88,21 @@ func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error
|
|||
Mutable: true,
|
||||
MaxAge: time.Hour,
|
||||
},
|
||||
FederationPDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{
|
||||
cache: cache,
|
||||
Prefix: federationPDUsCache,
|
||||
Mutable: true,
|
||||
MaxAge: time.Hour / 2,
|
||||
FederationPDUs: &RistrettoCostedCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{
|
||||
&RistrettoCachePartition[int64, *gomatrixserverlib.HeaderedEvent]{
|
||||
cache: cache,
|
||||
Prefix: federationPDUsCache,
|
||||
Mutable: true,
|
||||
MaxAge: time.Hour / 2,
|
||||
},
|
||||
},
|
||||
FederationEDUs: &RistrettoCachePartition[int64, *gomatrixserverlib.EDU]{
|
||||
cache: cache,
|
||||
Prefix: federationEDUsCache,
|
||||
Mutable: true,
|
||||
MaxAge: time.Hour / 2,
|
||||
FederationEDUs: &RistrettoCostedCachePartition[int64, *gomatrixserverlib.EDU]{
|
||||
&RistrettoCachePartition[int64, *gomatrixserverlib.EDU]{
|
||||
cache: cache,
|
||||
Prefix: federationEDUsCache,
|
||||
Mutable: true,
|
||||
MaxAge: time.Hour / 2,
|
||||
},
|
||||
},
|
||||
SpaceSummaryRooms: &RistrettoCachePartition[string, gomatrixserverlib.MSC2946SpacesResponse]{
|
||||
cache: cache,
|
||||
|
|
@ -120,22 +126,24 @@ type RistrettoCachePartition[K keyable, V any] struct {
|
|||
MaxAge time.Duration
|
||||
}
|
||||
|
||||
func (c *RistrettoCachePartition[K, V]) Set(key K, value V) {
|
||||
func (c *RistrettoCachePartition[K, V]) setWithCost(key K, value V, cost int64) {
|
||||
bkey := fmt.Sprintf("%c%v", c.Prefix, key)
|
||||
if !c.Mutable {
|
||||
if v, ok := c.cache.Get(bkey); ok && v != nil && !reflect.DeepEqual(v, value) {
|
||||
panic(fmt.Sprintf("invalid use of immutable cache tries to change value of %v from %v to %v", key, v, value))
|
||||
}
|
||||
}
|
||||
c.cache.SetWithTTL(bkey, value, cost, c.MaxAge)
|
||||
}
|
||||
|
||||
func (c *RistrettoCachePartition[K, V]) Set(key K, value V) {
|
||||
var cost int64
|
||||
if cv, ok := any(value).(costable); ok {
|
||||
cost = cv.CacheCost()
|
||||
} else if cv, ok := any(value).(string); ok {
|
||||
if cv, ok := any(value).(string); ok {
|
||||
cost = int64(len(cv))
|
||||
} else {
|
||||
cost = int64(unsafe.Sizeof(value))
|
||||
}
|
||||
c.cache.SetWithTTL(bkey, value, cost, c.MaxAge)
|
||||
c.setWithCost(key, value, cost)
|
||||
}
|
||||
|
||||
func (c *RistrettoCachePartition[K, V]) Unset(key K) {
|
||||
|
|
@ -156,3 +164,12 @@ func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) {
|
|||
value, ok = v.(V)
|
||||
return
|
||||
}
|
||||
|
||||
type RistrettoCostedCachePartition[k keyable, v costable] struct {
|
||||
*RistrettoCachePartition[k, v]
|
||||
}
|
||||
|
||||
func (c *RistrettoCostedCachePartition[K, V]) Set(key K, value V) {
|
||||
cost := value.CacheCost()
|
||||
c.setWithCost(key, value, int64(cost))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue