mirror of
https://github.com/1f349/dendrite.git
synced 2024-11-14 15:51:37 +00:00
57b7fa3db8
* More key tweaks * Start testing stuff * Move responsibility for generating local keys into server key API, don't register prom in caches unless needed, start tests * Don't store our own keys in the database * Don't store our own keys in the database * Don't run tests for now * Tweak caching behaviour, update tests * Update comments, add fixes from forward-merge * Debug logging * Debug logging * Perform final comparison against original set of requests * oops * Fetcher timeouts * Fetcher timeouts * missing func * Tweaks * Update gomatrixserverlib * Fix Federation API test * Break up FetchKeys * Add comments to caching * Add URL check in test * Partially revert "Move responsibility for generating local keys into server key API, don't register prom in caches unless needed, start tests" This reverts commit d7eb54c5b30b2f6a9d6514b643e32e6ad2b602f3. * Fix federation API test * Fix internal cache stuff again * Fix server key API test * Update comments * Update comments from review * Fix lint
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
)
|
|
|
|
// A Database implements gomatrixserverlib.KeyDatabase and is used to store
|
|
// the public keys for other matrix servers.
|
|
type KeyDatabase struct {
|
|
inner gomatrixserverlib.KeyDatabase
|
|
cache caching.ServerKeyCache
|
|
}
|
|
|
|
func NewKeyDatabase(inner gomatrixserverlib.KeyDatabase, cache caching.ServerKeyCache) (*KeyDatabase, error) {
|
|
if inner == nil {
|
|
return nil, errors.New("inner database can't be nil")
|
|
}
|
|
if cache == nil {
|
|
return nil, errors.New("cache can't be nil")
|
|
}
|
|
return &KeyDatabase{
|
|
inner: inner,
|
|
cache: cache,
|
|
}, nil
|
|
}
|
|
|
|
// FetcherName implements KeyFetcher
|
|
func (d KeyDatabase) FetcherName() string {
|
|
return "InMemoryKeyCache"
|
|
}
|
|
|
|
// FetchKeys implements gomatrixserverlib.KeyDatabase
|
|
func (d *KeyDatabase) FetchKeys(
|
|
ctx context.Context,
|
|
requests map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.Timestamp,
|
|
) (map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult, error) {
|
|
results := make(map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult)
|
|
for req, ts := range requests {
|
|
if res, cached := d.cache.GetServerKey(req, ts); cached {
|
|
results[req] = res
|
|
delete(requests, req)
|
|
}
|
|
}
|
|
fromDB, err := d.inner.FetchKeys(ctx, requests)
|
|
if err != nil {
|
|
return results, err
|
|
}
|
|
for req, res := range fromDB {
|
|
results[req] = res
|
|
d.cache.StoreServerKey(req, res)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// StoreKeys implements gomatrixserverlib.KeyDatabase
|
|
func (d *KeyDatabase) StoreKeys(
|
|
ctx context.Context,
|
|
keyMap map[gomatrixserverlib.PublicKeyLookupRequest]gomatrixserverlib.PublicKeyLookupResult,
|
|
) error {
|
|
for req, res := range keyMap {
|
|
d.cache.StoreServerKey(req, res)
|
|
}
|
|
return d.inner.StoreKeys(ctx, keyMap)
|
|
}
|