dendrite/internal/caching/cache_serverkeys.go

51 lines
1.6 KiB
Go
Raw Normal View History

2020-06-05 10:42:01 -05:00
package caching
import (
"fmt"
2020-06-12 05:07:26 -05:00
"time"
2020-06-05 10:42:01 -05:00
"github.com/matrix-org/gomatrixserverlib"
)
const (
ServerKeyCacheName = "server_key"
ServerKeyCacheMaxEntries = 4096
ServerKeyCacheMutable = true
)
// ServerKeyCache contains the subset of functions needed for
// a server key cache.
type ServerKeyCache interface {
GetServerKey(request gomatrixserverlib.PublicKeyLookupRequest) (response gomatrixserverlib.PublicKeyLookupResult, ok bool)
StoreServerKey(request gomatrixserverlib.PublicKeyLookupRequest, response gomatrixserverlib.PublicKeyLookupResult)
}
func (c Caches) GetServerKey(
request gomatrixserverlib.PublicKeyLookupRequest,
) (gomatrixserverlib.PublicKeyLookupResult, bool) {
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
2020-06-12 05:07:26 -05:00
now := gomatrixserverlib.AsTimestamp(time.Now())
2020-06-05 10:42:01 -05:00
val, found := c.ServerKeys.Get(key)
if found && val != nil {
if keyLookupResult, ok := val.(gomatrixserverlib.PublicKeyLookupResult); ok {
2020-06-12 05:07:26 -05:00
if !keyLookupResult.WasValidAt(now, true) {
// We appear to be past the key validity so don't return this
// with the results. This ensures that the cache doesn't return
// values that are not useful to us.
c.ServerKeys.Unset(key)
return gomatrixserverlib.PublicKeyLookupResult{}, false
}
2020-06-05 10:42:01 -05:00
return keyLookupResult, true
}
}
return gomatrixserverlib.PublicKeyLookupResult{}, false
}
func (c Caches) StoreServerKey(
request gomatrixserverlib.PublicKeyLookupRequest,
response gomatrixserverlib.PublicKeyLookupResult,
) {
key := fmt.Sprintf("%s/%s", request.ServerName, request.KeyID)
c.ServerKeys.Set(key, response)
}