More strict keyable interface

This commit is contained in:
Neil Alexander 2022-06-14 14:08:09 +01:00
parent 7f2584a7c5
commit e614269bc3
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
3 changed files with 11 additions and 6 deletions

View file

@ -1,6 +1,8 @@
package caching package caching
import ( import (
"fmt"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -30,11 +32,12 @@ func (c Caches) GetServerKey(
request gomatrixserverlib.PublicKeyLookupRequest, request gomatrixserverlib.PublicKeyLookupRequest,
timestamp gomatrixserverlib.Timestamp, timestamp gomatrixserverlib.Timestamp,
) (gomatrixserverlib.PublicKeyLookupResult, bool) { ) (gomatrixserverlib.PublicKeyLookupResult, bool) {
val, found := c.ServerKeys.Get(request) key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
val, found := c.ServerKeys.Get(key)
if found && !val.WasValidAt(timestamp, true) { if found && !val.WasValidAt(timestamp, true) {
// The key wasn't valid at the requested timestamp so don't // The key wasn't valid at the requested timestamp so don't
// return it. The caller will have to work out what to do. // return it. The caller will have to work out what to do.
c.ServerKeys.Unset(request) c.ServerKeys.Unset(key)
return gomatrixserverlib.PublicKeyLookupResult{}, false return gomatrixserverlib.PublicKeyLookupResult{}, false
} }
return val, found return val, found
@ -44,5 +47,6 @@ func (c Caches) StoreServerKey(
request gomatrixserverlib.PublicKeyLookupRequest, request gomatrixserverlib.PublicKeyLookupRequest,
response gomatrixserverlib.PublicKeyLookupResult, response gomatrixserverlib.PublicKeyLookupResult,
) { ) {
c.ServerKeys.Set(request, response) key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
c.ServerKeys.Set(key, response)
} }

View file

@ -12,7 +12,7 @@ import (
// interface. // interface.
type Caches struct { type Caches struct {
RoomVersions Cache[string, gomatrixserverlib.RoomVersion] RoomVersions Cache[string, gomatrixserverlib.RoomVersion]
ServerKeys Cache[gomatrixserverlib.PublicKeyLookupRequest, gomatrixserverlib.PublicKeyLookupResult] ServerKeys Cache[string, gomatrixserverlib.PublicKeyLookupResult]
RoomServerRoomNIDs Cache[string, types.RoomNID] RoomServerRoomNIDs Cache[string, types.RoomNID]
RoomServerRoomIDs Cache[types.RoomNID, string] RoomServerRoomIDs Cache[types.RoomNID, string]
RoomInfos Cache[string, types.RoomInfo] RoomInfos Cache[string, types.RoomInfo]
@ -30,7 +30,8 @@ type Cache[K keyable, T any] interface {
} }
type keyable interface { type keyable interface {
comparable // from https://github.com/dgraph-io/ristretto/blob/8e850b710d6df0383c375ec6a7beae4ce48fc8d5/z/z.go#L34
~uint64 | ~string | ~[]byte | ~byte | ~int | ~int32 | ~uint32 | ~int64
} }
type costable interface { type costable interface {

View file

@ -33,7 +33,7 @@ func NewRistrettoCache(maxCost CacheSize, enablePrometheus bool) (*Caches, error
cache: cache, cache: cache,
Name: "room_versions", Name: "room_versions",
}, },
ServerKeys: &RistrettoCachePartition[gomatrixserverlib.PublicKeyLookupRequest, gomatrixserverlib.PublicKeyLookupResult]{ ServerKeys: &RistrettoCachePartition[string, gomatrixserverlib.PublicKeyLookupResult]{
cache: cache, cache: cache,
Name: "server_keys", Name: "server_keys",
Mutable: true, Mutable: true,